78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2007, 2008 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 * Matt Carter - Bug 180392
|
|
11 * - Character support completed (bug 197679)
|
|
12 *******************************************************************************/
|
|
13
|
|
14 module org.eclipse.core.databinding.UpdateStrategy;
|
81
|
15 import org.eclipse.core.databinding.BindingException;
|
78
|
16
|
|
17 import java.lang.all;
|
|
18
|
|
19 import java.math.BigDecimal;
|
|
20 import java.math.BigInteger;
|
|
21 import java.util.HashMap;
|
|
22 import java.util.Map;
|
|
23
|
|
24 import org.eclipse.core.databinding.conversion.IConverter;
|
|
25 import org.eclipse.core.databinding.conversion.NumberToStringConverter;
|
|
26 import org.eclipse.core.databinding.conversion.StringToNumberConverter;
|
|
27 import org.eclipse.core.databinding.util.Policy;
|
|
28 import org.eclipse.core.internal.databinding.ClassLookupSupport;
|
|
29 import org.eclipse.core.internal.databinding.Pair;
|
|
30 import org.eclipse.core.internal.databinding.conversion.CharacterToStringConverter;
|
|
31 import org.eclipse.core.internal.databinding.conversion.IdentityConverter;
|
|
32 import org.eclipse.core.internal.databinding.conversion.IntegerToStringConverter;
|
|
33 import org.eclipse.core.internal.databinding.conversion.NumberToBigDecimalConverter;
|
|
34 import org.eclipse.core.internal.databinding.conversion.NumberToBigIntegerConverter;
|
|
35 import org.eclipse.core.internal.databinding.conversion.NumberToByteConverter;
|
|
36 import org.eclipse.core.internal.databinding.conversion.NumberToDoubleConverter;
|
|
37 import org.eclipse.core.internal.databinding.conversion.NumberToFloatConverter;
|
|
38 import org.eclipse.core.internal.databinding.conversion.NumberToIntegerConverter;
|
|
39 import org.eclipse.core.internal.databinding.conversion.NumberToLongConverter;
|
|
40 import org.eclipse.core.internal.databinding.conversion.NumberToShortConverter;
|
|
41 import org.eclipse.core.internal.databinding.conversion.ObjectToStringConverter;
|
|
42 import org.eclipse.core.internal.databinding.conversion.StringToByteConverter;
|
|
43 import org.eclipse.core.internal.databinding.conversion.StringToCharacterConverter;
|
|
44 import org.eclipse.core.internal.databinding.conversion.StringToShortConverter;
|
|
45 import org.eclipse.core.runtime.IStatus;
|
|
46 import org.eclipse.core.runtime.Status;
|
|
47
|
|
48 import com.ibm.icu.text.NumberFormat;
|
|
49
|
|
50 /**
|
|
51 * @since 1.0
|
|
52 *
|
|
53 */
|
|
54 /* package */class UpdateStrategy {
|
|
55
|
|
56 private static final String BOOLEAN_TYPE = "java.lang.Boolean.TYPE"; //$NON-NLS-1$
|
|
57
|
|
58 private static final String SHORT_TYPE = "java.lang.Short.TYPE"; //$NON-NLS-1$
|
|
59
|
|
60 private static final String BYTE_TYPE = "java.lang.Byte.TYPE"; //$NON-NLS-1$
|
|
61
|
|
62 private static final String DOUBLE_TYPE = "java.lang.Double.TYPE"; //$NON-NLS-1$
|
|
63
|
|
64 private static final String FLOAT_TYPE = "java.lang.Float.TYPE"; //$NON-NLS-1$
|
|
65
|
|
66 private static final String INTEGER_TYPE = "java.lang.Integer.TYPE"; //$NON-NLS-1$
|
|
67
|
|
68 private static final String LONG_TYPE = "java.lang.Long.TYPE"; //$NON-NLS-1$
|
|
69
|
|
70 private static final String CHARACTER_TYPE = "java.lang.Character.TYPE"; //$NON-NLS-1$
|
|
71
|
|
72 private static Map converterMap;
|
|
73
|
|
74 private static ClassInfo autoboxed(ClassInfo clazz) {
|
|
75 if (clazz is Float.TYPE)
|
|
76 return Float.classinfo;
|
|
77 else if (clazz is Double.TYPE)
|
|
78 return Double.classinfo;
|
|
79 else if (clazz is Short.TYPE)
|
|
80 return Short.classinfo;
|
|
81 else if (clazz is Integer.TYPE)
|
|
82 return Integer.classinfo;
|
|
83 else if (clazz is Long.TYPE)
|
|
84 return Long.classinfo;
|
|
85 else if (clazz is Byte.TYPE)
|
|
86 return Byte.classinfo;
|
|
87 else if (clazz is Boolean.TYPE)
|
|
88 return Boolean.classinfo;
|
|
89 else if (clazz is Character.TYPE)
|
|
90 return Character.classinfo;
|
|
91 return clazz;
|
|
92 }
|
|
93
|
|
94 final protected void checkAssignable(Object toType, Object fromType,
|
|
95 String errorString) {
|
|
96 Boolean assignableFromModelToModelConverter = isAssignableFromTo(
|
|
97 fromType, toType);
|
|
98 if (assignableFromModelToModelConverter !is null
|
|
99 && !assignableFromModelToModelConverter.booleanValue()) {
|
|
100 throw new BindingException(errorString
|
|
101 + " Expected: " + fromType + ", actual: " + toType); //$NON-NLS-1$//$NON-NLS-2$
|
|
102 }
|
|
103 }
|
|
104
|
|
105 /**
|
|
106 * Tries to create a converter that can convert from values of type
|
|
107 * fromType. Returns <code>null</code> if no converter could be created.
|
|
108 * Either toType or modelDescription can be <code>null</code>, but not
|
|
109 * both.
|
|
110 *
|
|
111 * @param fromType
|
|
112 * @param toType
|
|
113 * @return an IConverter, or <code>null</code> if unsuccessful
|
|
114 */
|
|
115 protected IConverter createConverter(Object fromType, Object toType) {
|
|
116 if (!( null !is cast(ClassInfo)fromType ) || !( null !is cast(ClassInfo)toType )) {
|
|
117 return new DefaultConverter(fromType, toType);
|
|
118 }
|
|
119 ClassInfo toClass = cast(ClassInfo) toType;
|
|
120 ClassInfo originalToClass = toClass;
|
|
121 if (toClass.isPrimitive()) {
|
|
122 toClass = autoboxed(toClass);
|
|
123 }
|
|
124 ClassInfo fromClass = cast(ClassInfo) fromType;
|
|
125 ClassInfo originalFromClass = fromClass;
|
|
126 if (fromClass.isPrimitive()) {
|
|
127 fromClass = autoboxed(fromClass);
|
|
128 }
|
|
129 if (!(cast(ClassInfo) toType).isPrimitive()
|
|
130 && toClass.isAssignableFrom(fromClass)) {
|
|
131 return new IdentityConverter(originalFromClass, originalToClass);
|
|
132 }
|
|
133 if ((cast(ClassInfo) fromType).isPrimitive() && (cast(ClassInfo) toType).isPrimitive()
|
|
134 && fromType.equals(toType)) {
|
|
135 return new IdentityConverter(originalFromClass, originalToClass);
|
|
136 }
|
|
137 Map converterMap = getConverterMap();
|
|
138 ClassInfo[] supertypeHierarchyFlattened = ClassLookupSupport
|
|
139 .getTypeHierarchyFlattened(fromClass);
|
|
140 for (int i = 0; i < supertypeHierarchyFlattened.length; i++) {
|
|
141 ClassInfo currentFromClass = supertypeHierarchyFlattened[i];
|
|
142 if (currentFromClass is toType) {
|
|
143 // converting to toType is just a widening
|
|
144 return new IdentityConverter(fromClass, toClass);
|
|
145 }
|
|
146 Pair key = new Pair(getKeyForClass(fromType, currentFromClass),
|
|
147 getKeyForClass(toType, toClass));
|
|
148 Object converterOrClassname = converterMap.get(key);
|
|
149 if ( null !is cast(IConverter)converterOrClassname ) {
|
|
150 return cast(IConverter) converterOrClassname;
|
|
151 } else if ( null !is cast(String)converterOrClassname ) {
|
|
152 String classname = cast(String) converterOrClassname;
|
|
153 ClassInfo converterClass;
|
|
154 try {
|
|
155 converterClass = ClassInfo.forName(classname);
|
|
156 IConverter result = cast(IConverter) converterClass
|
|
157 .newInstance();
|
|
158 converterMap.put(key, result);
|
|
159 return result;
|
|
160 } catch (Exception e) {
|
|
161 Policy
|
|
162 .getLog()
|
|
163 .log(
|
|
164 new Status(
|
|
165 IStatus.ERROR,
|
|
166 Policy.JFACE_DATABINDING,
|
|
167 0,
|
|
168 "Error while instantiating default converter", e)); //$NON-NLS-1$
|
|
169 }
|
|
170 }
|
|
171 }
|
|
172 // Since we found no converter yet, try a "downcast" converter;
|
|
173 // the IdentityConverter will automatically check the actual types at
|
|
174 // runtime.
|
|
175 if (fromClass.isAssignableFrom(toClass)) {
|
|
176 return new IdentityConverter(originalFromClass, originalToClass);
|
|
177 }
|
|
178 return new DefaultConverter(fromType, toType);
|
|
179 }
|
|
180
|
|
181 private synchronized static Map getConverterMap() {
|
|
182 // using string-based lookup avoids loading of too many classes
|
|
183 if (converterMap is null) {
|
|
184 // NumberFormat to be shared across converters for the formatting of
|
|
185 // integer values
|
|
186 NumberFormat integerFormat = NumberFormat.getIntegerInstance();
|
|
187 // NumberFormat to be shared across converters for formatting non
|
|
188 // integer values
|
|
189 NumberFormat numberFormat = NumberFormat.getNumberInstance();
|
|
190
|
|
191 converterMap = new HashMap();
|
|
192 // Standard and Boxed Types
|
|
193 converterMap
|
|
194 .put(
|
|
195 new Pair("java.util.Date", "java.lang.String"), "org.eclipse.core.internal.databinding.conversion.DateToStringConverter"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
196 converterMap
|
|
197 .put(
|
|
198 new Pair("java.lang.String", "java.lang.Boolean"), "org.eclipse.core.internal.databinding.conversion.StringToBooleanConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
|
199 converterMap
|
|
200 .put(
|
|
201 new Pair("java.lang.String", "java.lang.Byte"), StringToByteConverter.toByte(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
202 converterMap
|
|
203 .put(
|
|
204 new Pair("java.lang.String", "java.util.Date"), "org.eclipse.core.internal.databinding.conversion.StringToDateConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
|
205 converterMap
|
|
206 .put(
|
|
207 new Pair("java.lang.String", "java.lang.Short"), StringToShortConverter.toShort(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
208 converterMap
|
|
209 .put(
|
|
210 new Pair("java.lang.String", "java.lang.Character"), StringToCharacterConverter.toCharacter(false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
211 converterMap
|
|
212 .put(
|
|
213 new Pair("java.lang.String", "java.lang.Integer"), StringToNumberConverter.toInteger(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
214 converterMap
|
|
215 .put(
|
|
216 new Pair("java.lang.String", "java.lang.Double"), StringToNumberConverter.toDouble(numberFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
217 converterMap
|
|
218 .put(
|
|
219 new Pair("java.lang.String", "java.lang.Long"), StringToNumberConverter.toLong(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
220 converterMap
|
|
221 .put(
|
|
222 new Pair("java.lang.String", "java.lang.Float"), StringToNumberConverter.toFloat(numberFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
223 converterMap
|
|
224 .put(
|
|
225 new Pair("java.lang.String", "java.math.BigInteger"), StringToNumberConverter.toBigInteger(integerFormat)); //$NON-NLS-1$//$NON-NLS-2$
|
|
226 converterMap
|
|
227 .put(
|
|
228 new Pair("java.lang.Integer", "java.lang.String"), NumberToStringConverter.fromInteger(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
229 converterMap
|
|
230 .put(
|
|
231 new Pair("java.lang.Long", "java.lang.String"), NumberToStringConverter.fromLong(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
232 converterMap
|
|
233 .put(
|
|
234 new Pair("java.lang.Double", "java.lang.String"), NumberToStringConverter.fromDouble(numberFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
235 converterMap
|
|
236 .put(
|
|
237 new Pair("java.lang.Float", "java.lang.String"), NumberToStringConverter.fromFloat(numberFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
238 converterMap
|
|
239 .put(
|
|
240 new Pair("java.math.BigInteger", "java.lang.String"), NumberToStringConverter.fromBigInteger(integerFormat)); //$NON-NLS-1$//$NON-NLS-2$
|
|
241 converterMap
|
|
242 .put(
|
|
243 new Pair("java.lang.Byte", "java.lang.String"), IntegerToStringConverter.fromByte(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
244 converterMap
|
|
245 .put(
|
|
246 new Pair("java.lang.Short", "java.lang.String"), IntegerToStringConverter.fromShort(integerFormat, false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
247 converterMap
|
|
248 .put(
|
|
249 new Pair("java.lang.Character", "java.lang.String"), CharacterToStringConverter.fromCharacter(false)); //$NON-NLS-1$//$NON-NLS-2$
|
|
250
|
|
251 converterMap
|
|
252 .put(
|
|
253 new Pair("java.lang.Object", "java.lang.String"), "org.eclipse.core.internal.databinding.conversion.ObjectToStringConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
|
254
|
|
255 // Integer.TYPE
|
|
256 converterMap
|
|
257 .put(
|
|
258 new Pair("java.lang.String", INTEGER_TYPE), StringToNumberConverter.toInteger(integerFormat, true)); //$NON-NLS-1$
|
|
259 converterMap
|
|
260 .put(
|
|
261 new Pair(INTEGER_TYPE, "java.lang.Integer"), new IdentityConverter(Integer.TYPE, Integer.classinfo)); //$NON-NLS-1$
|
|
262 converterMap
|
|
263 .put(
|
|
264 new Pair(INTEGER_TYPE, "java.lang.Object"), new IdentityConverter(Integer.TYPE, Object.classinfo)); //$NON-NLS-1$
|
|
265 converterMap
|
|
266 .put(
|
|
267 new Pair(INTEGER_TYPE, "java.lang.String"), NumberToStringConverter.fromInteger(integerFormat, true)); //$NON-NLS-1$
|
|
268
|
|
269 // Byte.TYPE
|
|
270 converterMap
|
|
271 .put(
|
|
272 new Pair("java.lang.String", BYTE_TYPE), StringToByteConverter.toByte(integerFormat, true)); //$NON-NLS-1$
|
|
273 converterMap
|
|
274 .put(
|
|
275 new Pair(BYTE_TYPE, "java.lang.Byte"), new IdentityConverter(Byte.TYPE, Byte.classinfo)); //$NON-NLS-1$
|
|
276 converterMap
|
|
277 .put(
|
|
278 new Pair(BYTE_TYPE, "java.lang.String"), IntegerToStringConverter.fromByte(integerFormat, true)); //$NON-NLS-1$
|
|
279 converterMap
|
|
280 .put(
|
|
281 new Pair(BYTE_TYPE, "java.lang.Object"), new IdentityConverter(Byte.TYPE, Object.classinfo)); //$NON-NLS-1$
|
|
282
|
|
283 // Double.TYPE
|
|
284 converterMap
|
|
285 .put(
|
|
286 new Pair("java.lang.String", DOUBLE_TYPE), StringToNumberConverter.toDouble(numberFormat, true)); //$NON-NLS-1$
|
|
287 converterMap
|
|
288 .put(
|
|
289 new Pair(DOUBLE_TYPE, "java.lang.String"), NumberToStringConverter.fromDouble(numberFormat, true)); //$NON-NLS-1$
|
|
290
|
|
291 converterMap
|
|
292 .put(
|
|
293 new Pair(DOUBLE_TYPE, "java.lang.Double"), new IdentityConverter(Double.TYPE, Double.classinfo)); //$NON-NLS-1$
|
|
294 converterMap
|
|
295 .put(
|
|
296 new Pair(DOUBLE_TYPE, "java.lang.Object"), new IdentityConverter(Double.TYPE, Object.classinfo)); //$NON-NLS-1$
|
|
297
|
|
298 // Boolean.TYPE
|
|
299 converterMap
|
|
300 .put(
|
|
301 new Pair("java.lang.String", BOOLEAN_TYPE), "org.eclipse.core.internal.databinding.conversion.StringToBooleanPrimitiveConverter"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
302 converterMap
|
|
303 .put(
|
|
304 new Pair(BOOLEAN_TYPE, "java.lang.Boolean"), new IdentityConverter(Boolean.TYPE, Boolean.classinfo)); //$NON-NLS-1$
|
|
305 converterMap
|
|
306 .put(
|
|
307 new Pair(BOOLEAN_TYPE, "java.lang.String"), new ObjectToStringConvertercast(Boolean.TYPE)); //$NON-NLS-1$
|
|
308 converterMap
|
|
309 .put(
|
|
310 new Pair(BOOLEAN_TYPE, "java.lang.Object"), new IdentityConverter(Boolean.TYPE, Object.classinfo)); //$NON-NLS-1$
|
|
311
|
|
312 // Float.TYPE
|
|
313 converterMap
|
|
314 .put(
|
|
315 new Pair("java.lang.String", FLOAT_TYPE), StringToNumberConverter.toFloat(numberFormat, true)); //$NON-NLS-1$
|
|
316 converterMap
|
|
317 .put(
|
|
318 new Pair(FLOAT_TYPE, "java.lang.String"), NumberToStringConverter.fromFloat(numberFormat, true)); //$NON-NLS-1$
|
|
319 converterMap
|
|
320 .put(
|
|
321 new Pair(FLOAT_TYPE, "java.lang.Float"), new IdentityConverter(Float.TYPE, Float.classinfo)); //$NON-NLS-1$
|
|
322 converterMap
|
|
323 .put(
|
|
324 new Pair(FLOAT_TYPE, "java.lang.Object"), new IdentityConverter(Float.TYPE, Object.classinfo)); //$NON-NLS-1$
|
|
325
|
|
326 // Short.TYPE
|
|
327 converterMap
|
|
328 .put(
|
|
329 new Pair("java.lang.String", SHORT_TYPE), StringToShortConverter.toShort(integerFormat, true)); //$NON-NLS-1$
|
|
330 converterMap
|
|
331 .put(
|
|
332 new Pair(SHORT_TYPE, "java.lang.Short"), new IdentityConverter(Short.TYPE, Short.classinfo)); //$NON-NLS-1$
|
|
333 converterMap
|
|
334 .put(
|
|
335 new Pair(SHORT_TYPE, "java.lang.String"), IntegerToStringConverter.fromShort(integerFormat, true)); //$NON-NLS-1$
|
|
336 converterMap
|
|
337 .put(
|
|
338 new Pair(SHORT_TYPE, "java.lang.Object"), new IdentityConverter(Short.TYPE, Object.classinfo)); //$NON-NLS-1$
|
|
339
|
|
340 // Long.TYPE
|
|
341 converterMap
|
|
342 .put(
|
|
343 new Pair("java.lang.String", LONG_TYPE), StringToNumberConverter.toLong(integerFormat, true)); //$NON-NLS-1$
|
|
344 converterMap
|
|
345 .put(
|
|
346 new Pair(LONG_TYPE, "java.lang.String"), NumberToStringConverter.fromLong(integerFormat, true)); //$NON-NLS-1$
|
|
347 converterMap
|
|
348 .put(
|
|
349 new Pair(LONG_TYPE, "java.lang.Long"), new IdentityConverter(Long.TYPE, Long.classinfo)); //$NON-NLS-1$
|
|
350 converterMap
|
|
351 .put(
|
|
352 new Pair(LONG_TYPE, "java.lang.Object"), new IdentityConverter(Long.TYPE, Object.classinfo)); //$NON-NLS-1$
|
|
353
|
|
354 // Character.TYPE
|
|
355 converterMap
|
|
356 .put(
|
|
357 new Pair("java.lang.String", CHARACTER_TYPE), StringToCharacterConverter.toCharacter(true)); //$NON-NLS-1$
|
|
358 converterMap
|
|
359 .put(
|
|
360 new Pair(CHARACTER_TYPE, "java.lang.Character"), new IdentityConverter(Character.TYPE, Character.classinfo)); //$NON-NLS-1$
|
|
361 converterMap
|
|
362 .put(
|
|
363 new Pair(CHARACTER_TYPE, "java.lang.String"), CharacterToStringConverter.fromCharacter(true)); //$NON-NLS-1$
|
|
364 converterMap
|
|
365 .put(
|
|
366 new Pair(CHARACTER_TYPE, "java.lang.Object"), new IdentityConverter(Character.TYPE, Object.classinfo)); //$NON-NLS-1$
|
|
367
|
|
368 // Miscellaneous
|
|
369 converterMap
|
|
370 .put(
|
|
371 new Pair(
|
|
372 "org.eclipse.core.runtime.IStatus", "java.lang.String"), "org.eclipse.core.internal.databinding.conversion.StatusToStringConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
|
373
|
|
374 addNumberToByteConverters(converterMap, integerFormat,
|
|
375 integerClasses);
|
|
376 addNumberToByteConverters(converterMap, numberFormat, floatClasses);
|
|
377
|
|
378 addNumberToShortConverters(converterMap, integerFormat,
|
|
379 integerClasses);
|
|
380 addNumberToShortConverters(converterMap, numberFormat, floatClasses);
|
|
381
|
|
382 addNumberToIntegerConverters(converterMap, integerFormat,
|
|
383 integerClasses);
|
|
384 addNumberToIntegerConverters(converterMap, numberFormat,
|
|
385 floatClasses);
|
|
386
|
|
387 addNumberToLongConverters(converterMap, integerFormat,
|
|
388 integerClasses);
|
|
389 addNumberToLongConverters(converterMap, numberFormat, floatClasses);
|
|
390
|
|
391 addNumberToFloatConverters(converterMap, integerFormat,
|
|
392 integerClasses);
|
|
393 addNumberToFloatConverters(converterMap, numberFormat, floatClasses);
|
|
394
|
|
395 addNumberToDoubleConverters(converterMap, integerFormat,
|
|
396 integerClasses);
|
|
397 addNumberToDoubleConverters(converterMap, numberFormat,
|
|
398 floatClasses);
|
|
399
|
|
400 addNumberToBigIntegerConverters(converterMap, integerFormat,
|
|
401 integerClasses);
|
|
402 addNumberToBigIntegerConverters(converterMap, numberFormat,
|
|
403 floatClasses);
|
|
404
|
|
405 addNumberToBigDecimalConverters(converterMap, integerFormat,
|
|
406 integerClasses);
|
|
407 addNumberToBigDecimalConverters(converterMap, numberFormat,
|
|
408 floatClasses);
|
|
409 }
|
|
410
|
|
411 return converterMap;
|
|
412 }
|
|
413
|
|
414 private static final ClassInfo[] integerClasses = new ClassInfo[] [ Byte.TYPE,
|
|
415 Byte.classinfo, Short.TYPE, Short.classinfo, Integer.TYPE, Integer.classinfo,
|
|
416 Long.TYPE, Long.classinfo, BigInteger.classinfo ];
|
|
417
|
|
418 private static final ClassInfo[] floatClasses = new ClassInfo[] [ Float.TYPE,
|
|
419 Float.classinfo, Double.TYPE, Double.classinfo, BigDecimal.classinfo ];
|
|
420
|
|
421 /**
|
|
422 * Registers converters to boxed and unboxed types from a list of from
|
|
423 * classes.
|
|
424 *
|
|
425 * @param map
|
|
426 * @param numberFormat
|
|
427 * @param fromTypes
|
|
428 */
|
|
429 private static void addNumberToByteConverters(Map map,
|
|
430 NumberFormat numberFormat, ClassInfo[] fromTypes) {
|
|
431
|
|
432 for (int i = 0; i < fromTypes.length; i++) {
|
|
433 ClassInfo fromType = fromTypes[i];
|
|
434 if (!fromType.equals(Byte.classinfo) && !fromType.equalscast(Byte.TYPE)) {
|
|
435 String fromName = (fromType.isPrimitive()) ? getKeyForClass(
|
|
436 fromType, null) : fromType.getName();
|
|
437
|
|
438 map
|
|
439 .put(new Pair(fromName, BYTE_TYPE),
|
|
440 new NumberToByteConverter(numberFormat,
|
|
441 fromType, true));
|
|
442 map
|
|
443 .put(new Pair(fromName, Byte.classinfo.getName()),
|
|
444 new NumberToByteConverter(numberFormat,
|
|
445 fromType, false));
|
|
446 }
|
|
447 }
|
|
448 }
|
|
449
|
|
450 /**
|
|
451 * Registers converters to boxed and unboxed types from a list of from
|
|
452 * classes.
|
|
453 *
|
|
454 * @param map
|
|
455 * @param numberFormat
|
|
456 * @param fromTypes
|
|
457 */
|
|
458 private static void addNumberToShortConverters(Map map,
|
|
459 NumberFormat numberFormat, ClassInfo[] fromTypes) {
|
|
460 for (int i = 0; i < fromTypes.length; i++) {
|
|
461 ClassInfo fromType = fromTypes[i];
|
|
462 if (!fromType.equals(Short.classinfo) && !fromType.equalscast(Short.TYPE)) {
|
|
463 String fromName = (fromType.isPrimitive()) ? getKeyForClass(
|
|
464 fromType, null) : fromType.getName();
|
|
465
|
|
466 map
|
|
467 .put(new Pair(fromName, SHORT_TYPE),
|
|
468 new NumberToShortConverter(numberFormat,
|
|
469 fromType, true));
|
|
470 map.put(new Pair(fromName, Short.classinfo.getName()),
|
|
471 new NumberToShortConverter(numberFormat, fromType,
|
|
472 false));
|
|
473 }
|
|
474 }
|
|
475 }
|
|
476
|
|
477 /**
|
|
478 * Registers converters to boxed and unboxed types from a list of from
|
|
479 * classes.
|
|
480 *
|
|
481 * @param map
|
|
482 * @param numberFormat
|
|
483 * @param fromTypes
|
|
484 */
|
|
485 private static void addNumberToIntegerConverters(Map map,
|
|
486 NumberFormat numberFormat, ClassInfo[] fromTypes) {
|
|
487 for (int i = 0; i < fromTypes.length; i++) {
|
|
488 ClassInfo fromType = fromTypes[i];
|
|
489 if (!fromType.equals(Integer.classinfo)
|
|
490 && !fromType.equalscast(Integer.TYPE)) {
|
|
491 String fromName = (fromType.isPrimitive()) ? getKeyForClass(
|
|
492 fromType, null) : fromType.getName();
|
|
493
|
|
494 map.put(new Pair(fromName, INTEGER_TYPE),
|
|
495 new NumberToIntegerConverter(numberFormat, fromType,
|
|
496 true));
|
|
497 map.put(new Pair(fromName, Integer.classinfo.getName()),
|
|
498 new NumberToIntegerConverter(numberFormat, fromType,
|
|
499 false));
|
|
500 }
|
|
501 }
|
|
502 }
|
|
503
|
|
504 /**
|
|
505 * Registers converters to boxed and unboxed types from a list of from
|
|
506 * classes.
|
|
507 *
|
|
508 * @param map
|
|
509 * @param numberFormat
|
|
510 * @param fromTypes
|
|
511 */
|
|
512 private static void addNumberToLongConverters(Map map,
|
|
513 NumberFormat numberFormat, ClassInfo[] fromTypes) {
|
|
514 for (int i = 0; i < fromTypes.length; i++) {
|
|
515 ClassInfo fromType = fromTypes[i];
|
|
516 if (!fromType.equals(Long.classinfo) && !fromType.equalscast(Long.TYPE)) {
|
|
517 String fromName = (fromType.isPrimitive()) ? getKeyForClass(
|
|
518 fromType, null) : fromType.getName();
|
|
519
|
|
520 map
|
|
521 .put(new Pair(fromName, LONG_TYPE),
|
|
522 new NumberToLongConverter(numberFormat,
|
|
523 fromType, true));
|
|
524 map
|
|
525 .put(new Pair(fromName, Long.classinfo.getName()),
|
|
526 new NumberToLongConverter(numberFormat,
|
|
527 fromType, false));
|
|
528 }
|
|
529 }
|
|
530 }
|
|
531
|
|
532 /**
|
|
533 * Registers converters to boxed and unboxed types from a list of from
|
|
534 * classes.
|
|
535 *
|
|
536 * @param map
|
|
537 * @param numberFormat
|
|
538 * @param fromTypes
|
|
539 */
|
|
540 private static void addNumberToFloatConverters(Map map,
|
|
541 NumberFormat numberFormat, ClassInfo[] fromTypes) {
|
|
542 for (int i = 0; i < fromTypes.length; i++) {
|
|
543 ClassInfo fromType = fromTypes[i];
|
|
544 if (!fromType.equals(Float.classinfo) && !fromType.equalscast(Float.TYPE)) {
|
|
545 String fromName = (fromType.isPrimitive()) ? getKeyForClass(
|
|
546 fromType, null) : fromType.getName();
|
|
547
|
|
548 map
|
|
549 .put(new Pair(fromName, FLOAT_TYPE),
|
|
550 new NumberToFloatConverter(numberFormat,
|
|
551 fromType, true));
|
|
552 map.put(new Pair(fromName, Float.classinfo.getName()),
|
|
553 new NumberToFloatConverter(numberFormat, fromType,
|
|
554 false));
|
|
555 }
|
|
556 }
|
|
557 }
|
|
558
|
|
559 /**
|
|
560 * Registers converters to boxed and unboxed types from a list of from
|
|
561 * classes.
|
|
562 *
|
|
563 * @param map
|
|
564 * @param numberFormat
|
|
565 * @param fromTypes
|
|
566 */
|
|
567 private static void addNumberToDoubleConverters(Map map,
|
|
568 NumberFormat numberFormat, ClassInfo[] fromTypes) {
|
|
569 for (int i = 0; i < fromTypes.length; i++) {
|
|
570 ClassInfo fromType = fromTypes[i];
|
|
571 if (!fromType.equals(Double.classinfo) && !fromType.equalscast(Double.TYPE)) {
|
|
572 String fromName = (fromType.isPrimitive()) ? getKeyForClass(
|
|
573 fromType, null) : fromType.getName();
|
|
574
|
|
575 map.put(new Pair(fromName, DOUBLE_TYPE),
|
|
576 new NumberToDoubleConverter(numberFormat, fromType,
|
|
577 true));
|
|
578 map.put(new Pair(fromName, Double.classinfo.getName()),
|
|
579 new NumberToDoubleConverter(numberFormat, fromType,
|
|
580 false));
|
|
581 }
|
|
582 }
|
|
583 }
|
|
584
|
|
585 /**
|
|
586 * Registers converters to boxed and unboxed types from a list of from
|
|
587 * classes.
|
|
588 *
|
|
589 * @param map
|
|
590 * @param numberFormat
|
|
591 * @param fromTypes
|
|
592 */
|
|
593 private static void addNumberToBigIntegerConverters(Map map,
|
|
594 NumberFormat numberFormat, ClassInfo[] fromTypes) {
|
|
595 for (int i = 0; i < fromTypes.length; i++) {
|
|
596 ClassInfo fromType = fromTypes[i];
|
|
597 if (!fromType.equals(BigInteger.classinfo)) {
|
|
598 String fromName = (fromType.isPrimitive()) ? getKeyForClass(
|
|
599 fromType, null) : fromType.getName();
|
|
600
|
|
601 map
|
|
602 .put(new Pair(fromName, BigInteger.classinfo.getName()),
|
|
603 new NumberToBigIntegerConverter(numberFormat,
|
|
604 fromType));
|
|
605 }
|
|
606 }
|
|
607 }
|
|
608
|
|
609 /**
|
|
610 * Registers converters to boxed and unboxed types from a list of from
|
|
611 * classes.
|
|
612 *
|
|
613 * @param map
|
|
614 * @param numberFormat
|
|
615 * @param fromTypes
|
|
616 */
|
|
617 private static void addNumberToBigDecimalConverters(Map map,
|
|
618 NumberFormat numberFormat, ClassInfo[] fromTypes) {
|
|
619 for (int i = 0; i < fromTypes.length; i++) {
|
|
620 ClassInfo fromType = fromTypes[i];
|
|
621 if (!fromType.equals(BigDecimal.classinfo)) {
|
|
622 String fromName = (fromType.isPrimitive()) ? getKeyForClass(
|
|
623 fromType, null) : fromType.getName();
|
|
624
|
|
625 map
|
|
626 .put(new Pair(fromName, BigDecimal.classinfo.getName()),
|
|
627 new NumberToBigDecimalConverter(numberFormat,
|
|
628 fromType));
|
|
629 }
|
|
630 }
|
|
631 }
|
|
632
|
|
633 private static String getKeyForClass(Object originalValue,
|
|
634 ClassInfo filteredValue) {
|
|
635 if ( null !is cast(ClassInfo)originalValue ) {
|
|
636 ClassInfo originalClass = cast(ClassInfo) originalValue;
|
|
637 if (originalClass.equalscast(Integer.TYPE)) {
|
|
638 return INTEGER_TYPE;
|
|
639 } else if (originalClass.equalscast(Byte.TYPE)) {
|
|
640 return BYTE_TYPE;
|
|
641 } else if (originalClass.equalscast(Boolean.TYPE)) {
|
|
642 return BOOLEAN_TYPE;
|
|
643 } else if (originalClass.equalscast(Double.TYPE)) {
|
|
644 return DOUBLE_TYPE;
|
|
645 } else if (originalClass.equalscast(Float.TYPE)) {
|
|
646 return FLOAT_TYPE;
|
|
647 } else if (originalClass.equalscast(Long.TYPE)) {
|
|
648 return LONG_TYPE;
|
|
649 } else if (originalClass.equalscast(Short.TYPE)) {
|
|
650 return SHORT_TYPE;
|
|
651 }
|
|
652 }
|
|
653 return filteredValue.getName();
|
|
654 }
|
|
655
|
|
656 /**
|
|
657 * Returns {@link Boolean#TRUE} if the from type is assignable to the to
|
|
658 * type, or {@link Boolean#FALSE} if it not, or <code>null</code> if
|
|
659 * unknown.
|
|
660 *
|
|
661 * @param fromType
|
|
662 * @param toType
|
|
663 * @return whether fromType is assignable to toType, or <code>null</code>
|
|
664 * if unknown
|
|
665 */
|
|
666 protected Boolean isAssignableFromTo(Object fromType, Object toType) {
|
|
667 if ( null !is cast(ClassInfo )fromType && null !is cast(ClassInfo)toType ) {
|
|
668 ClassInfo toClass = cast(ClassInfo) toType;
|
|
669 if (toClass.isPrimitive()) {
|
|
670 toClass = autoboxed(toClass);
|
|
671 }
|
|
672 ClassInfo fromClass = cast(ClassInfo) fromType;
|
|
673 if (fromClass.isPrimitive()) {
|
|
674 fromClass = autoboxed(fromClass);
|
|
675 }
|
|
676 return toClass.isAssignableFrom(fromClass) ? Boolean.TRUE
|
|
677 : Boolean.FALSE;
|
|
678 }
|
|
679 return null;
|
|
680 }
|
|
681
|
|
682 /*
|
|
683 * Default converter implementation, does not perform any conversion.
|
|
684 */
|
|
685 protected static final class DefaultConverter : IConverter {
|
|
686
|
|
687 private final Object toType;
|
|
688
|
|
689 private final Object fromType;
|
|
690
|
|
691 /**
|
|
692 * @param fromType
|
|
693 * @param toType
|
|
694 */
|
|
695 this(Object fromType, Object toType) {
|
|
696 this.toType = toType;
|
|
697 this.fromType = fromType;
|
|
698 }
|
|
699
|
|
700 public Object convert(Object fromObject) {
|
|
701 return fromObject;
|
|
702 }
|
|
703
|
|
704 public Object getFromType() {
|
|
705 return fromType;
|
|
706 }
|
|
707
|
|
708 public Object getToType() {
|
|
709 return toType;
|
|
710 }
|
|
711 }
|
|
712
|
|
713 }
|