FloatValidator.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>Float Validation</strong> and Conversion routines ({@code java.lang.Float}).</p>
  23.  *
  24.  * <p>This validator provides a number of methods for
  25.  *    validating/converting a {@link String} value to
  26.  *    a {@code Float} 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 Float} 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 FloatValidator extends AbstractNumberValidator {

  64.     private static final long serialVersionUID = -4513245432806414267L;

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

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

  73.     /**
  74.      * Constructs a <em>strict</em> instance.
  75.      */
  76.     public FloatValidator() {
  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 FloatValidator(final boolean strict, final int formatType) {
  101.         super(strict, formatType, true);
  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 float value, final float min, final float 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 Float value, final float min, final float max) {
  125.         return isInRange(value.floatValue(), 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 float value, final float 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 Float value, final float max) {
  147.         return maxValue(value.floatValue(), 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 float value, final float 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 Float value, final float min) {
  169.         return minValue(value.floatValue(), min);
  170.     }

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

  182.         final double doubleValue = ((Number) value).doubleValue();

  183.         if (doubleValue > 0) {
  184.             if (doubleValue < Float.MIN_VALUE || doubleValue > Float.MAX_VALUE) {
  185.                 return null;
  186.             }
  187.         } else if (doubleValue < 0) {
  188.             final double posDouble = doubleValue * -1;
  189.             if (posDouble < Float.MIN_VALUE || posDouble > Float.MAX_VALUE) {
  190.                 return null;
  191.             }
  192.         }

  193.         return Float.valueOf((float) doubleValue);

  194.     }

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

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

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

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

  241. }