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