ByteValidator.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.  *      https://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.text.NumberFormat;
  20. import java.util.Locale;

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

  64.     private static final long serialVersionUID = 7001640945881854649L;

  65.     private static final ByteValidator VALIDATOR = new ByteValidator();

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

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

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

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

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

  127.     /**
  128.      * Check if the value is less than or equal to a maximum.
  129.      *
  130.      * @param value The value validation is being performed on.
  131.      * @param max The maximum value.
  132.      * @return {@code true} if the value is less than
  133.      *         or equal to the maximum.
  134.      */
  135.     public boolean maxValue(final byte value, final byte max) {
  136.         return value <= max;
  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 Byte value, final byte max) {
  147.         return maxValue(value.byteValue(), max);
  148.     }

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

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

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

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

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

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

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

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

  238. }