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

  63.     private static final long serialVersionUID = 5867946581318211330L;

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

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

  72.     /**
  73.      * Constructs a <i>strict</i> instance.
  74.      */
  75.     public DoubleValidator() {
  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 DoubleValidator(final boolean strict, final int formatType) {
  100.         super(strict, formatType, true);
  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 double value, final double min, final double 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 Double value, final double min, final double max) {
  124.         return isInRange(value.doubleValue(), 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 double value, final double 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 Double value, final double max) {
  146.         return maxValue(value.doubleValue(), 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 double value, final double 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 Double value, final double min) {
  168.         return minValue(value.doubleValue(), min);
  169.     }

  170.     /**
  171.      * Convert the parsed value to a <code>Double</code>.
  172.      *
  173.      * @param value The parsed <code>Number</code> object created.
  174.      * @param formatter The Format used to parse the value with.
  175.      * @return The validated/converted <code>Double</code> value if valid
  176.      * or {@code null} if invalid.
  177.      */
  178.     @Override
  179.     protected Object processParsedValue(final Object value, final Format formatter) {
  180.         if (value instanceof Double) {
  181.             return value;
  182.         }
  183.         return Double.valueOf(((Number) value).doubleValue());

  184.     }

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

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

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

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