1 3 package jodd.typeconverter; 4 5 import jodd.mutable.*; 6 7 import java.util.HashMap ; 8 9 13 public class TypeConverterManager { 14 15 private static HashMap converters = new HashMap (); 16 17 static { 18 registerDefaults(); 19 } 20 21 24 public static void unregisterAll() { 25 converters.clear(); 26 } 27 28 31 public static void registerDefaults() { 32 register(String .class, new StringConverter()); 33 register(String [].class, new StringArrayConverter()); 34 35 register(Integer .class, new IntegerConverter()); 36 register(int.class, new IntegerConverter()); 37 register(MutableInteger.class, new MutableIntegerConverter()); 38 39 register(Short .class, new ShortConverter()); 40 register(short.class, new ShortConverter()); 41 register(MutableShort.class, new MutableShortConverter()); 42 43 register(Long .class, new LongConverter()); 44 register(long.class, new LongConverter()); 45 register(MutableLong.class, new MutableLongConverter()); 46 47 register(Byte .class, new ByteConverter()); 48 register(byte.class, new ByteConverter()); 49 register(MutableByte.class, new MutableByteConverter()); 50 51 register(Float .class, new FloatConverter()); 52 register(float.class, new FloatConverter()); 53 register(MutableFloat.class, new MutableFloatConverter()); 54 55 register(Double .class, new DoubleConverter()); 56 register(double.class, new DoubleConverter()); 57 register(MutableDouble.class, new MutableDoubleConverter()); 58 59 register(Boolean .class, new BooleanConverter()); 60 register(boolean.class, new BooleanConverter()); 61 62 register(Character .class, new CharacterConverter()); 63 register(char.class, new CharacterConverter()); 64 65 register(byte[].class, new ByteArrayConverter()); 66 register(short[].class, new ShortArrayConverter()); 67 register(int[].class, new IntegerArrayConverter()); 68 register(long[].class, new LongArrayConverter()); 69 register(float[].class, new FloatArrayConverter()); 70 register(double[].class, new DoubleArrayConverter()); 71 register(boolean[].class, new BooleanArrayConverter()); 72 73 74 register(java.math.BigDecimal .class, new BigDecimalConverter()); 75 register(java.math.BigInteger .class, new BigIntegerConverter()); 76 register(java.sql.Date .class, new SqlDateConverter()); 77 register(java.sql.Time .class, new SqlTimeConverter()); 78 register(java.sql.Timestamp .class, new SqlTimestampConverter()); 79 80 register(jodd.servlet.upload.FileUpload.class, new FileUploadConverter()); 81 register(jodd.datetime.JDateTime.class, new JDateTimeConverter()); 82 } 83 84 91 public static void register(Class type, TypeConverter typeConverter) { 92 converters.put(type, typeConverter); 93 } 94 95 96 public static void unregister(Class type) { 97 converters.remove(type); 98 } 99 100 106 public static TypeConverter lookup(Class type) { 107 return (TypeConverter) converters.get(type); 108 } 109 110 112 115 public static Object convert(Object value, Class type) { 116 TypeConverter conv = lookup(type); 117 if (conv == null) { 118 throw new TypeConversionException("Unable to find registered bean converter for type: '" + type.getName() + '\''); 119 } 120 return conv.convert(value); 121 } 122 123 124 127 public static Object convert(Object value, TypeConverter conv) { 128 return conv.convert(value); 129 } 130 } 131 | Popular Tags |