1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.commons.validator.routines;
22
23 import java.math.BigInteger;
24 import java.text.Format;
25 import java.util.Locale;
26
27 /***
28 * <p><b>BigInteger Validation</b> and Conversion routines (<code>java.math.BigInteger</code>).</p>
29 *
30 * <p>This validator provides a number of methods for
31 * validating/converting a <code>String</code> value to
32 * a <code>BigInteger</code> using <code>java.text.NumberFormat</code>
33 * to parse either:</p>
34 * <ul>
35 * <li>using the default format for the default <code>Locale</code></li>
36 * <li>using a specified pattern with the default <code>Locale</code></li>
37 * <li>using the default format for a specified <code>Locale</code></li>
38 * <li>using a specified pattern with a specified <code>Locale</code></li>
39 * </ul>
40 *
41 * <p>Use one of the <code>isValid()</code> methods to just validate or
42 * one of the <code>validate()</code> methods to validate and receive a
43 * <i>converted</i> <code>BigInteger</code> value.</p>
44 *
45 * <p>Once a value has been sucessfully converted the following
46 * methods can be used to perform minimum, maximum and range checks:</p>
47 * <ul>
48 * <li><code>minValue()</code> checks whether the value is greater
49 * than or equal to a specified minimum.</li>
50 * <li><code>maxValue()</code> checks whether the value is less
51 * than or equal to a specified maximum.</li>
52 * <li><code>isInRange()</code> checks whether the value is within
53 * a specified range of values.</li>
54 * </ul>
55 *
56 * <p>So that the same mechanism used for parsing an <i>input</i> value
57 * for validation can be used to format <i>output</i>, corresponding
58 * <code>format()</code> methods are also provided. That is you can
59 * format either:</p>
60 * <ul>
61 * <li>using the default format for the default <code>Locale</code></li>
62 * <li>using a specified pattern with the default <code>Locale</code></li>
63 * <li>using the default format for a specified <code>Locale</code></li>
64 * <li>using a specified pattern with a specified <code>Locale</code></li>
65 * </ul>
66 *
67 * @version $Revision: 386637 $ $Date: 2006-03-17 13:22:26 +0000 (Fri, 17 Mar 2006) $
68 * @since Validator 1.3.0
69 */
70 public class BigIntegerValidator extends AbstractNumberValidator {
71
72 private static final BigIntegerValidator VALIDATOR = new BigIntegerValidator();
73
74 /***
75 * Return a singleton instance of this validator.
76 * @return A singleton instance of the BigIntegerValidator.
77 */
78 public static BigIntegerValidator getInstance() {
79 return VALIDATOR;
80 }
81
82 /***
83 * Construct a <i>strict</i> instance.
84 */
85 public BigIntegerValidator() {
86 this(true, STANDARD_FORMAT);
87 }
88
89 /***
90 * <p>Construct an instance with the specified strict setting
91 * and format type.</p>
92 *
93 * <p>The <code>formatType</code> specified what type of
94 * <code>NumberFormat</code> is created - valid types
95 * are:</p>
96 * <ul>
97 * <li>AbstractNumberValidator.STANDARD_FORMAT -to create
98 * <i>standard</i> number formats (the default).</li>
99 * <li>AbstractNumberValidator.CURRENCY_FORMAT -to create
100 * <i>currency</i> number formats.</li>
101 * <li>AbstractNumberValidator.PERCENT_FORMAT -to create
102 * <i>percent</i> number formats (the default).</li>
103 * </ul>
104 *
105 * @param strict <code>true</code> if strict
106 * <code>Format</code> parsing should be used.
107 * @param formatType The <code>NumberFormat</code> type to
108 * create for validation, default is STANDARD_FORMAT.
109 */
110 public BigIntegerValidator(boolean strict, int formatType) {
111 super(strict, formatType, false);
112 }
113
114 /***
115 * <p>Validate/convert a <code>BigInteger</code> using the default
116 * <code>Locale</code>.
117 *
118 * @param value The value validation is being performed on.
119 * @return The parsed <code>BigInteger</code> if valid or <code>null</code>
120 * if invalid.
121 */
122 public BigInteger validate(String value) {
123 return (BigInteger)parse(value, (String)null, (Locale)null);
124 }
125
126 /***
127 * <p>Validate/convert a <code>BigInteger</code> using the
128 * specified <i>pattern</i>.
129 *
130 * @param value The value validation is being performed on.
131 * @param pattern The pattern used to validate the value against.
132 * @return The parsed <code>BigInteger</code> if valid or <code>null</code> if invalid.
133 */
134 public BigInteger validate(String value, String pattern) {
135 return (BigInteger)parse(value, pattern, (Locale)null);
136 }
137
138 /***
139 * <p>Validate/convert a <code>BigInteger</code> using the
140 * specified <code>Locale</code>.
141 *
142 * @param value The value validation is being performed on.
143 * @param locale The locale to use for the number format, system default if null.
144 * @return The parsed <code>BigInteger</code> if valid or <code>null</code> if invalid.
145 */
146 public BigInteger validate(String value, Locale locale) {
147 return (BigInteger)parse(value, (String)null, locale);
148 }
149
150 /***
151 * <p>Validate/convert a <code>BigInteger</code> using the
152 * specified pattern and/ or <code>Locale</code>.
153 *
154 * @param value The value validation is being performed on.
155 * @param pattern The pattern used to validate the value against, or the
156 * default for the <code>Locale</code> if <code>null</code>.
157 * @param locale The locale to use for the date format, system default if null.
158 * @return The parsed <code>BigInteger</code> if valid or <code>null</code> if invalid.
159 */
160 public BigInteger validate(String value, String pattern, Locale locale) {
161 return (BigInteger)parse(value, pattern, locale);
162 }
163
164 /***
165 * Check if the value is within a specified range.
166 *
167 * @param value The <code>Number</code> value to check.
168 * @param min The minimum value of the range.
169 * @param max The maximum value of the range.
170 * @return <code>true</code> if the value is within the
171 * specified range.
172 */
173 public boolean isInRange(BigInteger value, long min, long max) {
174 return (value.longValue() >= min && value.longValue() <= max);
175 }
176
177 /***
178 * Check if the value is greater than or equal to a minimum.
179 *
180 * @param value The value validation is being performed on.
181 * @param min The minimum value.
182 * @return <code>true</code> if the value is greater than
183 * or equal to the minimum.
184 */
185 public boolean minValue(BigInteger value, long min) {
186 return (value.longValue() >= min);
187 }
188
189 /***
190 * Check if the value is less than or equal to a maximum.
191 *
192 * @param value The value validation is being performed on.
193 * @param max The maximum value.
194 * @return <code>true</code> if the value is less than
195 * or equal to the maximum.
196 */
197 public boolean maxValue(BigInteger value, long max) {
198 return (value.longValue() <= max);
199 }
200
201 /***
202 * Convert the parsed value to a <code>BigInteger</code>.
203 *
204 * @param value The parsed <code>Number</code> object created.
205 * @param formatter The Format used to parse the value with.
206 * @return The parsed <code>Number</code> converted to a
207 * <code>BigInteger</code>.
208 */
209 protected Object processParsedValue(Object value, Format formatter) {
210 return BigInteger.valueOf(((Number)value).longValue());
211 }
212 }