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