%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.services.intake.validator.BigDecimalValidator |
|
|
1 | package org.apache.turbine.services.intake.validator; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
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 | 0 | private BigDecimal minValue = null; |
47 | 0 | 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 | 0 | { |
58 | 0 | invalidNumberMessage = "Entry was not a valid BigDecimal"; |
59 | 0 | init(paramMap); |
60 | 0 | } |
61 | ||
62 | /** |
|
63 | * Default Constructor |
|
64 | */ |
|
65 | public BigDecimalValidator() |
|
66 | 0 | { |
67 | 0 | } |
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 | 0 | super.init(paramMap); |
79 | ||
80 | 0 | Constraint constraint = (Constraint) paramMap.get(MIN_VALUE_RULE_NAME); |
81 | 0 | if (constraint != null) |
82 | { |
|
83 | 0 | String param = constraint.getValue(); |
84 | 0 | minValue = new BigDecimal(param); |
85 | 0 | minValueMessage = constraint.getMessage(); |
86 | } |
|
87 | ||
88 | 0 | constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME); |
89 | 0 | if (constraint != null) |
90 | { |
|
91 | 0 | String param = constraint.getValue(); |
92 | 0 | maxValue = new BigDecimal(param); |
93 | 0 | maxValueMessage = constraint.getMessage(); |
94 | } |
|
95 | 0 | } |
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 | 0 | super.assertValidity(testValue); |
109 | ||
110 | 0 | if (required || StringUtils.isNotEmpty(testValue)) |
111 | { |
|
112 | 0 | BigDecimal bd = null; |
113 | try |
|
114 | { |
|
115 | 0 | bd = new BigDecimal(testValue); |
116 | } |
|
117 | 0 | catch (RuntimeException e) |
118 | { |
|
119 | 0 | errorMessage = invalidNumberMessage; |
120 | 0 | throw new ValidationException(invalidNumberMessage); |
121 | 0 | } |
122 | ||
123 | 0 | if (minValue != null && bd.compareTo(minValue) < 0) |
124 | { |
|
125 | 0 | errorMessage = minValueMessage; |
126 | 0 | throw new ValidationException(minValueMessage); |
127 | } |
|
128 | 0 | if (maxValue != null && bd.compareTo(maxValue) > 0) |
129 | { |
|
130 | 0 | errorMessage = maxValueMessage; |
131 | 0 | throw new ValidationException(maxValueMessage); |
132 | } |
|
133 | } |
|
134 | 0 | } |
135 | ||
136 | ||
137 | // ************************************************************ |
|
138 | // ** Bean accessor methods ** |
|
139 | // ************************************************************ |
|
140 | ||
141 | /** |
|
142 | * Get the value of minValue. |
|
143 | * |
|
144 | * @return value of minValue. |
|
145 | */ |
|
146 | public BigDecimal getMinValue() |
|
147 | { |
|
148 | 0 | 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 | 0 | this.minValue = minValue; |
159 | 0 | } |
160 | ||
161 | /** |
|
162 | * Get the value of maxValue. |
|
163 | * |
|
164 | * @return value of maxValue. |
|
165 | */ |
|
166 | public BigDecimal getMaxValue() |
|
167 | { |
|
168 | 0 | 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 | 0 | this.maxValue = maxValue; |
179 | 0 | } |
180 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |