78
|
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 module org.eclipse.core.internal.databinding.validation.NumberToNumberValidator;
|
|
13
|
|
14 import java.lang.all;
|
85
|
15 import java.nonstandard.RuntimeTraits;
|
78
|
16
|
|
17 import org.eclipse.core.databinding.validation.IValidator;
|
|
18 import org.eclipse.core.databinding.validation.ValidationStatus;
|
|
19 import org.eclipse.core.internal.databinding.conversion.NumberToNumberConverter;
|
|
20 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser;
|
|
21 import org.eclipse.core.runtime.IStatus;
|
|
22 import org.eclipse.core.runtime.Status;
|
|
23
|
|
24 /**
|
|
25 * Base class for validators that validate if a Number can fit in another Number type.
|
|
26 * <p>
|
|
27 * Class is thread safe.
|
|
28 * </p>
|
|
29 *
|
|
30 * @since 1.0
|
|
31 */
|
|
32 public abstract class NumberToNumberValidator : IValidator {
|
|
33 private final NumberToNumberConverter converter;
|
|
34
|
|
35 private final Number min;
|
|
36
|
|
37 private final Number max;
|
|
38
|
|
39 private String outOfRangeMessage;
|
|
40
|
|
41 private final bool primitive;
|
|
42
|
|
43 /**
|
|
44 * @param converter
|
|
45 * @param min
|
|
46 * can be <code>null</code>
|
|
47 * @param max
|
|
48 * can be <code>null</code>
|
|
49 */
|
|
50 protected this(NumberToNumberConverter converter,
|
|
51 Number min, Number max) {
|
|
52 this.converter = converter;
|
|
53 this.min = min;
|
|
54 this.max = max;
|
|
55
|
85
|
56 primitive = isJavaPrimitive(cast(TypeInfo)converter.getToType());
|
78
|
57 }
|
|
58
|
|
59 /*
|
|
60 * (non-Javadoc)
|
|
61 *
|
|
62 * @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object)
|
|
63 */
|
|
64 public final IStatus validate(Object value) {
|
|
65 if (value is null) {
|
|
66 if (primitive) {
|
|
67 throw new IllegalArgumentException(
|
|
68 "Parameter 'value' cannot be null."); //$NON-NLS-1$
|
|
69 }
|
|
70
|
|
71 return Status.OK_STATUS;
|
|
72 }
|
|
73
|
|
74 if (!( null !is cast(Number)value )) {
|
|
75 throw new IllegalArgumentException(
|
|
76 "Parameter 'value' is not of type Number."); //$NON-NLS-1$
|
|
77 }
|
|
78
|
|
79 Number number = cast(Number) value;
|
|
80 if (inRange(number)) {
|
|
81 return Status.OK_STATUS;
|
|
82 }
|
|
83
|
|
84 synchronized (this) {
|
|
85 if (outOfRangeMessage is null && min !is null && max !is null) {
|
|
86 outOfRangeMessage = StringToNumberParser
|
|
87 .createOutOfRangeMessage(min, max, converter
|
|
88 .getNumberFormat());
|
|
89 }
|
|
90
|
|
91 return ValidationStatus.error(outOfRangeMessage);
|
|
92 }
|
|
93 }
|
|
94
|
|
95 /**
|
|
96 * Invoked to determine if the value is in range.
|
|
97 *
|
|
98 * @param number
|
|
99 * @return <code>true</code> if in range
|
|
100 */
|
|
101 protected abstract bool inRange(Number number);
|
|
102 }
|