001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.validator.routines;
018
019import java.text.Format;
020import java.text.NumberFormat;
021import java.util.Locale;
022
023/**
024 * <p><strong>Integer Validation</strong> and Conversion routines ({{@link Integer}).</p>
025 *
026 * <p>This validator provides a number of methods for
027 *    validating/converting a {@link String} value to
028 *    a {@code Integer} using {@link NumberFormat}
029 *    to parse either:</p>
030 *    <ul>
031 *       <li>using the default format for the default {@link Locale}</li>
032 *       <li>using a specified pattern with the default {@link Locale}</li>
033 *       <li>using the default format for a specified {@link Locale}</li>
034 *       <li>using a specified pattern with a specified {@link Locale}</li>
035 *    </ul>
036 *
037 * <p>Use one of the {@code isValid()} methods to just validate or
038 *    one of the {@code validate()} methods to validate and receive a
039 *    <em>converted</em> {@code Integer} value.</p>
040 *
041 * <p>Once a value has been successfully converted the following
042 *    methods can be used to perform minimum, maximum and range checks:</p>
043 *    <ul>
044 *       <li>{@code minValue()} checks whether the value is greater
045 *           than or equal to a specified minimum.</li>
046 *       <li>{@code maxValue()} checks whether the value is less
047 *           than or equal to a specified maximum.</li>
048 *       <li>{@code isInRange()} checks whether the value is within
049 *           a specified range of values.</li>
050 *    </ul>
051 *
052 * <p>So that the same mechanism used for parsing an <em>input</em> value
053 *    for validation can be used to format <em>output</em>, corresponding
054 *    {@code format()} methods are also provided. That is you can
055 *    format either:</p>
056 *    <ul>
057 *       <li>using the default format for the default {@link Locale}</li>
058 *       <li>using a specified pattern with the default {@link Locale}</li>
059 *       <li>using the default format for a specified {@link Locale}</li>
060 *       <li>using a specified pattern with a specified {@link Locale}</li>
061 *    </ul>
062 *
063 * @since 1.3.0
064 */
065public class IntegerValidator extends AbstractNumberValidator {
066
067    private static final long serialVersionUID = 422081746310306596L;
068
069    private static final IntegerValidator VALIDATOR = new IntegerValidator();
070
071    /**
072     * Gets the singleton instance of this validator.
073     *
074     * @return A singleton instance of the IntegerValidator.
075     */
076    public static IntegerValidator getInstance() {
077        return VALIDATOR;
078    }
079
080    /**
081     * Constructs a <em>strict</em> instance.
082     */
083    public IntegerValidator() {
084        this(true, STANDARD_FORMAT);
085    }
086
087    /**
088     * <p>Construct an instance with the specified strict setting
089     *    and format type.</p>
090     *
091     * <p>The {@code formatType} specified what type of
092     *    {@code NumberFormat} is created - valid types
093     *    are:</p>
094     *    <ul>
095     *       <li>AbstractNumberValidator.STANDARD_FORMAT -to create
096     *           <em>standard</em> number formats (the default).</li>
097     *       <li>AbstractNumberValidator.CURRENCY_FORMAT -to create
098     *           <em>currency</em> number formats.</li>
099     *       <li>AbstractNumberValidator.PERCENT_FORMAT -to create
100     *           <em>percent</em> number formats (the default).</li>
101     *    </ul>
102     *
103     * @param strict {@code true} if strict
104     *        {@code Format} parsing should be used.
105     * @param formatType The {@code NumberFormat} type to
106     *        create for validation, default is STANDARD_FORMAT.
107     */
108    public IntegerValidator(final boolean strict, final int formatType) {
109        super(strict, formatType, false);
110    }
111
112    /**
113     * Check if the value is within a specified range.
114     *
115     * @param value The {@code Number} value to check.
116     * @param min The minimum value of the range.
117     * @param max The maximum value of the range.
118     * @return {@code true} if the value is within the
119     *         specified range.
120     */
121    public boolean isInRange(final int value, final int min, final int max) {
122        return value >= min && value <= max;
123    }
124
125    /**
126     * Check if the value is within a specified range.
127     *
128     * @param value The {@code Number} value to check.
129     * @param min The minimum value of the range.
130     * @param max The maximum value of the range.
131     * @return {@code true} if the value is within the
132     *         specified range.
133     */
134    public boolean isInRange(final Integer value, final int min, final int max) {
135        return isInRange(value.intValue(), min, max);
136    }
137
138    /**
139     * Check if the value is less than or equal to a maximum.
140     *
141     * @param value The value validation is being performed on.
142     * @param max The maximum value.
143     * @return {@code true} if the value is less than
144     *         or equal to the maximum.
145     */
146    public boolean maxValue(final int value, final int max) {
147        return value <= max;
148    }
149
150    /**
151     * Check if the value is less than or equal to a maximum.
152     *
153     * @param value The value validation is being performed on.
154     * @param max The maximum value.
155     * @return {@code true} if the value is less than
156     *         or equal to the maximum.
157     */
158    public boolean maxValue(final Integer value, final int max) {
159        return maxValue(value.intValue(), max);
160    }
161
162    /**
163     * Check if the value is greater than or equal to a minimum.
164     *
165     * @param value The value validation is being performed on.
166     * @param min The minimum value.
167     * @return {@code true} if the value is greater than
168     *         or equal to the minimum.
169     */
170    public boolean minValue(final int value, final int min) {
171        return value >= min;
172    }
173
174    /**
175     * Check if the value is greater than or equal to a minimum.
176     *
177     * @param value The value validation is being performed on.
178     * @param min The minimum value.
179     * @return {@code true} if the value is greater than
180     *         or equal to the minimum.
181     */
182    public boolean minValue(final Integer value, final int min) {
183        return minValue(value.intValue(), min);
184    }
185
186    /**
187     * <p>Perform further validation and convert the {@code Number} to
188     * an {@code Integer}.</p>
189     *
190     * @param value The parsed {@code Number} object created.
191     * @param formatter The Format used to parse the value with.
192     * @return The parsed {@code Number} converted to an
193     *   {@code Integer} if valid or {@code null} if invalid.
194     */
195    @Override
196    protected Object processParsedValue(final Object value, final Format formatter) {
197
198        // Parsed value will be Long if it fits in a long and is not fractional
199        if (value instanceof Long) {
200            final long longValue = ((Long) value).longValue();
201            if (longValue >= Integer.MIN_VALUE &&
202                longValue <= Integer.MAX_VALUE) {
203                return Integer.valueOf((int) longValue);
204            }
205        }
206        return null;
207    }
208
209    /**
210     * <p>Validate/convert an {@code Integer} using the default
211     *    {@link Locale}.
212     *
213     * @param value The value validation is being performed on.
214     * @return The parsed {@code Integer} if valid or {@code null}
215     *  if invalid.
216     */
217    public Integer validate(final String value) {
218        return (Integer) parse(value, (String) null, (Locale) null);
219    }
220
221    /**
222     * <p>Validate/convert an {@code Integer} using the
223     *    specified {@link Locale}.
224     *
225     * @param value The value validation is being performed on.
226     * @param locale The locale to use for the number format, system default if null.
227     * @return The parsed {@code Integer} if valid or {@code null} if invalid.
228     */
229    public Integer validate(final String value, final Locale locale) {
230        return (Integer) parse(value, (String) null, locale);
231    }
232
233    /**
234     * <p>Validate/convert an {@code Integer} using the
235     *    specified <em>pattern</em>.
236     *
237     * @param value The value validation is being performed on.
238     * @param pattern The pattern used to validate the value against.
239     * @return The parsed {@code Integer} if valid or {@code null} if invalid.
240     */
241    public Integer validate(final String value, final String pattern) {
242        return (Integer) parse(value, pattern, (Locale) null);
243    }
244
245    /**
246     * <p>Validate/convert a {@code Integer} using the
247     *    specified pattern and/ or {@link Locale}.
248     *
249     * @param value The value validation is being performed on.
250     * @param pattern The pattern used to validate the value against, or the
251     *        default for the {@link Locale} if {@code null}.
252     * @param locale The locale to use for the date format, system default if null.
253     * @return The parsed {@code Integer} if valid or {@code null} if invalid.
254     */
255    public Integer validate(final String value, final String pattern, final Locale locale) {
256        return (Integer) parse(value, pattern, locale);
257    }
258}