KickJava   Java API By Example, From Geeks To Geeks.

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


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.NumberToNumberConverter;
17 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20
21 /**
22  * Base class for validators that validate if a Number can fit in another Number type.
23  * <p>
24  * Class is thread safe.
25  * </p>
26  *
27  * @since 1.0
28  */

29 public abstract class NumberToNumberValidator implements IValidator {
30     private final NumberToNumberConverter converter;
31
32     private final Number JavaDoc min;
33
34     private final Number JavaDoc max;
35
36     private String JavaDoc outOfRangeMessage;
37
38     private final boolean primitive;
39
40     /**
41      * @param converter
42      * @param min
43      * can be <code>null</code>
44      * @param max
45      * can be <code>null</code>
46      */

47     protected NumberToNumberValidator(NumberToNumberConverter converter,
48             Number JavaDoc min, Number JavaDoc max) {
49         this.converter = converter;
50         this.min = min;
51         this.max = max;
52
53         primitive = ((Class JavaDoc) converter.getToType()).isPrimitive();
54     }
55
56     /*
57      * (non-Javadoc)
58      *
59      * @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object)
60      */

61     public final IStatus validate(Object JavaDoc value) {
62         if (value == null) {
63             if (primitive) {
64                 throw new IllegalArgumentException JavaDoc(
65                         "Parameter 'value' cannot be null."); //$NON-NLS-1$
66
}
67
68             return Status.OK_STATUS;
69         }
70
71         if (!(value instanceof Number JavaDoc)) {
72             throw new IllegalArgumentException JavaDoc(
73                     "Parameter 'value' is not of type Number."); //$NON-NLS-1$
74
}
75
76         Number JavaDoc number = (Number JavaDoc) value;
77         if (inRange(number)) {
78             return Status.OK_STATUS;
79         }
80
81         synchronized (this) {
82             if (outOfRangeMessage == null && min != null && max != null) {
83                 outOfRangeMessage = StringToNumberParser
84                         .createOutOfRangeMessage(min, max, converter
85                                 .getNumberFormat());
86             }
87
88             return ValidationStatus.error(outOfRangeMessage);
89         }
90     }
91
92     /**
93      * Invoked to determine if the value is in range.
94      *
95      * @param number
96      * @return <code>true</code> if in range
97      */

98     protected abstract boolean inRange(Number JavaDoc number);
99 }
100
Popular Tags