ObjectConvertUtil

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Map;

import com.google.common.collect.Maps;

public class ObjectConvertUtil {

private static final Map<String, Method> CONVERTERS = Maps.newHashMap();
static {
    Method[] methods = ObjectConvertUtil.class.getDeclaredMethods();
    for (Method method : methods) {
        if (method.getParameterTypes().length == 1) {
            CONVERTERS.put(method.getParameterTypes()[0].getName() + "_"
                + method.getReturnType().getName(), method);
        }
    }
}
public static <T> T convert(Object from, Class<T> to) {

    if (from == null) {
        return null;
    }

    if (to.isAssignableFrom(from.getClass())) {
        return to.cast(from);
    }

    String converterId = from.getClass().getName() + "_" + to.getName();
    Method converter = CONVERTERS.get(converterId);
    if (converter == null) {
        throw new UnsupportedOperationException("Cannot convert from "
            + from.getClass().getName() + " to " + to.getName()
            + ". Requested converter does not exist.");
    }

    try {
        return to.cast(converter.invoke(to, from));
    } catch (Exception e) {
        throw new RuntimeException("Cannot convert from "
            + from.getClass().getName() + " to " + to.getName()
            + ". Conversion failed with " + e.getMessage(), e);
    }
}

public static Boolean integerToBoolean(Integer value) {
    return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
}

public static Integer booleanToInteger(Boolean value) {
    return value.booleanValue() ? Integer.valueOf(1) : Integer.valueOf(0);
}
public static BigDecimal doubleToBigDecimal(Double value) {
    return new BigDecimal(value.doubleValue());
}

public static Double bigDecimalToDouble(BigDecimal value) {
    return new Double(value.doubleValue());
}

public static String integerToString(Integer value) {
    return String.valueOf(value);
}

public static Integer stringToInteger(String value) {
    return Integer.parseInt(value);
}

public static String booleanToString(Boolean value) {
    return value.toString();
}

public static Boolean stringToBoolean(String value) {
    return Boolean.valueOf(value);
}

public static Long stringToLong(String value) {
    return Long.parseLong(value);
}

public static String longToString(Long value) {
    return String.valueOf(value);
}

public static Integer objectToInteger(Object value){
    return (Integer)value;
}

public static String objectToString(Object value){
    return (String)value;
}

public static Long objectToLong(Object value){
    return (Long)value;
}

public static Long doubleToLong(Double value) {
    return value.longValue();
}

public static Double longToDouble(Long value) {
    return value.doubleValue();
}

public static Integer doubleToInteger(Double value) {
    return value.intValue();
}

public static Double integerToDouble(Integer value) {
    return value.doubleValue();
}

public static String doubleToString(Double value) {
    return String.valueOf(value);
}

public static Double stringToDouble(String value) {
    return Double.parseDouble(value);
}

}

添加新评论