IntegerValidator.java

  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. import java.text.Format;
  19. import java.util.Locale;

  20. /**
  21.  * <p><b>Integer Validation</b> and Conversion routines (<code>java.lang.Integer</code>).</p>
  22.  *
  23.  * <p>This validator provides a number of methods for
  24.  *    validating/converting a <code>String</code> value to
  25.  *    a <code>Integer</code> using <code>java.text.NumberFormat</code>
  26.  *    to parse either:</p>
  27.  *    <ul>
  28.  *       <li>using the default format for the default <code>Locale</code></li>
  29.  *       <li>using a specified pattern with the default <code>Locale</code></li>
  30.  *       <li>using the default format for a specified <code>Locale</code></li>
  31.  *       <li>using a specified pattern with a specified <code>Locale</code></li>
  32.  *    </ul>
  33.  *
  34.  * <p>Use one of the <code>isValid()</code> methods to just validate or
  35.  *    one of the <code>validate()</code> methods to validate and receive a
  36.  *    <i>converted</i> <code>Integer</code> value.</p>
  37.  *
  38.  * <p>Once a value has been successfully converted the following
  39.  *    methods can be used to perform minimum, maximum and range checks:</p>
  40.  *    <ul>
  41.  *       <li><code>minValue()</code> checks whether the value is greater
  42.  *           than or equal to a specified minimum.</li>
  43.  *       <li><code>maxValue()</code> checks whether the value is less
  44.  *           than or equal to a specified maximum.</li>
  45.  *       <li><code>isInRange()</code> checks whether the value is within
  46.  *           a specified range of values.</li>
  47.  *    </ul>
  48.  *
  49.  * <p>So that the same mechanism used for parsing an <i>input</i> value
  50.  *    for validation can be used to format <i>output</i>, corresponding
  51.  *    <code>format()</code> methods are also provided. That is you can
  52.  *    format either:</p>
  53.  *    <ul>
  54.  *       <li>using the default format for the default <code>Locale</code></li>
  55.  *       <li>using a specified pattern with the default <code>Locale</code></li>
  56.  *       <li>using the default format for a specified <code>Locale</code></li>
  57.  *       <li>using a specified pattern with a specified <code>Locale</code></li>
  58.  *    </ul>
  59.  *
  60.  * @since 1.3.0
  61.  */
  62. public class IntegerValidator extends AbstractNumberValidator {

  63.     private static final long serialVersionUID = 422081746310306596L;

  64.     private static final IntegerValidator VALIDATOR = new IntegerValidator();

  65.     /**
  66.      * Gets the singleton instance of this validator.
  67.      * @return A singleton instance of the IntegerValidator.
  68.      */
  69.     public static IntegerValidator getInstance() {
  70.         return VALIDATOR;
  71.     }

  72.     /**
  73.      * Constructs a <i>strict</i> instance.
  74.      */
  75.     public IntegerValidator() {
  76.         this(true, STANDARD_FORMAT);
  77.     }

  78.     /**
  79.      * <p>Construct an instance with the specified strict setting
  80.      *    and format type.</p>
  81.      *
  82.      * <p>The <code>formatType</code> specified what type of
  83.      *    <code>NumberFormat</code> is created - valid types
  84.      *    are:</p>
  85.      *    <ul>
  86.      *       <li>AbstractNumberValidator.STANDARD_FORMAT -to create
  87.      *           <i>standard</i> number formats (the default).</li>
  88.      *       <li>AbstractNumberValidator.CURRENCY_FORMAT -to create
  89.      *           <i>currency</i> number formats.</li>
  90.      *       <li>AbstractNumberValidator.PERCENT_FORMAT -to create
  91.      *           <i>percent</i> number formats (the default).</li>
  92.      *    </ul>
  93.      *
  94.      * @param strict {@code true} if strict
  95.      *        <code>Format</code> parsing should be used.
  96.      * @param formatType The <code>NumberFormat</code> type to
  97.      *        create for validation, default is STANDARD_FORMAT.
  98.      */
  99.     public IntegerValidator(final boolean strict, final int formatType) {
  100.         super(strict, formatType, false);
  101.     }

  102.     /**
  103.      * Check if the value is within a specified range.
  104.      *
  105.      * @param value The <code>Number</code> value to check.
  106.      * @param min The minimum value of the range.
  107.      * @param max The maximum value of the range.
  108.      * @return {@code true} if the value is within the
  109.      *         specified range.
  110.      */
  111.     public boolean isInRange(final int value, final int min, final int max) {
  112.         return value >= min && value <= max;
  113.     }

  114.     /**
  115.      * Check if the value is within a specified range.
  116.      *
  117.      * @param value The <code>Number</code> value to check.
  118.      * @param min The minimum value of the range.
  119.      * @param max The maximum value of the range.
  120.      * @return {@code true} if the value is within the
  121.      *         specified range.
  122.      */
  123.     public boolean isInRange(final Integer value, final int min, final int max) {
  124.         return isInRange(value.intValue(), min, max);
  125.     }

  126.     /**
  127.      * Check if the value is less than or equal to a maximum.
  128.      *
  129.      * @param value The value validation is being performed on.
  130.      * @param max The maximum value.
  131.      * @return {@code true} if the value is less than
  132.      *         or equal to the maximum.
  133.      */
  134.     public boolean maxValue(final int value, final int max) {
  135.         return value <= max;
  136.     }

  137.     /**
  138.      * Check if the value is less than or equal to a maximum.
  139.      *
  140.      * @param value The value validation is being performed on.
  141.      * @param max The maximum value.
  142.      * @return {@code true} if the value is less than
  143.      *         or equal to the maximum.
  144.      */
  145.     public boolean maxValue(final Integer value, final int max) {
  146.         return maxValue(value.intValue(), max);
  147.     }

  148.     /**
  149.      * Check if the value is greater than or equal to a minimum.
  150.      *
  151.      * @param value The value validation is being performed on.
  152.      * @param min The minimum value.
  153.      * @return {@code true} if the value is greater than
  154.      *         or equal to the minimum.
  155.      */
  156.     public boolean minValue(final int value, final int min) {
  157.         return value >= min;
  158.     }

  159.     /**
  160.      * Check if the value is greater than or equal to a minimum.
  161.      *
  162.      * @param value The value validation is being performed on.
  163.      * @param min The minimum value.
  164.      * @return {@code true} if the value is greater than
  165.      *         or equal to the minimum.
  166.      */
  167.     public boolean minValue(final Integer value, final int min) {
  168.         return minValue(value.intValue(), min);
  169.     }

  170.     /**
  171.      * <p>Perform further validation and convert the <code>Number</code> to
  172.      * an <code>Integer</code>.</p>
  173.      *
  174.      * @param value The parsed <code>Number</code> object created.
  175.      * @param formatter The Format used to parse the value with.
  176.      * @return The parsed <code>Number</code> converted to an
  177.      *   <code>Integer</code> if valid or {@code null} if invalid.
  178.      */
  179.     @Override
  180.     protected Object processParsedValue(final Object value, final Format formatter) {

  181.         // Parsed value will be Long if it fits in a long and is not fractional
  182.         if (value instanceof Long) {
  183.             final long longValue = ((Long) value).longValue();
  184.             if (longValue >= Integer.MIN_VALUE &&
  185.                 longValue <= Integer.MAX_VALUE) {
  186.                 return Integer.valueOf((int) longValue);
  187.             }
  188.         }
  189.         return null;
  190.     }

  191.     /**
  192.      * <p>Validate/convert an <code>Integer</code> using the default
  193.      *    <code>Locale</code>.
  194.      *
  195.      * @param value The value validation is being performed on.
  196.      * @return The parsed <code>Integer</code> if valid or {@code null}
  197.      *  if invalid.
  198.      */
  199.     public Integer validate(final String value) {
  200.         return (Integer) parse(value, (String) null, (Locale) null);
  201.     }

  202.     /**
  203.      * <p>Validate/convert an <code>Integer</code> using the
  204.      *    specified <code>Locale</code>.
  205.      *
  206.      * @param value The value validation is being performed on.
  207.      * @param locale The locale to use for the number format, system default if null.
  208.      * @return The parsed <code>Integer</code> if valid or {@code null} if invalid.
  209.      */
  210.     public Integer validate(final String value, final Locale locale) {
  211.         return (Integer) parse(value, (String) null, locale);
  212.     }

  213.     /**
  214.      * <p>Validate/convert an <code>Integer</code> using the
  215.      *    specified <i>pattern</i>.
  216.      *
  217.      * @param value The value validation is being performed on.
  218.      * @param pattern The pattern used to validate the value against.
  219.      * @return The parsed <code>Integer</code> if valid or {@code null} if invalid.
  220.      */
  221.     public Integer validate(final String value, final String pattern) {
  222.         return (Integer) parse(value, pattern, (Locale) null);
  223.     }

  224.     /**
  225.      * <p>Validate/convert a <code>Integer</code> using the
  226.      *    specified pattern and/ or <code>Locale</code>.
  227.      *
  228.      * @param value The value validation is being performed on.
  229.      * @param pattern The pattern used to validate the value against, or the
  230.      *        default for the <code>Locale</code> if {@code null}.
  231.      * @param locale The locale to use for the date format, system default if null.
  232.      * @return The parsed <code>Integer</code> if valid or {@code null} if invalid.
  233.      */
  234.     public Integer validate(final String value, final String pattern, final Locale locale) {
  235.         return (Integer) parse(value, pattern, locale);
  236.     }
  237. }