KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > validators > FloatValidator


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.util.validators;
25
26 import org.infoglue.cms.exception.Bug;
27 import org.infoglue.cms.exception.ConstraintException;
28 import org.infoglue.cms.util.ConstraintExceptionBuffer;
29
30
31 /**
32  * <todo>
33  * Time is running out, the illusion fades away...
34  * This package will be refactored/extended after iteration 1.
35  * - move to com.holigrow.yoda.util.validators ?
36  * - interfaces + factory
37  * - constructor madness (setXXX instead of N constructors?)
38  * - more validators
39  * - more fun
40  * </todo>
41  *
42  * @@author <a HREF="meat_for_the_butcher@@yahoo.com">Patrik Nyborg</a>
43  */

44 public class FloatValidator extends AbstractValidator
45 {
46   // --- [Constants] -----------------------------------------------------------
47

48   // error codes
49
private static final String JavaDoc ILLEGAL_VALUE_ERROR_CODE = "306";
50
51
52
53   // --- [Attributes] ----------------------------------------------------------
54

55   // <todo>fix so Range supports Number and values lower than 0</todo>
56
private float lowerLimit;
57   private float upperLimit;
58
59   
60
61   // --- [Static] --------------------------------------------------------------
62
// --- [Constructors] --------------------------------------------------------
63

64   /**
65    *
66    */

67   FloatValidator(String JavaDoc fieldName, boolean isRequired, float lowerLimit, float upperLimit) {
68     super(fieldName, isRequired);
69
70     if(lowerLimit > upperLimit) {
71       throw new Bug("Illegal arguments : lowerLimit=" + lowerLimit + ", upperLimit=" + upperLimit + ".");
72     }
73     this.lowerLimit = lowerLimit;
74     this.upperLimit = upperLimit;
75   }
76
77
78
79   // --- [Public] --------------------------------------------------------------
80

81   /**
82    *
83    */

84   public void validate(Float JavaDoc value) throws ConstraintException {
85     validateIsRequired(value);
86     if(value == null) { // no validation needed + no need for further null checking
87
return;
88     }
89     validateValueSpace(value.floatValue());
90     failIfAnyExceptionsFound();
91   }
92
93   /**
94    */

95   public void validate(Float JavaDoc value, ConstraintExceptionBuffer ceb) {
96     try {
97       validate(value);
98     } catch(ConstraintException e) {
99       ceb.add(e);
100     }
101   }
102
103   // --- [X implementation] ----------------------------------------------------
104
// --- [X Overrides] ---------------------------------------------------------
105
// --- [Package protected] ---------------------------------------------------
106
// --- [Private] -------------------------------------------------------------
107

108   /**
109    *
110    */

111   private void validateValueSpace(float value) {
112     if(value < this.lowerLimit || value > this.upperLimit) {
113       addConstraintException(ILLEGAL_VALUE_ERROR_CODE);
114     }
115   }
116
117
118
119   // --- [Protected] -----------------------------------------------------------
120
// --- [Inner classes] -------------------------------------------------------
121
}
Popular Tags