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