KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > ConverterFactory


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.reflect;
21
22 import java.math.BigDecimal JavaDoc;
23 import java.math.BigInteger JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.cayenne.util.Util;
28
29 /**
30  * A factory of property type converters.
31  *
32  * @since 1.2
33  * @author Andrus Adamchik
34  */

35 class ConverterFactory {
36
37     private static final String JavaDoc FACTORY_CLASS_JDK15 = "org.apache.cayenne.reflect.ConverterFactory15";
38
39     static final ConverterFactory factory = createFactory();
40     static Map JavaDoc converters;
41
42     static final Converter noopConverter = new Converter() {
43
44         Object JavaDoc convert(Object JavaDoc object, Class JavaDoc type) {
45             return object;
46         }
47     };
48
49     static {
50
51         Converter stringConverter = new Converter() {
52
53             Object JavaDoc convert(Object JavaDoc object, Class JavaDoc type) {
54                 return object != null ? object.toString() : null;
55             }
56         };
57
58         Converter booleanConverter = new Converter() {
59
60             Object JavaDoc convert(Object JavaDoc object, Class JavaDoc type) {
61                 if (object == null) {
62                     return type.isPrimitive() ? Boolean.FALSE : null;
63                 }
64
65                 if (object instanceof Boolean JavaDoc) {
66                     return object;
67                 }
68
69                 return "true".equalsIgnoreCase(object.toString())
70                         ? Boolean.TRUE
71                         : Boolean.FALSE;
72             }
73         };
74
75         Converter intConverter = new Converter() {
76
77             Object JavaDoc convert(Object JavaDoc object, Class JavaDoc type) {
78                 if (object == null) {
79                     return type.isPrimitive() ? new Integer JavaDoc(0) : null;
80                 }
81
82                 if (object instanceof Integer JavaDoc) {
83                     return object;
84                 }
85
86                 return new Integer JavaDoc(object.toString());
87             }
88         };
89
90         Converter byteConverter = new Converter() {
91
92             Object JavaDoc convert(Object JavaDoc object, Class JavaDoc type) {
93                 if (object == null) {
94                     return type.isPrimitive() ? new Byte JavaDoc((byte) 0) : null;
95                 }
96
97                 if (object instanceof Byte JavaDoc) {
98                     return object;
99                 }
100
101                 return new Byte JavaDoc(object.toString());
102             }
103         };
104
105         Converter shortConverter = new Converter() {
106
107             Object JavaDoc convert(Object JavaDoc object, Class JavaDoc type) {
108                 if (object == null) {
109                     return type.isPrimitive() ? new Short JavaDoc((short) 0) : null;
110                 }
111
112                 if (object instanceof Short JavaDoc) {
113                     return object;
114                 }
115
116                 return new Short JavaDoc(object.toString());
117             }
118         };
119
120         Converter charConverter = new Converter() {
121
122             Object JavaDoc convert(Object JavaDoc object, Class JavaDoc type) {
123                 if (object == null) {
124                     return type.isPrimitive() ? new Character JavaDoc((char) 0) : null;
125                 }
126
127                 if (object instanceof Character JavaDoc) {
128                     return object;
129                 }
130
131                 String JavaDoc string = object.toString();
132                 return new Character JavaDoc(string.length() > 0 ? string.charAt(0) : 0);
133             }
134         };
135
136         Converter doubleConverter = new Converter() {
137
138             Object JavaDoc convert(Object JavaDoc object, Class JavaDoc type) {
139                 if (object == null) {
140                     return type.isPrimitive() ? new Double JavaDoc(0d) : null;
141                 }
142
143                 if (object instanceof Double JavaDoc) {
144                     return object;
145                 }
146
147                 return new Double JavaDoc(object.toString());
148             }
149         };
150
151         Converter floatConverter = new Converter() {
152
153             Object JavaDoc convert(Object JavaDoc object, Class JavaDoc type) {
154                 if (object == null) {
155                     return type.isPrimitive() ? new Float JavaDoc(0f) : null;
156                 }
157
158                 if (object instanceof Float JavaDoc) {
159                     return object;
160                 }
161
162                 return new Float JavaDoc(object.toString());
163             }
164         };
165
166         Converter bigDecimalConverter = new Converter() {
167
168             Object JavaDoc convert(Object JavaDoc object, Class JavaDoc type) {
169                 if (object == null) {
170                     return null;
171                 }
172
173                 if (object instanceof BigDecimal JavaDoc) {
174                     return object;
175                 }
176
177                 return new BigDecimal JavaDoc(object.toString());
178             }
179         };
180
181         Converter bigIntegerConverter = new Converter() {
182
183             Object JavaDoc convert(Object JavaDoc object, Class JavaDoc type) {
184                 if (object == null) {
185                     return null;
186                 }
187
188                 if (object instanceof BigInteger JavaDoc) {
189                     return object;
190                 }
191
192                 return new BigInteger JavaDoc(object.toString());
193             }
194         };
195
196         // TODO: byte[] converter...
197

198         converters = new HashMap JavaDoc();
199
200         converters.put(Boolean JavaDoc.class.getName(), booleanConverter);
201         converters.put("boolean", booleanConverter);
202
203         converters.put(Short JavaDoc.class.getName(), shortConverter);
204         converters.put("short", shortConverter);
205
206         converters.put(Byte JavaDoc.class.getName(), byteConverter);
207         converters.put("byte", byteConverter);
208
209         converters.put(Integer JavaDoc.class.getName(), intConverter);
210         converters.put("int", intConverter);
211
212         converters.put(Double JavaDoc.class.getName(), doubleConverter);
213         converters.put("double", doubleConverter);
214
215         converters.put(Float JavaDoc.class.getName(), floatConverter);
216         converters.put("float", floatConverter);
217
218         converters.put(Character JavaDoc.class.getName(), charConverter);
219         converters.put("char", charConverter);
220
221         converters.put(BigDecimal JavaDoc.class.getName(), bigDecimalConverter);
222         converters.put(BigInteger JavaDoc.class.getName(), bigIntegerConverter);
223         converters.put(Number JavaDoc.class.getName(), bigDecimalConverter);
224         converters.put(String JavaDoc.class.getName(), stringConverter);
225     }
226
227     static ConverterFactory createFactory() {
228         try {
229             // sniff JDK 1.5
230
Class.forName("java.lang.StringBuilder");
231
232             Class JavaDoc factoryClass = Util.getJavaClass(FACTORY_CLASS_JDK15);
233             return (ConverterFactory) factoryClass.newInstance();
234         }
235         catch (Throwable JavaDoc th) {
236             // .. jdk 1.4
237
return new ConverterFactory();
238         }
239     }
240
241     Converter getConverter(Class JavaDoc type) {
242         if (type == null) {
243             throw new IllegalArgumentException JavaDoc("Null type");
244         }
245
246         Converter c = (Converter) converters.get(type.getName());
247         return c != null ? c : noopConverter;
248     }
249 }
250
Popular Tags