KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > validation > AbstractStringToNumberValidator


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 package org.eclipse.core.internal.databinding.validation;
13
14 import org.eclipse.core.databinding.validation.IValidator;
15 import org.eclipse.core.databinding.validation.ValidationStatus;
16 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser;
17 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser.ParseResult;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20
21 /**
22  * Validates a number that is to be converted by a {@link NumberFormatConverter}.
23  * Validation is comprised of parsing the String and range checks.
24  *
25  * @since 1.0
26  */

27 public abstract class AbstractStringToNumberValidator implements IValidator {
28     private final NumberFormatConverter converter;
29     private final boolean toPrimitive;
30
31     private final Number JavaDoc min;
32     private final Number JavaDoc max;
33
34     private String JavaDoc outOfRangeMessage;
35
36     /**
37      * Constructs a new instance.
38      *
39      * @param converter converter and thus formatter to be used in validation
40      * @param min minimum value, used for reporting a range error to the user
41      * @param max maximum value, used for reporting a range error to the user
42      */

43     protected AbstractStringToNumberValidator(NumberFormatConverter converter,
44             Number JavaDoc min, Number JavaDoc max) {
45         this.converter = converter;
46         this.min = min;
47         this.max = max;
48
49         if (converter.getToType() instanceof Class JavaDoc) {
50             Class JavaDoc clazz = (Class JavaDoc) converter.getToType();
51             toPrimitive = clazz.isPrimitive();
52         } else {
53             toPrimitive = false;
54         }
55     }
56
57     /**
58      * Validates the provided <code>value</code>. An error status is returned if:
59      * <ul>
60      * <li>The value cannot be parsed.</li>
61      * <li>The value is out of range.</li>
62      * </ul>
63      *
64      * @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object)
65      */

66     public final IStatus validate(Object JavaDoc value) {
67         ParseResult result = StringToNumberParser.parse(value, converter
68                 .getNumberFormat(), toPrimitive);
69
70         if (result.getNumber() != null) {
71             if (!isInRange(result.getNumber())) {
72                 if (outOfRangeMessage == null) {
73                     outOfRangeMessage = StringToNumberParser
74                             .createOutOfRangeMessage(min, max, converter
75                                     .getNumberFormat());
76                 }
77
78                 return ValidationStatus.error(outOfRangeMessage);
79             }
80         } else if (result.getPosition() != null) {
81             String JavaDoc parseErrorMessage = StringToNumberParser.createParseErrorMessage(
82                     (String JavaDoc) value, result.getPosition());
83
84             return ValidationStatus.error(parseErrorMessage);
85         }
86
87         return Status.OK_STATUS;
88     }
89
90     /**
91      * Invoked by {@link #validate(Object)} when the range is to be validated.
92      *
93      * @param number
94      * @return <code>true</code> if in range
95      */

96     protected abstract boolean isInRange(Number JavaDoc number);
97 }
98
Popular Tags