KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > intake > validator > BigDecimalValidator


1 package org.apache.turbine.services.intake.validator;
2
3 /*
4  * Copyright 2001-2004 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 JavaDoc;
20
21 import java.util.Map JavaDoc;
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>&nbsp;</td></tr>
33  * <tr><td>maxValue</td><td>less than BigDecimal maxValue</td>
34  * <td>&nbsp;</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,v 1.2.2.4 2004/05/20 03:06:47 seade Exp $
42  */

43 public class BigDecimalValidator
44         extends NumberValidator
45 {
46     private BigDecimal JavaDoc minValue = null;
47     private BigDecimal JavaDoc maxValue = null;
48
49     /**
50      * Constructor to use when initialising Object
51      *
52      * @param paramMap
53      * @throws InvalidMaskException
54      */

55     public BigDecimalValidator(Map JavaDoc 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 JavaDoc 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 JavaDoc param = constraint.getValue();
84             minValue = new BigDecimal JavaDoc(param);
85             minValueMessage = constraint.getMessage();
86         }
87
88         constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME);
89         if (constraint != null)
90         {
91             String JavaDoc param = constraint.getValue();
92             maxValue = new BigDecimal JavaDoc(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 JavaDoc testValue)
106             throws ValidationException
107     {
108         super.assertValidity(testValue);
109
110         if (required || StringUtils.isNotEmpty(testValue))
111         {
112             BigDecimal JavaDoc bd = null;
113             try
114             {
115                 bd = new BigDecimal JavaDoc(testValue);
116             }
117             catch (RuntimeException JavaDoc 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
// ** Bean accessor methods **
139
// ************************************************************
140

141     /**
142      * Get the value of minValue.
143      *
144      * @return value of minValue.
145      */

146     public BigDecimal JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc maxValue)
177     {
178         this.maxValue = maxValue;
179     }
180 }
181
Popular Tags