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