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