public interface Converter { /** * 进行转换操作 * @param val 原对象 * @return 转换后对象 */ Object convert(Object val);}
public abstract class AbstractConverter implements Converter{ private boolean useDefaultVal = false; private Object defaultVal = null; public boolean isUseDefaultVal() { return useDefaultVal; } public void setUseDefaultVal(boolean useDefaultVal) { this.useDefaultVal = useDefaultVal; } public Object getDefaultVal() { return defaultVal; } public void setDefaultVal(Object defaultVal) { this.defaultVal = defaultVal; } protected abstract Class<?> getTargetClass(); protected abstract Object convert2Type(Object val); protected abstract String convert2String(Object val); @Override public Object convert(Object val) { if (null == val) return handleNullVal(val); if (getTargetClass().equals(val.getClass())) return getTargetClass().cast(val); if (getTargetClass().equals(String.class)) return getTargetClass().cast(convert2String(val)); return getTargetClass().cast(convert2Type(val)); } protected Object handleNullVal(Object val){ if (isUseDefaultVal()) return getDefaultVal(); return val; } protected <T> Class<T> primitiveToWrapper(final Class<T> type) { if (type == null || !type.isPrimitive()) return type; if (type == Integer.TYPE) return (Class<T>) Integer.class; if (type == Double.TYPE) return (Class<T>) Double.class; if (type == Long.TYPE) return (Class<T>) Long.class; if (type == Boolean.TYPE) return (Class<T>) Boolean.class; if (type == Float.TYPE) return (Class<T>) Float.class; if (type == Short.TYPE) return (Class<T>) Short.class; if (type == Byte.TYPE) return (Class<T>) Byte.class; if (type == Character.TYPE) return (Class<T>) Character.class; return type; }}
public abstract class NumberConverter extends AbstractConverter { @Override protected Number convert2Type(Object val) { final Class<?> sourceType = val.getClass(); Number targetVal; if (val instanceof Number) targetVal = (Number) val; else if (val instanceof Boolean) targetVal = ((Boolean)val).booleanValue() ? 1 : 0; else if (val instanceof Date) targetVal = ((Date)val).getTime(); else if (val instanceof Calendar) targetVal = ((Calendar)val).getTime().getTime(); else targetVal = toNumber(sourceType, (Class<? extends Number>) getTargetClass(), val.toString().trim()); return toNumber(sourceType, (Class<? extends Number>) getTargetClass(), targetVal); } @Override protected String convert2String(Object val) { return String.valueOf(val); } private Number toNumber(final Class<?> sourceType, final Class<? extends Number> targetType, final Number value){ if (targetType.equals(sourceType)) return targetType.cast(value); if (targetType.equals(Byte.class)){ final long longValue = value.longValue(); if (longValue > Byte.MAX_VALUE) throw new IllegalArgumentException("the value is bigger than Byte.MAX_VALUE"); if (longValue < Byte.MIN_VALUE) throw new IllegalArgumentException("the value is smaller than Byte.MIN_VALUE"); return value.byteValue(); } if (targetType.equals(Short.class)){ final long longValue = value.longValue(); if (longValue > Short.MAX_VALUE) throw new IllegalArgumentException("the value is bigger than Short.MAX_VALUE"); if (longValue < Short.MIN_VALUE) throw new IllegalArgumentException("the value is smaller than Short.MIN_VALUE"); return value.shortValue(); } if (targetType.equals(Integer.class)){ final long longValue = value.longValue(); if (longValue > Integer.MAX_VALUE) throw new IllegalArgumentException("the value is bigger than Integer.MAX_VALUE"); if (longValue < Integer.MIN_VALUE) throw new IllegalArgumentException("the value is smaller than Integer.MIN_VALUE"); return value.intValue(); } if (targetType.equals(Long.class)) { return targetType.cast(new Long(value.longValue())); } if (targetType.equals(Float.class)){ final double doubleValue = value.doubleValue(); if (doubleValue > Float.MAX_VALUE) throw new IllegalArgumentException("the value is bigger than Float.MAX_VALUE"); if (doubleValue < Float.MIN_VALUE) throw new IllegalArgumentException("the value is smaller than Float.MIN_VALUE"); return value.floatValue(); } if (targetType.equals(Double.class)) { return targetType.cast(new Double(value.doubleValue())); } // BigDecimal if (targetType.equals(BigDecimal.class)) { if (value instanceof Float || value instanceof Double) { return targetType.cast(new BigDecimal(value.toString())); } else if (value instanceof BigInteger) { return targetType.cast(new BigDecimal((BigInteger)value)); } else if (value instanceof BigDecimal) { return targetType.cast(new BigDecimal(value.toString())); } else { return targetType.cast(BigDecimal.valueOf(value.longValue())); } } if (targetType.equals(BigInteger.class)) { if (value instanceof BigDecimal) { return targetType.cast(((BigDecimal)value).toBigInteger()); } else { return targetType.cast(BigInteger.valueOf(value.longValue())); } } throw new IllegalArgumentException("can't convert from " + sourceType.getName() +" to " + targetType.getName()+" and the value is "+value); } private Number toNumber(final Class<?> sourceType, final Class<? extends Number> targetType, final String value){ // Byte if (targetType.equals(Byte.class)) { return new Byte(value); } // Short if (targetType.equals(Short.class)) { return new Short(value); } // Integer if (targetType.equals(Integer.class)) { return new Integer(value); } // Long if (targetType.equals(Long.class)) { return new Long(value); } // Float if (targetType.equals(Float.class)) { return new Float(value); } // Double if (targetType.equals(Double.class)) { return new Double(value); } // BigDecimal if (targetType.equals(BigDecimal.class)) { return new BigDecimal(value); } // BigInteger if (targetType.equals(BigInteger.class)) { return new BigInteger(value); } throw new IllegalArgumentException("can't convert from " + sourceType.getName() +" to " + targetType.getName()+" and the value is "+value); } }
public class IntegerConverter extends NumberConverter { @Override protected Class<?> getTargetClass() { return Integer.class; }}
public class FloatConverter extends NumberConverter { @Override protected Class<?> getTargetClass() { return Float.class; }}