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