Mercurial > projects > dwt2
annotate org.eclipse.core.databinding/src/org/eclipse/core/databinding/UpdateValueStrategy.d @ 88:9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 11:10:09 +0200 |
parents | 6be48cf9f95c |
children |
rev | line source |
---|---|
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 - Character support completed (bug 197679) | |
11 * Tom Schindl<tom.schindl@bestsolution.at> - bugfix for 217940 | |
12 *******************************************************************************/ | |
13 | |
14 module org.eclipse.core.databinding.UpdateValueStrategy; | |
81 | 15 import org.eclipse.core.databinding.UpdateStrategy; |
78 | 16 |
17 import java.lang.all; | |
18 | |
19 import java.util.Date; | |
20 import java.util.HashMap; | |
21 | |
22 import org.eclipse.core.databinding.conversion.IConverter; | |
23 import org.eclipse.core.databinding.observable.value.IObservableValue; | |
24 import org.eclipse.core.databinding.validation.IValidator; | |
25 import org.eclipse.core.databinding.validation.ValidationStatus; | |
26 import org.eclipse.core.internal.databinding.BindingMessages; | |
27 import org.eclipse.core.internal.databinding.Pair; | |
28 import org.eclipse.core.internal.databinding.conversion.NumberToBigDecimalConverter; | |
29 import org.eclipse.core.internal.databinding.conversion.NumberToBigIntegerConverter; | |
30 import org.eclipse.core.internal.databinding.conversion.NumberToByteConverter; | |
31 import org.eclipse.core.internal.databinding.conversion.NumberToDoubleConverter; | |
32 import org.eclipse.core.internal.databinding.conversion.NumberToFloatConverter; | |
33 import org.eclipse.core.internal.databinding.conversion.NumberToIntegerConverter; | |
34 import org.eclipse.core.internal.databinding.conversion.NumberToLongConverter; | |
35 import org.eclipse.core.internal.databinding.conversion.NumberToNumberConverter; | |
36 import org.eclipse.core.internal.databinding.conversion.NumberToShortConverter; | |
37 import org.eclipse.core.internal.databinding.conversion.StringToCharacterConverter; | |
38 import org.eclipse.core.internal.databinding.conversion.StringToDateConverter; | |
39 import org.eclipse.core.internal.databinding.validation.NumberFormatConverter; | |
40 import org.eclipse.core.internal.databinding.validation.NumberToByteValidator; | |
41 import org.eclipse.core.internal.databinding.validation.NumberToDoubleValidator; | |
42 import org.eclipse.core.internal.databinding.validation.NumberToFloatValidator; | |
43 import org.eclipse.core.internal.databinding.validation.NumberToIntegerValidator; | |
44 import org.eclipse.core.internal.databinding.validation.NumberToLongValidator; | |
45 import org.eclipse.core.internal.databinding.validation.NumberToShortValidator; | |
46 import org.eclipse.core.internal.databinding.validation.NumberToUnboundedNumberValidator; | |
47 import org.eclipse.core.internal.databinding.validation.ObjectToPrimitiveValidator; | |
48 import org.eclipse.core.internal.databinding.validation.StringToByteValidator; | |
49 import org.eclipse.core.internal.databinding.validation.StringToCharacterValidator; | |
50 import org.eclipse.core.internal.databinding.validation.StringToDateValidator; | |
51 import org.eclipse.core.internal.databinding.validation.StringToDoubleValidator; | |
52 import org.eclipse.core.internal.databinding.validation.StringToFloatValidator; | |
53 import org.eclipse.core.internal.databinding.validation.StringToIntegerValidator; | |
54 import org.eclipse.core.internal.databinding.validation.StringToLongValidator; | |
55 import org.eclipse.core.internal.databinding.validation.StringToShortValidator; | |
56 import org.eclipse.core.runtime.IStatus; | |
57 import org.eclipse.core.runtime.Status; | |
58 | |
59 /** | |
60 * Customizes a {@link Binding} between two | |
61 * {@link IObservableValue observable values}. The following behaviors can be | |
62 * customized via the strategy: | |
63 * <ul> | |
64 * <li>Validation</li> | |
65 * <li>Conversion</li> | |
66 * <li>Automatic processing</li> | |
67 * </ul> | |
68 * <p> | |
69 * The update phases are: | |
70 * <ol> | |
85 | 71 * <li>Validate after get - {@link #validateAfterGet(Object)}</li> |
72 * <li>Conversion - {@link #convert(Object)}</li> | |
73 * <li>Validate after conversion - {@link #validateAfterConvert(Object)}</li> | |
74 * <li>Validate before set - {@link #validateBeforeSet(Object)}</li> | |
78 | 75 * <li>Value set - {@link #doSet(IObservableValue, Object)}</li> |
76 * </ol> | |
77 * </p> | |
78 * <p> | |
79 * Validation:<br/> {@link IValidator Validators} validate the value at | |
80 * multiple phases in the update process. Statuses returned from validators are | |
81 * aggregated into a <code>MultiStatus</code> until a status of | |
82 * <code>ERROR</code> or <code>CANCEL</code> is encountered. Either of these | |
83 * statuses will abort the update process. These statuses are available as the | |
84 * {@link Binding#getValidationStatus() binding validation status}. | |
85 * </p> | |
86 * <p> | |
87 * Conversion:<br/> A {@link IConverter converter} will convert the value from | |
88 * the type of the source observable into the type of the destination. The | |
89 * strategy has the ability to default converters for common scenarios. | |
90 * </p> | |
91 * <p> | |
92 * Automatic processing:<br/> The processing to perform when the source | |
93 * observable changes. This behavior is configured via policies provided on | |
94 * construction of the strategy (e.g. {@link #POLICY_NEVER}, | |
95 * {@link #POLICY_CONVERT}, {@link #POLICY_ON_REQUEST}, {@link #POLICY_UPDATE}). | |
96 * </p> | |
97 * | |
98 * @see DataBindingContext#bindValue(IObservableValue, IObservableValue, | |
99 * UpdateValueStrategy, UpdateValueStrategy) | |
100 * @see Binding#getValidationStatus() | |
101 * @see IValidator | |
102 * @see IConverter | |
103 * @since 1.0 | |
104 */ | |
105 public class UpdateValueStrategy : UpdateStrategy { | |
106 | |
107 /** | |
108 * Policy constant denoting that the source observable's state should not be | |
109 * tracked and that the destination observable's value should never be | |
110 * updated. | |
111 */ | |
112 public static int POLICY_NEVER = notInlined(1); | |
113 | |
114 /** | |
115 * Policy constant denoting that the source observable's state should not be | |
116 * tracked, but that validation, conversion and updating the destination | |
117 * observable's value should be performed when explicitly requested. | |
118 */ | |
119 public static int POLICY_ON_REQUEST = notInlined(2); | |
120 | |
121 /** | |
122 * Policy constant denoting that the source observable's state should be | |
123 * tracked, including validating changes except for | |
85 | 124 * {@link #validateBeforeSet(Object)}, but that the destination |
78 | 125 * observable's value should only be updated on request. |
126 */ | |
127 public static int POLICY_CONVERT = notInlined(4); | |
128 | |
129 /** | |
130 * Policy constant denoting that the source observable's state should be | |
131 * tracked, and that validation, conversion and updating the destination | |
132 * observable's value should be performed automaticlly on every change of | |
133 * the source observable value. | |
134 */ | |
135 public static int POLICY_UPDATE = notInlined(8); | |
136 | |
137 /** | |
138 * Helper method allowing API evolution of the above constant values. The | |
139 * compiler will not inline constant values into client code if values are | |
140 * "computed" using this helper. | |
141 * | |
142 * @param i | |
143 * an integer | |
144 * @return the same integer | |
145 */ | |
146 private static int notInlined(int i) { | |
147 return i; | |
148 } | |
149 | |
150 protected IValidator afterGetValidator; | |
151 protected IValidator afterConvertValidator; | |
152 protected IValidator beforeSetValidator; | |
153 protected IConverter converter; | |
154 | |
155 private int updatePolicy; | |
156 | |
85 | 157 private static ValidatorRegistry validatorRegistry; |
158 private static HashMap validatorsByConverter; | |
159 static this(){ | |
160 validatorRegistry = new ValidatorRegistry(); | |
161 validatorsByConverter = new HashMap(); | |
162 } | |
78 | 163 |
164 protected bool provideDefaults; | |
165 | |
166 /** | |
167 * <code>true</code> if we defaulted the converter | |
168 */ | |
169 private bool defaultedConverter = false; | |
170 | |
171 /** | |
172 * Creates a new update value strategy for automatically updating the | |
173 * destination observable value whenever the source observable value | |
174 * changes. Default validators and a default converter will be provided. The | |
175 * defaults can be changed by calling one of the setter methods. | |
176 */ | |
177 public this() { | |
178 this(true, POLICY_UPDATE); | |
179 } | |
180 | |
181 /** | |
182 * Creates a new update value strategy with a configurable update policy. | |
183 * Default validators and a default converter will be provided. The defaults | |
184 * can be changed by calling one of the setter methods. | |
185 * | |
186 * @param updatePolicy | |
187 * one of {@link #POLICY_NEVER}, {@link #POLICY_ON_REQUEST}, | |
188 * {@link #POLICY_CONVERT}, or {@link #POLICY_UPDATE} | |
189 */ | |
190 public this(int updatePolicy) { | |
191 this(true, updatePolicy); | |
192 } | |
193 | |
194 /** | |
195 * Creates a new update value strategy with a configurable update policy. | |
196 * Default validators and a default converter will be provided if | |
197 * <code>provideDefaults</code> is <code>true</code>. The defaults can | |
198 * be changed by calling one of the setter methods. | |
199 * | |
200 * @param provideDefaults | |
201 * if <code>true</code>, default validators and a default | |
202 * converter will be provided based on the observable value's | |
203 * type. | |
204 * @param updatePolicy | |
205 * one of {@link #POLICY_NEVER}, {@link #POLICY_ON_REQUEST}, | |
206 * {@link #POLICY_CONVERT}, or {@link #POLICY_UPDATE} | |
207 */ | |
208 public this(bool provideDefaults, int updatePolicy) { | |
209 this.provideDefaults = provideDefaults; | |
210 this.updatePolicy = updatePolicy; | |
211 } | |
212 | |
213 /** | |
214 * Converts the value from the source type to the destination type. | |
215 * <p> | |
216 * Default implementation will use the | |
85 | 217 * {@link #setConverter(IConverter) converter} if one exists. If no |
78 | 218 * converter exists no conversion occurs. |
219 * </p> | |
220 * | |
221 * @param value | |
222 * @return the converted value | |
223 */ | |
224 public Object convert(Object value) { | |
225 return converter is null ? value : converter.convert(value); | |
226 } | |
227 | |
228 /** | |
229 * Tries to create a validator that can validate values of type fromType. | |
230 * Returns <code>null</code> if no validator could be created. Either | |
231 * toType or modelDescription can be <code>null</code>, but not both. | |
232 * | |
233 * @param fromType | |
234 * @param toType | |
235 * @return an IValidator, or <code>null</code> if unsuccessful | |
236 */ | |
237 protected IValidator createValidator(Object fromType, Object toType) { | |
238 if (fromType is null || toType is null) { | |
239 return new class() IValidator { | |
240 | |
241 public IStatus validate(Object value) { | |
242 return Status.OK_STATUS; | |
243 } | |
244 }; | |
245 } | |
246 | |
247 return findValidator(fromType, toType); | |
248 } | |
249 | |
250 /** | |
251 * Fills out default values based upon the provided <code>source</code> | |
252 * and <code>destination</code>. If the strategy is to default values it | |
253 * will attempt to default a converter. If the converter can be defaulted an | |
254 * attempt is made to default the | |
85 | 255 * {@link #validateAfterGet(Object) after get validator}. If a validator |
78 | 256 * cannot be defaulted it will be <code>null</code>. |
257 * | |
258 * @param source | |
259 * @param destination | |
260 */ | |
261 protected void fillDefaults(IObservableValue source, | |
262 IObservableValue destination) { | |
263 Object sourceType = source.getValueType(); | |
264 Object destinationType = destination.getValueType(); | |
265 if (provideDefaults && sourceType !is null && destinationType !is null) { | |
266 if (converter is null) { | |
267 IConverter converter = createConverter(sourceType, | |
268 destinationType); | |
269 defaultedConverter = (converter !is null); | |
270 setConverter(converter); | |
271 } | |
272 | |
273 if (afterGetValidator is null) { | |
274 afterGetValidator = createValidator(sourceType, destinationType); | |
275 } | |
276 } | |
277 if (converter !is null) { | |
278 if (sourceType !is null) { | |
279 checkAssignable(converter.getFromType(), sourceType, | |
85 | 280 Format("converter does not convert from type {}", sourceType)); //$NON-NLS-1$ |
78 | 281 } |
282 if (destinationType !is null) { | |
283 checkAssignable(converter.getToType(), destinationType, | |
85 | 284 Format("converter does not convert to type {}", destinationType)); //$NON-NLS-1$ |
78 | 285 } |
286 } | |
287 } | |
85 | 288 package void fillDefaults_package(IObservableValue source, |
289 IObservableValue destination) { | |
290 fillDefaults(source, destination ); | |
291 } | |
78 | 292 |
293 private IValidator findValidator(Object fromType, Object toType) { | |
294 IValidator result = null; | |
295 | |
296 // We only default the validator if we defaulted the converter since the | |
297 // two are tightly coupled. | |
298 if (defaultedConverter) { | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
299 if (Class.fromType!(StringCls) is fromType) { |
85 | 300 result = cast(IValidator) validatorsByConverter.get(cast(Object)converter); |
78 | 301 |
302 if (result is null) { | |
303 // TODO sring based lookup | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
304 if (Class.fromType!(Integer).opEquals(toType) |
85 | 305 || Integer.TYPE.opEquals(toType)) { |
78 | 306 result = new StringToIntegerValidator( |
307 cast(NumberFormatConverter) converter); | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
308 } else if (Class.fromType!(Long).opEquals(toType) |
85 | 309 || Long.TYPE.opEquals(toType)) { |
78 | 310 result = new StringToLongValidator( |
311 cast(NumberFormatConverter) converter); | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
312 } else if (Class.fromType!(Float).opEquals(toType) |
85 | 313 || Float.TYPE.opEquals(toType)) { |
78 | 314 result = new StringToFloatValidator( |
315 cast(NumberFormatConverter) converter); | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
316 } else if (Class.fromType!(Double).opEquals(toType) |
85 | 317 || Double.TYPE.opEquals(toType)) { |
78 | 318 result = new StringToDoubleValidator( |
319 cast(NumberFormatConverter) converter); | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
320 } else if (Class.fromType!(Byte).opEquals(toType) |
85 | 321 || Byte.TYPE.opEquals(toType)) { |
78 | 322 result = new StringToByteValidator( |
323 cast(NumberFormatConverter) converter); | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
324 } else if (Class.fromType!(Short).opEquals(toType) |
85 | 325 || Short.TYPE.opEquals(toType)) { |
78 | 326 result = new StringToShortValidator( |
327 cast(NumberFormatConverter) converter); | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
328 } else if (Class.fromType!(Character).opEquals(toType) |
85 | 329 || Character.TYPE.opEquals(toType) |
78 | 330 && null !is cast(StringToCharacterConverter)converter ) { |
331 result = new StringToCharacterValidator( | |
332 cast(StringToCharacterConverter) converter); | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
333 } else if (Class.fromType!(Date).opEquals(toType) |
78 | 334 && null !is cast(StringToDateConverter)converter ) { |
335 result = new StringToDateValidator( | |
336 cast(StringToDateConverter) converter); | |
337 } | |
338 | |
339 if (result !is null) { | |
85 | 340 validatorsByConverter.put(cast(Object)converter, cast(Object)result); |
78 | 341 } |
342 } | |
343 } else if ( null !is cast(NumberToNumberConverter)converter ) { | |
85 | 344 result = cast(IValidator) validatorsByConverter.get(cast(Object)converter); |
78 | 345 |
346 if (result is null) { | |
347 if ( null !is cast(NumberToByteConverter)converter ) { | |
348 result = new NumberToByteValidator( | |
349 cast(NumberToByteConverter) converter); | |
350 } else if ( null !is cast(NumberToShortConverter)converter ) { | |
351 result = new NumberToShortValidator( | |
352 cast(NumberToShortConverter) converter); | |
353 } else if ( null !is cast(NumberToIntegerConverter)converter ) { | |
354 result = new NumberToIntegerValidator( | |
355 cast(NumberToIntegerConverter) converter); | |
356 } else if ( null !is cast(NumberToLongConverter)converter ) { | |
357 result = new NumberToLongValidator( | |
358 cast(NumberToLongConverter) converter); | |
359 } else if ( null !is cast(NumberToFloatConverter)converter ) { | |
360 result = new NumberToFloatValidator( | |
361 cast(NumberToFloatConverter) converter); | |
362 } else if ( null !is cast(NumberToDoubleConverter)converter ) { | |
363 result = new NumberToDoubleValidator( | |
364 cast(NumberToDoubleConverter) converter); | |
365 } else if ( null !is cast(NumberToBigIntegerConverter)converter | |
366 || null !is cast(NumberToBigDecimalConverter)converter ) { | |
367 result = new NumberToUnboundedNumberValidator( | |
368 cast(NumberToNumberConverter) converter); | |
369 } | |
370 } | |
371 } | |
372 | |
373 if (result is null) { | |
374 // TODO string based lookup | |
375 result = validatorRegistry.get(fromType, toType); | |
376 } | |
377 } | |
378 | |
379 return result; | |
380 } | |
381 | |
382 /** | |
383 * @return the update policy | |
384 */ | |
385 public int getUpdatePolicy() { | |
386 return updatePolicy; | |
387 } | |
388 | |
389 /** | |
390 * Sets the validator to be invoked after the source value is converted to | |
391 * the type of the destination observable. | |
392 * | |
393 * @param validator | |
394 * @return the receiver, to enable method call chaining | |
395 */ | |
396 public UpdateValueStrategy setAfterConvertValidator(IValidator validator) { | |
397 this.afterConvertValidator = validator; | |
398 return this; | |
399 } | |
400 | |
401 /** | |
402 * Sets the validator to be invoked after the source value is retrieved at | |
403 * the beginning of the synchronization process. | |
404 * | |
405 * @param validator | |
406 * @return the receiver, to enable method call chaining | |
407 */ | |
408 public UpdateValueStrategy setAfterGetValidator(IValidator validator) { | |
409 this.afterGetValidator = validator; | |
410 return this; | |
411 } | |
412 | |
413 /** | |
414 * Sets the validator to be invoked before the value is to be set on the | |
415 * destination at the end of the synchronization process. | |
416 * | |
417 * @param validator | |
418 * @return the receiver, to enable method call chaining | |
419 */ | |
420 public UpdateValueStrategy setBeforeSetValidator(IValidator validator) { | |
421 this.beforeSetValidator = validator; | |
422 return this; | |
423 } | |
424 | |
425 /** | |
426 * Sets the converter to be invoked when converting from the source type to | |
427 * the destination type. | |
428 * | |
429 * @param converter | |
430 * @return the receiver, to enable method call chaining | |
431 */ | |
432 public UpdateValueStrategy setConverter(IConverter converter) { | |
433 this.converter = converter; | |
434 return this; | |
435 } | |
436 | |
437 /** | |
438 * Validates the value after it is converted. | |
439 * <p> | |
440 * Default implementation will use the | |
85 | 441 * {@link #setAfterConvertValidator(IValidator) validator} if one exists. If |
78 | 442 * one does not exist no validation will occur. |
443 * </p> | |
444 * | |
445 * @param value | |
446 * @return an ok status | |
447 */ | |
448 public IStatus validateAfterConvert(Object value) { | |
449 return afterConvertValidator is null ? Status.OK_STATUS | |
450 : afterConvertValidator.validate(value); | |
451 } | |
452 | |
453 /** | |
454 * Validates the value after it is retrieved from the source. | |
455 * <p> | |
456 * Default implementation will use the | |
85 | 457 * {@link #setAfterGetValidator(IValidator) validator} if one exists. If one |
78 | 458 * does not exist no validation will occur. |
459 * </p> | |
460 * | |
461 * @param value | |
462 * @return an ok status | |
463 */ | |
464 public IStatus validateAfterGet(Object value) { | |
465 return afterGetValidator is null ? Status.OK_STATUS : afterGetValidator | |
466 .validate(value); | |
467 } | |
468 | |
469 /** | |
470 * Validates the value before it is set on the destination. | |
471 * <p> | |
472 * Default implementation will use the | |
85 | 473 * {@link #setBeforeSetValidator(IValidator) validator} if one exists. If |
78 | 474 * one does not exist no validation will occur. |
475 * </p> | |
476 * | |
477 * @param value | |
478 * @return an ok status | |
479 */ | |
480 public IStatus validateBeforeSet(Object value) { | |
481 return beforeSetValidator is null ? Status.OK_STATUS | |
482 : beforeSetValidator.validate(value); | |
483 } | |
484 | |
485 /** | |
486 * Sets the current value of the given observable to the given value. | |
487 * Clients may extend but must call the super implementation. | |
488 * | |
489 * @param observableValue | |
490 * @param value | |
491 * @return status | |
492 */ | |
493 protected IStatus doSet(IObservableValue observableValue, Object value) { | |
494 try { | |
495 observableValue.setValue(value); | |
496 } catch (Exception ex) { | |
497 return ValidationStatus.error(BindingMessages | |
85 | 498 .getString(BindingMessages.VALUEBINDING_ERROR_WHILE_SETTING_VALUE), |
78 | 499 ex); |
500 } | |
501 return Status.OK_STATUS; | |
502 } | |
85 | 503 package IStatus doSet_package(IObservableValue observableValue, Object value) { |
504 return doSet(observableValue,value); | |
505 } | |
78 | 506 |
507 private static class ValidatorRegistry { | |
508 | |
85 | 509 private HashMap validators; |
78 | 510 |
511 /** | |
512 * Adds the system-provided validators to the current validator | |
513 * registry. This is done automatically for the validator registry | |
514 * singleton. | |
515 */ | |
516 private this() { | |
85 | 517 validators = new HashMap(); |
78 | 518 // Standalone validators here... |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
519 associate(Class.fromType!(Integer), Integer.TYPE, |
85 | 520 new ObjectToPrimitiveValidator(Integer.TYPE)); |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
521 associate(Class.fromType!(Byte), Byte.TYPE, new ObjectToPrimitiveValidator( |
78 | 522 Byte.TYPE)); |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
523 associate(Class.fromType!(Short), Short.TYPE, new ObjectToPrimitiveValidator( |
78 | 524 Short.TYPE)); |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
525 associate(Class.fromType!(Long), Long.TYPE, new ObjectToPrimitiveValidator( |
78 | 526 Long.TYPE)); |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
527 associate(Class.fromType!(Float), Float.TYPE, new ObjectToPrimitiveValidator( |
78 | 528 Float.TYPE)); |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
529 associate(Class.fromType!(Double), Double.TYPE, |
85 | 530 new ObjectToPrimitiveValidator(Double.TYPE)); |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
531 associate(Class.fromType!(Boolean), Boolean.TYPE, |
85 | 532 new ObjectToPrimitiveValidator(Boolean.TYPE)); |
78 | 533 |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
534 associate(Class.fromType!(Object), Integer.TYPE, |
85 | 535 new ObjectToPrimitiveValidator(Integer.TYPE)); |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
536 associate(Class.fromType!(Object), Byte.TYPE, new ObjectToPrimitiveValidator( |
78 | 537 Byte.TYPE)); |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
538 associate(Class.fromType!(Object), Short.TYPE, new ObjectToPrimitiveValidator( |
78 | 539 Short.TYPE)); |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
540 associate(Class.fromType!(Object), Long.TYPE, new ObjectToPrimitiveValidator( |
78 | 541 Long.TYPE)); |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
542 associate(Class.fromType!(Object), Float.TYPE, new ObjectToPrimitiveValidator( |
78 | 543 Float.TYPE)); |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
544 associate(Class.fromType!(Object), Double.TYPE, |
85 | 545 new ObjectToPrimitiveValidator(Double.TYPE)); |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
546 associate(Class.fromType!(Object), Boolean.TYPE, |
85 | 547 new ObjectToPrimitiveValidator(Boolean.TYPE)); |
78 | 548 } |
549 | |
550 /** | |
551 * Associate a particular validator that can validate the conversion | |
552 * (fromClass, toClass) | |
553 * | |
554 * @param fromClass | |
555 * The Class to convert from | |
556 * @param toClass | |
557 * The Class to convert to | |
558 * @param validator | |
559 * The IValidator | |
560 */ | |
561 private void associate(Object fromClass, Object toClass, | |
562 IValidator validator) { | |
85 | 563 validators.put(new Pair(fromClass, toClass), cast(Object)validator); |
78 | 564 } |
565 | |
566 /** | |
567 * Return an IValidator for a specific fromClass and toClass. | |
568 * | |
569 * @param fromClass | |
570 * The Class to convert from | |
571 * @param toClass | |
572 * The Class to convert to | |
573 * @return An appropriate IValidator | |
574 */ | |
575 private IValidator get(Object fromClass, Object toClass) { | |
576 IValidator result = cast(IValidator) validators.get(new Pair(fromClass, | |
577 toClass)); | |
578 if (result !is null) | |
579 return result; | |
580 if (fromClass !is null && toClass !is null && fromClass is toClass) { | |
581 return new class() IValidator { | |
582 public IStatus validate(Object value) { | |
583 return Status.OK_STATUS; | |
584 } | |
585 }; | |
586 } | |
587 return new class() IValidator { | |
588 public IStatus validate(Object value) { | |
589 return Status.OK_STATUS; | |
590 } | |
591 }; | |
592 } | |
593 } | |
594 | |
595 } |