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