KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > bean > ConvertersManager


1 package jodd.bean;
2
3 import java.util.HashMap;
4
5 import jodd.bean.converters.BigDecimalConverter;
6 import jodd.bean.converters.BigIntegerConverter;
7 import jodd.bean.converters.BooleanConverter;
8 import jodd.bean.converters.ByteConverter;
9 import jodd.bean.converters.CharacterConverter;
10 import jodd.bean.converters.DoubleConverter;
11 import jodd.bean.converters.FloatConverter;
12 import jodd.bean.converters.IntegerConverter;
13 import jodd.bean.converters.LongConverter;
14 import jodd.bean.converters.SqlDateConverter;
15 import jodd.bean.converters.SqlTimeConverter;
16 import jodd.bean.converters.SqlTimestampConverter;
17 import jodd.bean.converters.StringArrayConverter;
18 import jodd.bean.converters.StringConverter;
19 import jodd.bean.converters.UploadedFileConverter;
20
21 /**
22  * Objects converters manager, register default converters and add new ones.
23  * It is used by BeanUtil methods, but it can be used independently.
24  *
25  * @see BeanUtil
26  */

27 public class ConvertersManager {
28
29     // ---------------------------------------------------------------- manager
30

31     private static HashMap convmap = new HashMap();
32
33     static {
34         registerDefaults();
35     }
36
37     /**
38      * Re-initialize all Converters: loads default ones.
39      */

40     public static void registerDefaults() {
41         convmap.clear();
42         register(String.class, new StringConverter());
43         register(String[].class, new StringArrayConverter());
44         register(Integer.class, new IntegerConverter());
45         register(int.class, new IntegerConverter());
46         register(Long.class, new LongConverter());
47         register(long.class, new LongConverter());
48         register(Byte.class, new ByteConverter());
49         register(byte.class, new ByteConverter());
50         register(Boolean.class, new BooleanConverter());
51         register(boolean.class, new BooleanConverter());
52         register(java.math.BigDecimal.class, new BigDecimalConverter());
53         register(java.math.BigInteger.class, new BigIntegerConverter());
54         register(Float.class, new FloatConverter());
55         register(float.class, new FloatConverter());
56         register(Double.class, new DoubleConverter());
57         register(double.class, new DoubleConverter());
58         register(Character.class, new CharacterConverter());
59         register(char.class, new CharacterConverter());
60         register(java.sql.Date.class, new SqlDateConverter());
61         register(java.sql.Time.class, new SqlTimeConverter());
62         register(java.sql.Timestamp.class, new SqlTimestampConverter());
63         register(jodd.servlet.UploadedFile.class, new UploadedFileConverter());
64     }
65
66     /**
67      * Register single converter.
68      *
69      * @param key class that converter is for
70      * @param conv converter object
71      */

72     public static void register(Class key, Converter conv) {
73         convmap.put(key, conv);
74     }
75
76     /**
77      * Retrieves one of the registered converters
78      *
79      * @param key class that converter is searched for
80      *
81      * @return founded converter, or <code>null</code>
82      */

83     public static Converter get(Class key) {
84         return (Converter) convmap.get(key);
85     }
86
87     // ---------------------------------------------------------------- convert
88

89     /**
90      * Convertes one object into another.
91      *
92      * @param value object to convert from
93      * @param c class of object to convert to
94      *
95      * @return converted object
96      * @exception IllegalArgumentException
97      */

98     public static Object convert(Object value, Class c) throws IllegalArgumentException {
99         Converter conv = (Converter) convmap.get(c);
100         if (conv == null) {
101             throw new IllegalArgumentException("No registered converter for " + c.getName());
102         }
103         Object result = null;
104         try {
105             result = conv.convert(value);
106         } catch (IllegalArgumentException iaex) {
107             throw iaex;
108         }
109         return result;
110     }
111
112
113 }
114
Popular Tags