BigDecimalValidator.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.math.BigDecimal;
  19. import java.text.Format;
  20. import java.text.NumberFormat;
  21. import java.util.Locale;

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

  67.     private static final long serialVersionUID = -670320911490506772L;

  68.     private static final BigDecimalValidator VALIDATOR = new BigDecimalValidator();

  69.     /**
  70.      * Gets the singleton instance of this validator.
  71.      * @return A singleton instance of the BigDecimalValidator.
  72.      */
  73.     public static BigDecimalValidator getInstance() {
  74.         return VALIDATOR;
  75.     }

  76.     /**
  77.      * Constructs a <i>strict</i> instance.
  78.      */
  79.     public BigDecimalValidator() {
  80.         this(true);
  81.     }

  82.     /**
  83.      * <p>Construct an instance with the specified strict setting.</p>
  84.      *
  85.      * @param strict {@code true} if strict
  86.      *        <code>Format</code> parsing should be used.
  87.      */
  88.     public BigDecimalValidator(final boolean strict) {
  89.         this(strict, STANDARD_FORMAT, true);
  90.     }

  91.     /**
  92.      * <p>Construct an instance with the specified strict setting
  93.      *    and format type.</p>
  94.      *
  95.      * <p>The <code>formatType</code> specified what type of
  96.      *    <code>NumberFormat</code> is created - valid types
  97.      *    are:</p>
  98.      *    <ul>
  99.      *       <li>AbstractNumberValidator.STANDARD_FORMAT -to create
  100.      *           <i>standard</i> number formats (the default).</li>
  101.      *       <li>AbstractNumberValidator.CURRENCY_FORMAT -to create
  102.      *           <i>currency</i> number formats.</li>
  103.      *       <li>AbstractNumberValidator.PERCENT_FORMAT -to create
  104.      *           <i>percent</i> number formats (the default).</li>
  105.      *    </ul>
  106.      *
  107.      * @param strict {@code true} if strict
  108.      *        <code>Format</code> parsing should be used.
  109.      * @param formatType The <code>NumberFormat</code> type to
  110.      *        create for validation, default is STANDARD_FORMAT.
  111.      * @param allowFractions {@code true} if fractions are
  112.      *        allowed or {@code false} if integers only.
  113.      */
  114.     protected BigDecimalValidator(final boolean strict, final int formatType,
  115.             final boolean allowFractions) {
  116.         super(strict, formatType, allowFractions);
  117.     }

  118.     /**
  119.      * Check if the value is within a specified range.
  120.      *
  121.      * @param value The <code>Number</code> value to check.
  122.      * @param min The minimum value of the range.
  123.      * @param max The maximum value of the range.
  124.      * @return {@code true} if the value is within the
  125.      *         specified range.
  126.      */
  127.     public boolean isInRange(final BigDecimal value, final double min, final double max) {
  128.         return value.doubleValue() >= min && value.doubleValue() <= max;
  129.     }

  130.     /**
  131.      * Check if the value is less than or equal to a maximum.
  132.      *
  133.      * @param value The value validation is being performed on.
  134.      * @param max The maximum value.
  135.      * @return {@code true} if the value is less than
  136.      *         or equal to the maximum.
  137.      */
  138.     public boolean maxValue(final BigDecimal value, final double max) {
  139.         return value.doubleValue() <= max;
  140.     }

  141.     /**
  142.      * Check if the value is greater than or equal to a minimum.
  143.      *
  144.      * @param value The value validation is being performed on.
  145.      * @param min The minimum value.
  146.      * @return {@code true} if the value is greater than
  147.      *         or equal to the minimum.
  148.      */
  149.     public boolean minValue(final BigDecimal value, final double min) {
  150.         return value.doubleValue() >= min;
  151.     }

  152.     /**
  153.      * Convert the parsed value to a <code>BigDecimal</code>.
  154.      *
  155.      * @param value The parsed <code>Number</code> object created.
  156.      * @param formatter The Format used to parse the value with.
  157.      * @return The parsed <code>Number</code> converted to a
  158.      *         <code>BigDecimal</code>.
  159.      */
  160.     @Override
  161.     protected Object processParsedValue(final Object value, final Format formatter) {
  162.         BigDecimal decimal;
  163.         if (value instanceof Long) {
  164.             decimal = BigDecimal.valueOf(((Long) value).longValue());
  165.         } else {
  166.             decimal = new BigDecimal(value.toString());
  167.         }

  168.         final int scale = determineScale((NumberFormat) formatter);
  169.         if (scale >= 0) {
  170.             decimal = decimal.setScale(scale, BigDecimal.ROUND_DOWN);
  171.         }

  172.         return decimal;
  173.     }

  174.     /**
  175.      * <p>Validate/convert a <code>BigDecimal</code> using the default
  176.      *    <code>Locale</code>.
  177.      *
  178.      * @param value The value validation is being performed on.
  179.      * @return The parsed <code>BigDecimal</code> if valid or {@code null}
  180.      *  if invalid.
  181.      */
  182.     public BigDecimal validate(final String value) {
  183.         return (BigDecimal) parse(value, (String) null, (Locale) null);
  184.     }

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

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

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