KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > UpdateStrategy


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.core.databinding;
13
14 import java.math.BigDecimal JavaDoc;
15 import java.math.BigInteger JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.databinding.conversion.IConverter;
20 import org.eclipse.core.databinding.conversion.NumberToStringConverter;
21 import org.eclipse.core.databinding.conversion.StringToNumberConverter;
22 import org.eclipse.core.databinding.util.Policy;
23 import org.eclipse.core.internal.databinding.ClassLookupSupport;
24 import org.eclipse.core.internal.databinding.Pair;
25 import org.eclipse.core.internal.databinding.conversion.IdentityConverter;
26 import org.eclipse.core.internal.databinding.conversion.IntegerToStringConverter;
27 import org.eclipse.core.internal.databinding.conversion.NumberToBigDecimalConverter;
28 import org.eclipse.core.internal.databinding.conversion.NumberToBigIntegerConverter;
29 import org.eclipse.core.internal.databinding.conversion.NumberToByteConverter;
30 import org.eclipse.core.internal.databinding.conversion.NumberToDoubleConverter;
31 import org.eclipse.core.internal.databinding.conversion.NumberToFloatConverter;
32 import org.eclipse.core.internal.databinding.conversion.NumberToIntegerConverter;
33 import org.eclipse.core.internal.databinding.conversion.NumberToLongConverter;
34 import org.eclipse.core.internal.databinding.conversion.NumberToShortConverter;
35 import org.eclipse.core.internal.databinding.conversion.ObjectToStringConverter;
36 import org.eclipse.core.internal.databinding.conversion.StringToByteConverter;
37 import org.eclipse.core.internal.databinding.conversion.StringToShortConverter;
38 import org.eclipse.core.runtime.IStatus;
39 import org.eclipse.core.runtime.Status;
40
41 import com.ibm.icu.text.NumberFormat;
42
43 /**
44  * @since 1.0
45  *
46  */

47 /* package */class UpdateStrategy {
48
49     private static final String JavaDoc BOOLEAN_TYPE = "java.lang.Boolean.TYPE"; //$NON-NLS-1$
50
private static final String JavaDoc SHORT_TYPE = "java.lang.Short.TYPE"; //$NON-NLS-1$
51
private static final String JavaDoc BYTE_TYPE = "java.lang.Byte.TYPE"; //$NON-NLS-1$
52
private static final String JavaDoc DOUBLE_TYPE = "java.lang.Double.TYPE"; //$NON-NLS-1$
53
private static final String JavaDoc FLOAT_TYPE = "java.lang.Float.TYPE"; //$NON-NLS-1$
54
private static final String JavaDoc INTEGER_TYPE = "java.lang.Integer.TYPE"; //$NON-NLS-1$
55
private static final String JavaDoc LONG_TYPE = "java.lang.Long.TYPE"; //$NON-NLS-1$
56
private static Map JavaDoc converterMap;
57
58     private static Class JavaDoc autoboxed(Class JavaDoc clazz) {
59         if (clazz == Float.TYPE)
60             return Float JavaDoc.class;
61         else if (clazz == Double.TYPE)
62             return Double JavaDoc.class;
63         else if (clazz == Short.TYPE)
64             return Short JavaDoc.class;
65         else if (clazz == Integer.TYPE)
66             return Integer JavaDoc.class;
67         else if (clazz == Long.TYPE)
68             return Long JavaDoc.class;
69         else if (clazz == Byte.TYPE)
70             return Byte JavaDoc.class;
71         else if (clazz == Boolean.TYPE)
72             return Boolean JavaDoc.class;
73         return clazz;
74     }
75
76     final protected void checkAssignable(Object JavaDoc toType, Object JavaDoc fromType,
77             String JavaDoc errorString) {
78         Boolean JavaDoc assignableFromModelToModelConverter = isAssignableFromTo(
79                 fromType, toType);
80         if (assignableFromModelToModelConverter != null
81                 && !assignableFromModelToModelConverter.booleanValue()) {
82             throw new BindingException(errorString
83                     + " Expected: " + fromType + ", actual: " + toType); //$NON-NLS-1$//$NON-NLS-2$
84
}
85     }
86
87     /**
88      * Tries to create a converter that can convert from values of type
89      * fromType. Returns <code>null</code> if no converter could be created.
90      * Either toType or modelDescription can be <code>null</code>, but not
91      * both.
92      *
93      * @param fromType
94      * @param toType
95      * @return an IConverter, or <code>null</code> if unsuccessful
96      */

97     protected IConverter createConverter(Object JavaDoc fromType, Object JavaDoc toType) {
98         if (!(fromType instanceof Class JavaDoc) || !(toType instanceof Class JavaDoc)) {
99             return new DefaultConverter(fromType, toType);
100         }
101         Class JavaDoc toClass = (Class JavaDoc) toType;
102         Class JavaDoc originalToClass = toClass;
103         if (toClass.isPrimitive()) {
104             toClass = autoboxed(toClass);
105         }
106         Class JavaDoc fromClass = (Class JavaDoc) fromType;
107         Class JavaDoc originalFromClass = fromClass;
108         if (fromClass.isPrimitive()) {
109             fromClass = autoboxed(fromClass);
110         }
111         if (!((Class JavaDoc) toType).isPrimitive()
112                 && toClass.isAssignableFrom(fromClass)) {
113             return new IdentityConverter(originalFromClass, originalToClass);
114         }
115         if (((Class JavaDoc) fromType).isPrimitive() && ((Class JavaDoc) toType).isPrimitive()
116                 && fromType.equals(toType)) {
117             return new IdentityConverter(originalFromClass, originalToClass);
118         }
119         Map JavaDoc converterMap = getConverterMap();
120         Class JavaDoc[] supertypeHierarchyFlattened = ClassLookupSupport
121                 .getTypeHierarchyFlattened(fromClass);
122         for (int i = 0; i < supertypeHierarchyFlattened.length; i++) {
123             Class JavaDoc currentFromClass = supertypeHierarchyFlattened[i];
124             if (currentFromClass == toType) {
125                 // converting to toType is just a widening
126
return new IdentityConverter(fromClass, toClass);
127             }
128             Pair key = new Pair(getKeyForClass(fromType, currentFromClass),
129                     getKeyForClass(toType, toClass));
130             Object JavaDoc converterOrClassname = converterMap.get(key);
131             if (converterOrClassname instanceof IConverter) {
132                 return (IConverter) converterOrClassname;
133             } else if (converterOrClassname instanceof String JavaDoc) {
134                 String JavaDoc classname = (String JavaDoc) converterOrClassname;
135                 Class JavaDoc converterClass;
136                 try {
137                     converterClass = Class.forName(classname);
138                     IConverter result = (IConverter) converterClass
139                             .newInstance();
140                     converterMap.put(key, result);
141                     return result;
142                 } catch (Exception JavaDoc e) {
143                     Policy
144                             .getLog()
145                             .log(
146                                     new Status(
147                                             IStatus.ERROR,
148                                             Policy.JFACE_DATABINDING,
149                                             0,
150                                             "Error while instantiating default converter", e)); //$NON-NLS-1$
151
}
152             }
153         }
154         // Since we found no converter yet, try a "downcast" converter;
155
// the IdentityConverter will automatically check the actual types at
156
// runtime.
157
if (fromClass.isAssignableFrom(toClass)) {
158             return new IdentityConverter(originalFromClass, originalToClass);
159         }
160         return new DefaultConverter(fromType, toType);
161     }
162
163     private static Map JavaDoc getConverterMap() {
164         // using string-based lookup avoids loading of too many classes
165
if (converterMap == null) {
166             //NumberFormat to be shared across converters for the formatting of integer values
167
NumberFormat integerFormat = NumberFormat.getIntegerInstance();
168             //NumberFormat to be shared across converters for formatting non integer values
169
NumberFormat numberFormat = NumberFormat.getNumberInstance();
170             
171             converterMap = new HashMap JavaDoc();
172             converterMap
173                     .put(
174                             new Pair("java.util.Date", "java.lang.String"), "org.eclipse.core.internal.databinding.conversion.DateToStringConverter"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
175
converterMap
176                     .put(
177                             new Pair("java.lang.String", "java.lang.Boolean"), "org.eclipse.core.internal.databinding.conversion.StringToBooleanConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
178
converterMap
179                     .put(
180                             new Pair("java.lang.String", "java.lang.Byte"), StringToByteConverter.toByte(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
181
converterMap
182                     .put(
183                             new Pair("java.lang.String", "java.lang.Character"), "org.eclipse.core.internal.databinding.conversion.StringToCharacterConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
184
converterMap
185                     .put(
186                             new Pair("java.lang.String", "java.util.Date"), "org.eclipse.core.internal.databinding.conversion.StringToDateConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
187
converterMap
188                     .put(
189                             new Pair("java.lang.String", "java.lang.Short"), StringToShortConverter.toShort(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
190

191             converterMap.put(new Pair("java.lang.String", "java.lang.Integer"), StringToNumberConverter.toInteger(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
192
converterMap.put(new Pair("java.lang.String", "java.lang.Double"), StringToNumberConverter.toDouble(numberFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
193
converterMap.put(new Pair("java.lang.String", "java.lang.Long"), StringToNumberConverter.toLong(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
194
converterMap.put(new Pair("java.lang.String", "java.lang.Float"), StringToNumberConverter.toFloat(numberFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
195
converterMap.put(new Pair("java.lang.String", "java.math.BigInteger"), StringToNumberConverter.toBigInteger(integerFormat)); //$NON-NLS-1$//$NON-NLS-2$
196
converterMap.put(new Pair("java.lang.Integer", "java.lang.String"), NumberToStringConverter.fromInteger(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
197
converterMap.put(new Pair("java.lang.Long", "java.lang.String"), NumberToStringConverter.fromLong(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
198
converterMap.put(new Pair("java.lang.Double", "java.lang.String"), NumberToStringConverter.fromDouble(numberFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
199
converterMap.put(new Pair("java.lang.Float", "java.lang.String"), NumberToStringConverter.fromFloat(numberFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
200
converterMap.put(new Pair("java.math.BigInteger", "java.lang.String"), NumberToStringConverter.fromBigInteger(integerFormat)); //$NON-NLS-1$//$NON-NLS-2$
201
converterMap.put(new Pair("java.lang.Byte", "java.lang.String"), IntegerToStringConverter.fromByte(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
202
converterMap.put(new Pair("java.lang.Short", "java.lang.String"), IntegerToStringConverter.fromShort(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
203

204             converterMap
205                     .put(
206                             new Pair("java.lang.Object", "java.lang.String"), "org.eclipse.core.internal.databinding.conversion.ObjectToStringConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
207

208             // Integer.TYPE
209
converterMap.put(new Pair("java.lang.String", INTEGER_TYPE), StringToNumberConverter.toInteger(integerFormat, true)); //$NON-NLS-1$
210
converterMap
211                     .put(
212                             new Pair(INTEGER_TYPE, "java.lang.Integer"), new IdentityConverter(Integer.TYPE, Integer JavaDoc.class)); //$NON-NLS-1$
213
converterMap
214                     .put(
215                             new Pair(INTEGER_TYPE, "java.lang.Object"), new IdentityConverter(Integer.TYPE, Object JavaDoc.class)); //$NON-NLS-1$
216
converterMap.put(new Pair(INTEGER_TYPE, "java.lang.String"), NumberToStringConverter.fromInteger(integerFormat, true)); //$NON-NLS-1$
217

218             // Byte.TYPE
219
converterMap
220                     .put(
221                             new Pair("java.lang.String", BYTE_TYPE), StringToByteConverter.toByte(integerFormat, true)); //$NON-NLS-1$
222
converterMap
223                     .put(
224                             new Pair(BYTE_TYPE, "java.lang.Byte"), new IdentityConverter(Byte.TYPE, Byte JavaDoc.class)); //$NON-NLS-1$
225
converterMap
226                     .put(
227                             new Pair(BYTE_TYPE, "java.lang.String"), IntegerToStringConverter.fromByte(integerFormat, true)); //$NON-NLS-1$
228
converterMap
229                     .put(
230                             new Pair(BYTE_TYPE, "java.lang.Object"), new IdentityConverter(Byte.TYPE, Object JavaDoc.class)); //$NON-NLS-1$
231

232             // Double.TYPE
233
converterMap.put(new Pair("java.lang.String", DOUBLE_TYPE), StringToNumberConverter.toDouble(numberFormat, true)); //$NON-NLS-1$
234
converterMap.put(new Pair(DOUBLE_TYPE, "java.lang.String"), NumberToStringConverter.fromDouble(numberFormat, true)); //$NON-NLS-1$
235

236             converterMap
237                     .put(
238                             new Pair(DOUBLE_TYPE, "java.lang.Double"), new IdentityConverter(Double.TYPE, Double JavaDoc.class)); //$NON-NLS-1$
239
converterMap
240                     .put(
241                             new Pair(DOUBLE_TYPE, "java.lang.Object"), new IdentityConverter(Double.TYPE, Object JavaDoc.class)); //$NON-NLS-1$
242

243             // Boolean.TYPE
244
converterMap
245                     .put(
246                             new Pair("java.lang.String", BOOLEAN_TYPE), "org.eclipse.core.internal.databinding.conversion.StringToBooleanPrimitiveConverter"); //$NON-NLS-1$ //$NON-NLS-2$
247
converterMap
248                     .put(
249                             new Pair(BOOLEAN_TYPE, "java.lang.Boolean"), new IdentityConverter(Boolean.TYPE, Boolean JavaDoc.class)); //$NON-NLS-1$
250
converterMap
251                     .put(
252                             new Pair(BOOLEAN_TYPE, "java.lang.String"), new ObjectToStringConverter(Boolean.TYPE)); //$NON-NLS-1$
253
converterMap
254                     .put(
255                             new Pair(BOOLEAN_TYPE, "java.lang.Object"), new IdentityConverter(Boolean.TYPE, Object JavaDoc.class)); //$NON-NLS-1$
256

257             // Float.TYPE
258
converterMap.put(new Pair("java.lang.String", FLOAT_TYPE), StringToNumberConverter.toFloat(numberFormat, true)); //$NON-NLS-1$
259
converterMap.put(new Pair(FLOAT_TYPE, "java.lang.String"), NumberToStringConverter.fromFloat(numberFormat, true)); //$NON-NLS-1$
260
converterMap
261                     .put(
262                             new Pair(FLOAT_TYPE, "java.lang.Float"), new IdentityConverter(Float.TYPE, Float JavaDoc.class)); //$NON-NLS-1$
263
converterMap
264                     .put(
265                             new Pair(FLOAT_TYPE, "java.lang.Object"), new IdentityConverter(Float.TYPE, Object JavaDoc.class)); //$NON-NLS-1$
266

267             // Short.TYPE
268
converterMap
269                     .put(
270                             new Pair("java.lang.String", SHORT_TYPE), StringToShortConverter.toShort(integerFormat, true)); //$NON-NLS-1$
271
converterMap
272                     .put(
273                             new Pair(SHORT_TYPE, "java.lang.Short"), new IdentityConverter(Short.TYPE, Short JavaDoc.class)); //$NON-NLS-1$
274
converterMap
275                     .put(
276                             new Pair(SHORT_TYPE, "java.lang.String"), IntegerToStringConverter.fromShort(integerFormat, true)); //$NON-NLS-1$
277
converterMap
278                     .put(
279                             new Pair(SHORT_TYPE, "java.lang.Object"), new IdentityConverter(Short.TYPE, Object JavaDoc.class)); //$NON-NLS-1$
280

281             // Long.TYPE
282
converterMap.put(new Pair("java.lang.String", LONG_TYPE), StringToNumberConverter.toLong(integerFormat, true)); //$NON-NLS-1$
283
converterMap.put(new Pair(LONG_TYPE, "java.lang.String"), NumberToStringConverter.fromLong(integerFormat, true)); //$NON-NLS-1$
284
converterMap
285                     .put(
286                             new Pair(LONG_TYPE, "java.lang.Long"), new IdentityConverter(Long.TYPE, Long JavaDoc.class)); //$NON-NLS-1$
287
converterMap
288                     .put(
289                             new Pair(LONG_TYPE, "java.lang.Object"), new IdentityConverter(Long.TYPE, Object JavaDoc.class)); //$NON-NLS-1$
290

291             converterMap
292                     .put(
293                             new Pair("org.eclipse.core.runtime.IStatus", "java.lang.String"), "org.eclipse.core.internal.databinding.conversion.StatusToStringConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
294

295             addNumberToByteConverters(converterMap, integerFormat, integerClasses);
296             addNumberToByteConverters(converterMap, numberFormat, floatClasses);
297             
298             addNumberToShortConverters(converterMap, integerFormat, integerClasses);
299             addNumberToShortConverters(converterMap, numberFormat, floatClasses);
300             
301             addNumberToIntegerConverters(converterMap, integerFormat, integerClasses);
302             addNumberToIntegerConverters(converterMap, numberFormat, floatClasses);
303             
304             addNumberToLongConverters(converterMap, integerFormat, integerClasses);
305             addNumberToLongConverters(converterMap, numberFormat, floatClasses);
306             
307             addNumberToFloatConverters(converterMap, integerFormat, integerClasses);
308             addNumberToFloatConverters(converterMap, numberFormat, floatClasses);
309             
310             addNumberToDoubleConverters(converterMap, integerFormat, integerClasses);
311             addNumberToDoubleConverters(converterMap, numberFormat, floatClasses);
312             
313             addNumberToBigIntegerConverters(converterMap, integerFormat, integerClasses);
314             addNumberToBigIntegerConverters(converterMap, numberFormat, floatClasses);
315             
316             addNumberToBigDecimalConverters(converterMap, integerFormat, integerClasses);
317             addNumberToBigDecimalConverters(converterMap, numberFormat, floatClasses);
318         }
319
320         return converterMap;
321     }
322     
323     private static final Class JavaDoc[] integerClasses = new Class JavaDoc[]{
324         Byte.TYPE, Byte JavaDoc.class,
325         Short.TYPE, Short JavaDoc.class,
326         Integer.TYPE, Integer JavaDoc.class,
327         Long.TYPE, Long JavaDoc.class,
328         BigInteger JavaDoc.class};
329
330     private static final Class JavaDoc[] floatClasses = new Class JavaDoc[] {
331         Float.TYPE, Float JavaDoc.class,
332         Double.TYPE, Double JavaDoc.class,
333         BigDecimal JavaDoc.class};
334     
335     /**
336      * Registers converters to boxed and unboxed types from a list of from classes.
337      *
338      * @param map
339      * @param numberFormat
340      * @param fromTypes
341      */

342     private static void addNumberToByteConverters(Map JavaDoc map,
343             NumberFormat numberFormat, Class JavaDoc[] fromTypes) {
344         
345         for (int i = 0; i < fromTypes.length; i++) {
346             Class JavaDoc fromType = fromTypes[i];
347             if (!fromType.equals(Byte JavaDoc.class) && !fromType.equals(Byte.TYPE)) {
348                 String JavaDoc fromName = (fromType.isPrimitive()) ? getKeyForClass(fromType, null) : fromType.getName();
349                 
350                 map.put(new Pair(fromName, BYTE_TYPE), new NumberToByteConverter(numberFormat, fromType, true));
351                 map.put(new Pair(fromName, Byte JavaDoc.class.getName()), new NumberToByteConverter(numberFormat, fromType, false));
352             }
353         }
354     }
355     
356     /**
357      * Registers converters to boxed and unboxed types from a list of from classes.
358      *
359      * @param map
360      * @param numberFormat
361      * @param fromTypes
362      */

363     private static void addNumberToShortConverters(Map JavaDoc map, NumberFormat numberFormat, Class JavaDoc[] fromTypes) {
364         for (int i = 0; i < fromTypes.length; i++) {
365             Class JavaDoc fromType = fromTypes[i];
366             if (!fromType.equals(Short JavaDoc.class) && !fromType.equals(Short.TYPE)) {
367                 String JavaDoc fromName = (fromType.isPrimitive()) ? getKeyForClass(fromType, null) : fromType.getName();
368                 
369                 map.put(new Pair(fromName, SHORT_TYPE), new NumberToShortConverter(numberFormat, fromType, true));
370                 map.put(new Pair(fromName, Short JavaDoc.class.getName()), new NumberToShortConverter(numberFormat, fromType, false));
371             }
372         }
373     }
374     
375     /**
376      * Registers converters to boxed and unboxed types from a list of from classes.
377      *
378      * @param map
379      * @param numberFormat
380      * @param fromTypes
381      */

382     private static void addNumberToIntegerConverters(Map JavaDoc map, NumberFormat numberFormat, Class JavaDoc[] fromTypes) {
383         for (int i = 0; i < fromTypes.length; i++) {
384             Class JavaDoc fromType = fromTypes[i];
385             if (!fromType.equals(Integer JavaDoc.class) && !fromType.equals(Integer.TYPE)) {
386                 String JavaDoc fromName = (fromType.isPrimitive()) ? getKeyForClass(fromType, null) : fromType.getName();
387                 
388                 map.put(new Pair(fromName, INTEGER_TYPE), new NumberToIntegerConverter(numberFormat, fromType, true));
389                 map.put(new Pair(fromName, Integer JavaDoc.class.getName()), new NumberToIntegerConverter(numberFormat, fromType, false));
390             }
391         }
392     }
393     
394     /**
395      * Registers converters to boxed and unboxed types from a list of from classes.
396      *
397      * @param map
398      * @param numberFormat
399      * @param fromTypes
400      */

401     private static void addNumberToLongConverters(Map JavaDoc map, NumberFormat numberFormat, Class JavaDoc[] fromTypes) {
402         for (int i = 0; i < fromTypes.length; i++) {
403             Class JavaDoc fromType = fromTypes[i];
404             if (!fromType.equals(Long JavaDoc.class) && !fromType.equals(Long.TYPE)) {
405                 String JavaDoc fromName = (fromType.isPrimitive()) ? getKeyForClass(fromType, null) : fromType.getName();
406                 
407                 map.put(new Pair(fromName, LONG_TYPE), new NumberToLongConverter(numberFormat, fromType, true));
408                 map.put(new Pair(fromName, Long JavaDoc.class.getName()), new NumberToLongConverter(numberFormat, fromType, false));
409             }
410         }
411     }
412
413     /**
414      * Registers converters to boxed and unboxed types from a list of from classes.
415      *
416      * @param map
417      * @param numberFormat
418      * @param fromTypes
419      */

420     private static void addNumberToFloatConverters(Map JavaDoc map, NumberFormat numberFormat, Class JavaDoc[] fromTypes) {
421         for (int i = 0; i < fromTypes.length; i++) {
422             Class JavaDoc fromType = fromTypes[i];
423             if (!fromType.equals(Float JavaDoc.class) && !fromType.equals(Float.TYPE)) {
424                 String JavaDoc fromName = (fromType.isPrimitive()) ? getKeyForClass(fromType, null) : fromType.getName();
425                 
426                 map.put(new Pair(fromName, FLOAT_TYPE), new NumberToFloatConverter(numberFormat, fromType, true));
427                 map.put(new Pair(fromName, Float JavaDoc.class.getName()), new NumberToFloatConverter(numberFormat, fromType, false));
428             }
429         }
430     }
431     
432     /**
433      * Registers converters to boxed and unboxed types from a list of from classes.
434      *
435      * @param map
436      * @param numberFormat
437      * @param fromTypes
438      */

439     private static void addNumberToDoubleConverters(Map JavaDoc map, NumberFormat numberFormat, Class JavaDoc[] fromTypes) {
440         for (int i = 0; i < fromTypes.length; i++) {
441             Class JavaDoc fromType = fromTypes[i];
442             if (!fromType.equals(Double JavaDoc.class) && !fromType.equals(Double.TYPE)) {
443                 String JavaDoc fromName = (fromType.isPrimitive()) ? getKeyForClass(fromType, null) : fromType.getName();
444                 
445                 map.put(new Pair(fromName, DOUBLE_TYPE), new NumberToDoubleConverter(numberFormat, fromType, true));
446                 map.put(new Pair(fromName, Double JavaDoc.class.getName()), new NumberToDoubleConverter(numberFormat, fromType, false));
447             }
448         }
449     }
450
451     /**
452      * Registers converters to boxed and unboxed types from a list of from classes.
453      *
454      * @param map
455      * @param numberFormat
456      * @param fromTypes
457      */

458     private static void addNumberToBigIntegerConverters(Map JavaDoc map, NumberFormat numberFormat, Class JavaDoc[] fromTypes) {
459         for (int i = 0; i < fromTypes.length; i++) {
460             Class JavaDoc fromType = fromTypes[i];
461             if (!fromType.equals(BigInteger JavaDoc.class)) {
462                 String JavaDoc fromName = (fromType.isPrimitive()) ? getKeyForClass(fromType, null) : fromType.getName();
463                 
464                 map.put(new Pair(fromName, BigInteger JavaDoc.class.getName()), new NumberToBigIntegerConverter(numberFormat, fromType));
465             }
466         }
467     }
468     
469     /**
470      * Registers converters to boxed and unboxed types from a list of from classes.
471      *
472      * @param map
473      * @param numberFormat
474      * @param fromTypes
475      */

476     private static void addNumberToBigDecimalConverters(Map JavaDoc map, NumberFormat numberFormat, Class JavaDoc[] fromTypes) {
477         for (int i = 0; i < fromTypes.length; i++) {
478             Class JavaDoc fromType = fromTypes[i];
479             if (!fromType.equals(BigDecimal JavaDoc.class)) {
480                 String JavaDoc fromName = (fromType.isPrimitive()) ? getKeyForClass(fromType, null) : fromType.getName();
481                 
482                 map.put(new Pair(fromName, BigDecimal JavaDoc.class.getName()), new NumberToBigDecimalConverter(numberFormat, fromType));
483             }
484         }
485     }
486     
487     private static String JavaDoc getKeyForClass(Object JavaDoc originalValue, Class JavaDoc filteredValue) {
488         if (originalValue instanceof Class JavaDoc) {
489             Class JavaDoc originalClass = (Class JavaDoc) originalValue;
490             if (originalClass.equals(Integer.TYPE)) {
491                 return INTEGER_TYPE;
492             } else if (originalClass.equals(Byte.TYPE)) {
493                 return BYTE_TYPE;
494             } else if (originalClass.equals(Boolean.TYPE)) {
495                 return BOOLEAN_TYPE;
496             } else if (originalClass.equals(Double.TYPE)) {
497                 return DOUBLE_TYPE;
498             } else if (originalClass.equals(Float.TYPE)) {
499                 return FLOAT_TYPE;
500             } else if (originalClass.equals(Long.TYPE)) {
501                 return LONG_TYPE;
502             } else if (originalClass.equals(Short.TYPE)) {
503                 return SHORT_TYPE;
504             }
505         }
506         return filteredValue.getName();
507     }
508
509     /**
510      * @param fromType
511      * @param toType
512      * @return whether fromType is assignable to toType
513      */

514     protected Boolean JavaDoc isAssignableFromTo(Object JavaDoc fromType, Object JavaDoc toType) {
515         if (fromType instanceof Class JavaDoc && toType instanceof Class JavaDoc) {
516             Class JavaDoc toClass = (Class JavaDoc) toType;
517             if (toClass.isPrimitive()) {
518                 toClass = autoboxed(toClass);
519             }
520             Class JavaDoc fromClass = (Class JavaDoc) fromType;
521             if (fromClass.isPrimitive()) {
522                 fromClass = autoboxed(fromClass);
523             }
524             return toClass.isAssignableFrom(fromClass) ? Boolean.TRUE
525                     : Boolean.FALSE;
526         }
527         return null;
528     }
529
530     /*
531      * Default converter implementation, does not perform any conversion.
532      */

533     protected static final class DefaultConverter implements IConverter {
534
535         private final Object JavaDoc toType;
536
537         private final Object JavaDoc fromType;
538
539         /**
540          * @param fromType
541          * @param toType
542          */

543         DefaultConverter(Object JavaDoc fromType, Object JavaDoc toType) {
544             this.toType = toType;
545             this.fromType = fromType;
546         }
547
548         public Object JavaDoc convert(Object JavaDoc fromObject) {
549             return fromObject;
550         }
551
552         public Object JavaDoc getFromType() {
553             return fromType;
554         }
555
556         public Object JavaDoc getToType() {
557             return toType;
558         }
559     }
560
561 }
562
Popular Tags