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