ShortValidator.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>Short Validation</b> and Conversion routines (<code>java.lang.Short</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>Short</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>Short</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 ShortValidator extends AbstractNumberValidator {

  63.     private static final long serialVersionUID = -5227510699747787066L;

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

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

  72.     /**
  73.      * Constructs a <i>strict</i> instance.
  74.      */
  75.     public ShortValidator() {
  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 ShortValidator(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 short value, final short min, final short 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 Short value, final short min, final short max) {
  124.         return isInRange(value.shortValue(), 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 short value, final short 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 Short value, final short max) {
  146.         return maxValue(value.shortValue(), 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 short value, final short 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 Short value, final short min) {
  168.         return minValue(value.shortValue(), min);
  169.     }

  170.     /**
  171.      * <p>Perform further validation and convert the <code>Number</code> to
  172.      * a <code>Short</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 a
  177.      *   <code>Short</code> if valid or {@code null} if invalid.
  178.      */
  179.     @Override
  180.     protected Object processParsedValue(final Object value, final Format formatter) {

  181.         final long longValue = ((Number) value).longValue();

  182.         if (longValue < Short.MIN_VALUE ||
  183.             longValue > Short.MAX_VALUE) {
  184.             return null;
  185.         }
  186.         return Short.valueOf((short) longValue);
  187.     }

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

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

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

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