1 package org.apache.turbine.services.intake.validator;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Map;
23
24 import org.apache.commons.lang.StringUtils;
25
26 /***
27 * Validates Longs with the following constraints in addition to those
28 * listed in NumberValidator and DefaultValidator.
29 *
30 * <table>
31 * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
32 * <tr><td>minValue</td><td>greater than Long.MIN_VALUE</td>
33 * <td> </td></tr>
34 * <tr><td>maxValue</td><td>less than Long.MAX_VALUE</td>
35 * <td> </td></tr>
36 * <tr><td>invalidNumberMessage</td><td>Some text</td>
37 * <td>Entry was not a valid number</td></tr>
38 * </table>
39 *
40 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
41 * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
42 * @version $Id: LongValidator.java 534527 2007-05-02 16:10:59Z tv $
43 */
44 public class LongValidator
45 extends NumberValidator
46 {
47
48 private long minValue = Long.MIN_VALUE;
49
50
51 private long maxValue = Long.MAX_VALUE;
52
53 /***
54 * Constructor to use when initialising Object
55 *
56 * @param paramMap
57 * @throws InvalidMaskException
58 */
59 public LongValidator(Map paramMap)
60 throws InvalidMaskException
61 {
62 invalidNumberMessage = "Entry was not a valid Long";
63 init(paramMap);
64 }
65
66 /***
67 * Default Constructor
68 */
69 public LongValidator()
70 {
71 }
72
73 /***
74 * Method to initialise Object
75 *
76 * @param paramMap
77 * @throws InvalidMaskException
78 */
79 public void init(Map paramMap)
80 throws InvalidMaskException
81 {
82 super.init(paramMap);
83
84 Constraint constraint = (Constraint) paramMap.get(MIN_VALUE_RULE_NAME);
85 if (constraint != null)
86 {
87 String param = constraint.getValue();
88 minValue = Long.parseLong(param);
89 minValueMessage = constraint.getMessage();
90 }
91
92 constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME);
93 if (constraint != null)
94 {
95 String param = constraint.getValue();
96 maxValue = Long.parseLong(param);
97 maxValueMessage = constraint.getMessage();
98 }
99 }
100
101 /***
102 * Determine whether a testValue meets the criteria specified
103 * in the constraints defined for this validator
104 *
105 * @param testValue a <code>String</code> to be tested
106 * @exception ValidationException containing an error message if the
107 * testValue did not pass the validation tests.
108 */
109 public void assertValidity(String testValue)
110 throws ValidationException
111 {
112 super.assertValidity(testValue);
113
114 if (required || StringUtils.isNotEmpty(testValue))
115 {
116 long l = 0L;
117 try
118 {
119 l = Long.parseLong(testValue);
120 }
121 catch (RuntimeException e)
122 {
123 errorMessage = invalidNumberMessage;
124 throw new ValidationException(invalidNumberMessage);
125 }
126
127 if (l < minValue)
128 {
129 errorMessage = minValueMessage;
130 throw new ValidationException(minValueMessage);
131 }
132 if (l > maxValue)
133 {
134 errorMessage = maxValueMessage;
135 throw new ValidationException(maxValueMessage);
136 }
137 }
138 }
139
140
141
142
143
144
145 /***
146 * Get the value of minValue.
147 *
148 * @return value of minValue.
149 */
150 public long getMinValue()
151 {
152 return minValue;
153 }
154
155 /***
156 * Set the value of minValue.
157 *
158 * @param minValue Value to assign to minValue.
159 */
160 public void setMinValue(long minValue)
161 {
162 this.minValue = minValue;
163 }
164
165 /***
166 * Get the value of maxValue.
167 *
168 * @return value of maxValue.
169 */
170 public long getMaxValue()
171 {
172 return maxValue;
173 }
174
175 /***
176 * Set the value of maxValue.
177 *
178 * @param maxValue Value to assign to maxValue.
179 */
180 public void setMaxValue(long maxValue)
181 {
182 this.maxValue = maxValue;
183 }
184 }