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.AbstractStringToNumberValidator;
|
81
|
13 import org.eclipse.core.internal.databinding.validation.NumberFormatConverter;
|
78
|
14
|
|
15 import java.lang.all;
|
|
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.StringToNumberParser;
|
|
20 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser.ParseResult;
|
|
21 import org.eclipse.core.runtime.IStatus;
|
|
22 import org.eclipse.core.runtime.Status;
|
|
23
|
|
24 /**
|
|
25 * Validates a number that is to be converted by a {@link NumberFormatConverter}.
|
|
26 * Validation is comprised of parsing the String and range checks.
|
|
27 *
|
|
28 * @since 1.0
|
|
29 */
|
|
30 public abstract class AbstractStringToNumberValidator : IValidator {
|
|
31 private final NumberFormatConverter converter;
|
|
32 private final bool toPrimitive;
|
|
33
|
|
34 private final Number min;
|
|
35 private final Number max;
|
|
36
|
|
37 private String outOfRangeMessage;
|
|
38
|
|
39 /**
|
|
40 * Constructs a new instance.
|
|
41 *
|
|
42 * @param converter converter and thus formatter to be used in validation
|
|
43 * @param min minimum value, used for reporting a range error to the user
|
|
44 * @param max maximum value, used for reporting a range error to the user
|
|
45 */
|
|
46 protected this(NumberFormatConverter converter,
|
|
47 Number min, Number max) {
|
|
48 this.converter = converter;
|
|
49 this.min = min;
|
|
50 this.max = max;
|
|
51
|
|
52 if (null !is cast(ClassInfo)converter.getToType()) {
|
|
53 ClassInfo clazz = cast(ClassInfo) converter.getToType();
|
|
54 toPrimitive = clazz.isPrimitive();
|
|
55 } else {
|
|
56 toPrimitive = false;
|
|
57 }
|
|
58 }
|
|
59
|
|
60 /**
|
|
61 * Validates the provided <code>value</code>. An error status is returned if:
|
|
62 * <ul>
|
|
63 * <li>The value cannot be parsed.</li>
|
|
64 * <li>The value is out of range.</li>
|
|
65 * </ul>
|
|
66 *
|
|
67 * @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object)
|
|
68 */
|
|
69 public final IStatus validate(Object value) {
|
|
70 ParseResult result = StringToNumberParser.parse(value, converter
|
|
71 .getNumberFormat(), toPrimitive);
|
|
72
|
|
73 if (result.getNumber() !is null) {
|
|
74 if (!isInRange(result.getNumber())) {
|
|
75 if (outOfRangeMessage is null) {
|
|
76 outOfRangeMessage = StringToNumberParser
|
|
77 .createOutOfRangeMessage(min, max, converter
|
|
78 .getNumberFormat());
|
|
79 }
|
|
80
|
|
81 return ValidationStatus.error(outOfRangeMessage);
|
|
82 }
|
|
83 } else if (result.getPosition() !is null) {
|
|
84 String parseErrorMessage = StringToNumberParser.createParseErrorMessage(
|
|
85 cast(String) value, result.getPosition());
|
|
86
|
|
87 return ValidationStatus.error(parseErrorMessage);
|
|
88 }
|
|
89
|
|
90 return Status.OK_STATUS;
|
|
91 }
|
|
92
|
|
93 /**
|
|
94 * Invoked by {@link #validatecast(Object)} when the range is to be validated.
|
|
95 *
|
|
96 * @param number
|
|
97 * @return <code>true</code> if in range
|
|
98 */
|
|
99 protected abstract bool isInRange(Number number);
|
|
100 }
|