1 16 package net.sf.dozer.util.mapping.converters; 17 18 import java.math.BigDecimal ; 19 import java.math.BigInteger ; 20 import java.util.Calendar ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import net.sf.dozer.util.mapping.util.DateFormatContainer; 25 26 import org.apache.commons.beanutils.Converter; 27 import org.apache.commons.beanutils.converters.BigDecimalConverter; 28 import org.apache.commons.beanutils.converters.BigIntegerConverter; 29 import org.apache.commons.beanutils.converters.BooleanConverter; 30 import org.apache.commons.beanutils.converters.ByteConverter; 31 import org.apache.commons.beanutils.converters.CharacterConverter; 32 import org.apache.commons.beanutils.converters.DoubleConverter; 33 import org.apache.commons.beanutils.converters.FloatConverter; 34 import org.apache.commons.beanutils.converters.IntegerConverter; 35 import org.apache.commons.beanutils.converters.LongConverter; 36 import org.apache.commons.beanutils.converters.ShortConverter; 37 38 43 public class PrimitiveOrWrapperConverter { 44 private static final Map PRIMITIVE_TYPE_MAP = new HashMap (); 45 private static final Map CONVERTER_MAP = new HashMap (); 46 private static final Converter DEFAULT_CONVERTER = new StringConstructorConverter(); 47 48 static { 49 Class [] primitives = { Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE }; 51 Class [] wrappers = { Boolean .class, Byte .class, Character .class, Short .class, Integer .class, Long .class, Float .class, Double .class }; 52 for (int i = 0; i < primitives.length; i++) { 53 PRIMITIVE_TYPE_MAP.put(primitives[i], wrappers[i]); 54 } 55 CONVERTER_MAP.put(Integer .class, new IntegerConverter()); 57 CONVERTER_MAP.put(Double .class, new DoubleConverter()); 58 CONVERTER_MAP.put(Short .class, new ShortConverter()); 59 CONVERTER_MAP.put(Character .class, new CharacterConverter()); 60 CONVERTER_MAP.put(Long .class, new LongConverter()); 61 CONVERTER_MAP.put(Boolean .class, new BooleanConverter()); 62 CONVERTER_MAP.put(Byte .class, new ByteConverter()); 63 CONVERTER_MAP.put(Float .class, new FloatConverter()); 64 CONVERTER_MAP.put(BigDecimal .class, new BigDecimalConverter()); 65 CONVERTER_MAP.put(BigInteger .class, new BigIntegerConverter()); 66 } 67 68 public Object convert(Object sourceFieldValue, Class destFieldClass, DateFormatContainer dateFormatContainer) { 69 if (sourceFieldValue == null || destFieldClass == null 70 || (sourceFieldValue.equals("") && !destFieldClass.equals(String .class))) { 71 return null; 72 } 73 Converter converter = getPrimitiveOrWrapperConverter(destFieldClass, dateFormatContainer); 74 try { 75 return converter.convert(destFieldClass, sourceFieldValue); 76 } catch (org.apache.commons.beanutils.ConversionException e) { 77 throw new net.sf.dozer.util.mapping.converters.ConversionException(e); 78 } 79 } 80 81 private Converter getPrimitiveOrWrapperConverter(Class destClass, DateFormatContainer dateFormatContainer) { 82 if (String .class.equals(destClass)) { 83 return new StringConverter(dateFormatContainer); 84 } 85 86 Converter result = (Converter) CONVERTER_MAP.get(destClass.isPrimitive() ? wrapPrimitive(destClass) : destClass); 87 88 if (result == null) { 89 if (java.util.Date .class.isAssignableFrom(destClass)) { 90 result = new DateConverter(dateFormatContainer.getDateFormat()); 91 } 92 if (Calendar .class.isAssignableFrom(destClass)) { 93 result = new CalendarConverter(dateFormatContainer.getDateFormat()); 94 } 95 } 96 return result == null ? DEFAULT_CONVERTER : result; 97 } 98 99 private Class wrapPrimitive(Class c) { 100 return (Class ) PRIMITIVE_TYPE_MAP.get(c); 101 } 102 103 } | Popular Tags |