com.github.dactiv.fear.commons.Casts.java Source code

Java tutorial

Introduction

Here is the source code for com.github.dactiv.fear.commons.Casts.java

Source

/*
 * Copyright 2015 dactiv
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.dactiv.fear.commons;

import com.github.dactiv.fear.commons.enumeration.FieldType;
import org.apache.commons.beanutils.ConvertUtils;

/**
 * 
 *
 * @author maurice
 *
 */
@SuppressWarnings("unchecked")
public abstract class Casts {

    /**
     *  value 
     *
     * @param value 
     * @param <T> 
     *
     * @return ?
     */
    public static <T> T cast(Object value) {
        return (T) value;
    }

    /**
     *  value 
     *
     * @param value 
     * @param type  class
     *
     * @param <T> 
     *
     * @return ?
     */
    public static <T> T cast(Object value, Class<T> type) {
        return (T) (value == null ? null : ConvertUtils.convert(value, type));
    }

    /**
     *  value 
     *
     * @param value 
     * @param fieldType 
     *
     * @param <T> 
     *
     * @return ?
     */
    public static <T> T cast(Object value, FieldType fieldType) {
        return (T) cast(value, fieldType.getValue());
    }

    /**
     *  value 
     * @param value 
     * @param className  calss string
     *
     * @param <T> 
     *
     * @see FieldType
     *
     * @return ?
     */
    public static <T> T cast(Object value, String className) {
        return (T) cast(value, FieldType.getClass(className));
    }

}