KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > typeconverter > TypeConverterManager


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.typeconverter;
4
5 import jodd.mutable.*;
6
7 import java.util.HashMap JavaDoc;
8
9 /**
10  * Provides dynamic object conversion to a type.
11  * Contains a map of registered converters. User may add new converter.
12  */

13 public class TypeConverterManager {
14
15     private static HashMap JavaDoc converters = new HashMap JavaDoc();
16
17     static {
18         registerDefaults();
19     }
20
21     /**
22      * Unregister all converters.
23      */

24     public static void unregisterAll() {
25         converters.clear();
26     }
27
28     /**
29      * Register default set of converters.
30      */

31     public static void registerDefaults() {
32         register(String JavaDoc.class, new StringConverter());
33         register(String JavaDoc[].class, new StringArrayConverter());
34
35         register(Integer JavaDoc.class, new IntegerConverter());
36         register(int.class, new IntegerConverter());
37         register(MutableInteger.class, new MutableIntegerConverter());
38
39         register(Short JavaDoc.class, new ShortConverter());
40         register(short.class, new ShortConverter());
41         register(MutableShort.class, new MutableShortConverter());
42
43         register(Long JavaDoc.class, new LongConverter());
44         register(long.class, new LongConverter());
45         register(MutableLong.class, new MutableLongConverter());
46
47         register(Byte JavaDoc.class, new ByteConverter());
48         register(byte.class, new ByteConverter());
49         register(MutableByte.class, new MutableByteConverter());
50
51         register(Float JavaDoc.class, new FloatConverter());
52         register(float.class, new FloatConverter());
53         register(MutableFloat.class, new MutableFloatConverter());
54
55         register(Double JavaDoc.class, new DoubleConverter());
56         register(double.class, new DoubleConverter());
57         register(MutableDouble.class, new MutableDoubleConverter());
58
59         register(Boolean JavaDoc.class, new BooleanConverter());
60         register(boolean.class, new BooleanConverter());
61
62         register(Character JavaDoc.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 JavaDoc.class, new BigDecimalConverter());
75         register(java.math.BigInteger JavaDoc.class, new BigIntegerConverter());
76         register(java.sql.Date JavaDoc.class, new SqlDateConverter());
77         register(java.sql.Time JavaDoc.class, new SqlTimeConverter());
78         register(java.sql.Timestamp JavaDoc.class, new SqlTimestampConverter());
79
80         register(jodd.servlet.upload.FileUpload.class, new FileUploadConverter());
81         register(jodd.datetime.JDateTime.class, new JDateTimeConverter());
82     }
83
84     /**
85      * Registers a converter for specified type.
86      * User must register converter for all super-classes as well.
87      *
88      * @param type class that converter is for
89      * @param typeConverter converter for provided class
90      */

91     public static void register(Class JavaDoc type, TypeConverter typeConverter) {
92         converters.put(type, typeConverter);
93     }
94
95
96     public static void unregister(Class JavaDoc type) {
97         converters.remove(type);
98     }
99
100     /**
101      * Retrieves converter for provided type. Only registered types are matched,
102      * therefore subclasses must be also registered.
103      *
104      * @return founded converter or <code>null</code>
105      */

106     public static TypeConverter lookup(Class JavaDoc type) {
107         return (TypeConverter) converters.get(type);
108     }
109
110     // ---------------------------------------------------------------- convert
111

112     /**
113      * Converts object into specified type.
114      */

115     public static Object JavaDoc convert(Object JavaDoc value, Class JavaDoc 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     /**
125      * Converts object using provided converter.
126      */

127     public static Object JavaDoc convert(Object JavaDoc value, TypeConverter conv) {
128         return conv.convert(value);
129     }
130 }
131
Popular Tags