View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2000-2003 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$ Date Author Changes 12/06/2000 Arnaud Blandin Created
34   */
35  package org.exolab.castor.xml.validators;
36  
37  import org.exolab.castor.xml.TypeValidator;
38  import org.exolab.castor.xml.ValidationContext;
39  import org.exolab.castor.xml.ValidationException;
40  
41  /**
42   * The Float Validation class. This class handles validation for the primitive <code>float</code>
43   * and <code>java.lang.Float</code> types.
44   *
45   * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
46   * @version $Revision$ $Date: 2003-03-03 02:57:21 -0700 (Mon, 03 Mar 2003) $
47   */
48  public class FloatValidator extends PatternValidator implements TypeValidator {
49  
50    /** If true, we perform "minimum inclusive value" validation. */
51    private boolean _useMinInclusive = false;
52    /** If true, we perform "minimum exclusive value" validation. */
53    private boolean _useMinExclusive = false;
54    /** If true, we perform "maximum inclusive value" validation. */
55    private boolean _useMaxInclusive = false;
56    /** If true, we perform "maximum exclusive value" validation. */
57    private boolean _useMaxExclusive = false;
58    /** If true, we perform "fixed" validation. */
59    private boolean _useFixed = false;
60    /** Minimum value (inclusive) for this float. (Not used unless _useMinInclusive == true.) */
61    private float _minInclusive = 0;
62    /** Minimum value (exclusive) for this float. (Not used unless _useMinExclusive == true.) */
63    private float _minExclusive = 0;
64    /** Maximum value (inclusive) for this float. (Not used unless _useMaxInclusive == true.) */
65    private float _maxInclusive = 0;
66    /** Maximum value (exclusive) for this float. (Not used unless _useMaxExclusive == true.) */
67    private float _maxExclusive = 0;
68    /** Fixed value of this float. (Not used unless _isFixed == true.) */
69    private float _fixed = 0;
70  
71    /**
72     * Creates a new FloatValidator with no restrictions.
73     */
74    public FloatValidator() {
75      super();
76    } // -- floatValidator
77  
78    /**
79     * Clears the fixed value for this FloatValidator.
80     */
81    public void clearFixed() {
82      _useFixed = false;
83    } // -- clearFixed
84  
85    /**
86     * Clears the maximum value for this FloatValidator.
87     */
88    public void clearMax() {
89      _useMaxExclusive = false;
90      _useMaxInclusive = false;
91    } // -- clearMax
92  
93    /**
94     * Clears the minimum value for this FloatValidator.
95     */
96    public void clearMin() {
97      _useMinExclusive = false;
98      _useMinInclusive = false;
99    } // -- clearMin
100 
101   /**
102    * Returns the configured fixed value for float validation. Returns null if no fixed value has
103    * been configured.
104    *
105    * @return the fixed value to validate against.
106    */
107   public Float getFixed() {
108     if (_useFixed) {
109       return new Float(_fixed);
110     }
111     return null;
112   } // -- getFixed
113 
114   /**
115    * Returns the configured inclusive maximum value for float validation. Returns null if no
116    * inclusive maximum has been configured.
117    *
118    * @return the inclusive maximum value to validate against.
119    */
120   public Float getMaxInclusive() {
121     if (_useMaxInclusive) {
122       return new Float(_maxInclusive);
123     }
124     return null;
125   } // -- getMaxInclusive
126 
127   /**
128    * Returns the configured exclusive maximum value for float validation. Returns null if no
129    * exclusive maximum has been configured.
130    *
131    * @return the exclusive maximum value to validate against.
132    */
133   public Float getMaxExclusive() {
134     if (_useMaxExclusive) {
135       return new Float(_maxExclusive);
136     }
137     return null;
138   } // -- getMaxInclusive
139 
140   /**
141    * Returns the configured inclusive minimum value for float validation. Returns null if no
142    * inclusive minimum has been configured.
143    *
144    * @return the inclusive minimum value to validate against.
145    */
146   public Float getMinInclusive() {
147     if (_useMinInclusive) {
148       return new Float(_minInclusive);
149     }
150     return null;
151   } // -- getMinInclusive
152 
153   /**
154    * Returns the configured exclusive minimum value for float validation. Returns null if no
155    * exclusive minimum has been configured.
156    *
157    * @return the exclusive minimum value to validate against.
158    */
159   public Float getMinExclusive() {
160     if (_useMinExclusive) {
161       return new Float(_minExclusive);
162     }
163     return null;
164   } // -- getMinInclusive
165 
166   /**
167    * Returns true if a fixed value to validate against has been set.
168    *
169    * @return true if a fixed value has been set.
170    */
171   public boolean hasFixed() {
172     return _useFixed;
173   } // -- hasFixed
174 
175   /**
176    * Sets the fixed value for float validation.
177    * <p>
178    * NOTE: If maximum and/or minimum values have been set and the fixed value is not within that
179    * max/min range, then no float will pass validation. This is as according to the XML Schema spec.
180    *
181    * @param fixedValue the fixed value that a float validated with this validator must be equal to.
182    */
183   public void setFixed(final float fixedValue) {
184     _fixed = fixedValue;
185     _useFixed = true;
186   } // -- setMinExclusive
187 
188   /**
189    * Sets the minimum (exclusive) value for float validation. To pass validation, a float must be
190    * greater than this value.
191    *
192    * @param minValue the minimum (exclusive) value for float validation.
193    */
194   public void setMinExclusive(final float minValue) {
195     _minExclusive = minValue;
196     _useMinExclusive = true;
197   } // -- setMinExclusive
198 
199   /**
200    * Sets the minimum (inclusive) value for float validation. To pass validation, a float must be
201    * greater than or equal to this value.
202    *
203    * @param minValue the minimum (inclusive) value for float validation.
204    */
205   public void setMinInclusive(final float minValue) {
206     _minInclusive = minValue;
207     _useMinInclusive = true;
208   } // -- setMinInclusive
209 
210   /**
211    * Sets the maximum (exclusive) value for float validation. To pass validation, a float must be
212    * less than this value.
213    *
214    * @param maxValue the maximum (exclusive) value for float validation.
215    */
216   public void setMaxExclusive(final float maxValue) {
217     _maxExclusive = maxValue;
218     _useMaxExclusive = true;
219   } // -- setMaxExclusive
220 
221   /**
222    * Sets the maximum (inclusive) value for float validation. To pass validation, a float must be
223    * less than or equal to this value.
224    *
225    * @param maxValue the maximum (inclusive) value for float validation.
226    */
227   public void setMaxInclusive(final float maxValue) {
228     _maxInclusive = maxValue;
229     _useMaxInclusive = true;
230   } // --setMaxInclusive
231 
232   /**
233    * Validates the given Object.
234    *
235    * @param d the float to validate
236    * @param context the ValidationContext
237    * @throws ValidationException if the object fails validation.
238    */
239   public void validate(final float d, final ValidationContext context) throws ValidationException {
240     if (_useFixed && d != _fixed) {
241       String err = "float " + d + " is not equal to the fixed value of " + _fixed;
242       throw new ValidationException(err);
243     }
244 
245     if (_useMinInclusive && d < _minInclusive) {
246       String err = "float " + d + " is less than the minimum allowed value: " + _minInclusive;
247       throw new ValidationException(err);
248     }
249 
250     if (_useMinExclusive && d <= _minExclusive) {
251       String err =
252           "float " + d + " is less than or equal to the minimum exclusive value: " + _minExclusive;
253       throw new ValidationException(err);
254     }
255 
256     if (_useMaxInclusive && d > _maxInclusive) {
257       String err = "float " + d + " is greater than the maximum allowed value: " + _maxInclusive;
258       throw new ValidationException(err);
259     }
260 
261     if (_useMaxExclusive && d >= _maxExclusive) {
262       String err = "float " + d + " is greater than or equal to the maximum exclusive value: "
263           + _maxExclusive;
264       throw new ValidationException(err);
265     }
266 
267     if (hasPattern()) {
268       super.validate(Float.toString(d), context);
269     }
270   } // -- validate
271 
272   /**
273    * Validates the given Object.
274    *
275    * @param object the Object to validate
276    * @throws ValidationException if the object fails validation.
277    */
278   public void validate(final Object object) throws ValidationException {
279     validate(object, (ValidationContext) null);
280   } // -- validate
281 
282   /**
283    * Validates the given Object.
284    *
285    * @param object the Object to validate
286    * @param context the ValidationContext
287    * @throws ValidationException if the object fails validation.
288    */
289   public void validate(final Object object, final ValidationContext context)
290       throws ValidationException {
291     if (object == null) {
292       String err = "floatValidator cannot validate a null object.";
293       throw new ValidationException(err);
294     }
295 
296     float value = 0;
297     try {
298       value = new java.lang.Float(object.toString()).floatValue();
299     } catch (Exception ex) {
300       String err = "Expecting a float, received instead: ";
301       err += object.getClass().getName();
302       throw new ValidationException(err);
303     }
304     validate(value, context);
305   } // -- validate
306 
307 } // -- FloatValidator