    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.validator.routines;
    018
    019import java.math.BigDecimal;
    020import java.text.Format;
    021import java.text.NumberFormat;
    022import java.util.Locale;
    023
    024/**
    025 * <p><b>BigDecimal Validation</b> and Conversion routines (<code>java.math.BigDecimal</code>).</p>
    026 *
    027 * <p>This validator provides a number of methods for
    028 *    validating/converting a <code>String</code> value to
    029 *    a <code>BigDecimal</code> using <code>java.text.NumberFormat</code>
    030 *    to parse either:</p>
    031 *    <ul>
    032 *       <li>using the default format for the default <code>Locale</code></li>
    033 *       <li>using a specified pattern with the default <code>Locale</code></li>
    034 *       <li>using the default format for a specified <code>Locale</code></li>
    035 *       <li>using a specified pattern with a specified <code>Locale</code></li>
    036 *    </ul>
    037 *
    038 * <p>Use one of the <code>isValid()</code> methods to just validate or
    039 *    one of the <code>validate()</code> methods to validate and receive a
    040 *    <i>converted</i> <code>BigDecimal</code> value.</p>
    041 *
    042 * <p>Fraction/decimal values are automatically trimmed to the appropriate length.</p>
    043 *
    044 * <p>Once a value has been successfully converted the following
    045 *    methods can be used to perform minimum, maximum and range checks:</p>
    046 *    <ul>
    047 *       <li><code>minValue()</code> checks whether the value is greater
    048 *           than or equal to a specified minimum.</li>
    049 *       <li><code>maxValue()</code> checks whether the value is less
    050 *           than or equal to a specified maximum.</li>
    051 *       <li><code>isInRange()</code> checks whether the value is within
    052 *           a specified range of values.</li>
    053 *    </ul>
    054 *
    055 * <p>So that the same mechanism used for parsing an <i>input</i> value
    056 *    for validation can be used to format <i>output</i>, corresponding
    057 *    <code>format()</code> methods are also provided. That is you can
    058 *    format either:</p>
    059 *    <ul>
    060 *       <li>using the default format for the default <code>Locale</code></li>
    061 *       <li>using a specified pattern with the default <code>Locale</code></li>
    062 *       <li>using the default format for a specified <code>Locale</code></li>
    063 *       <li>using a specified pattern with a specified <code>Locale</code></li>
    064 *    </ul>
    065 *
    066 * @version $Revision$
    067 * @since Validator 1.3.0
    068 */
    069public class BigDecimalValidator extends AbstractNumberValidator {
    070
    071    private static final long serialVersionUID = -670320911490506772L;
    072
    073    private static final BigDecimalValidator VALIDATOR = new BigDecimalValidator();
    074
    075    /**
    076     * Return a singleton instance of this validator.
    077     * @return A singleton instance of the BigDecimalValidator.
    078     */
    079    public static BigDecimalValidator getInstance() {
    080        return VALIDATOR;
    081    }
    082
    083    /**
    084     * Construct a <i>strict</i> instance.
    085     */
    086    public BigDecimalValidator() {
    087        this(true);
    088    }
    089
    090    /**
    091     * <p>Construct an instance with the specified strict setting.</p>
    092     *
    093     * @param strict <code>true</code> if strict
    094     *        <code>Format</code> parsing should be used.
    095     */
    096    public BigDecimalValidator(boolean strict) {
    097        this(strict, STANDARD_FORMAT, true);
    098    }
    099
    100    /**
    101     * <p>Construct an instance with the specified strict setting
    102     *    and format type.</p>
    103     *
    104     * <p>The <code>formatType</code> specified what type of
    105     *    <code>NumberFormat</code> is created - valid types
    106     *    are:</p>
    107     *    <ul>
    108     *       <li>AbstractNumberValidator.STANDARD_FORMAT -to create
    109     *           <i>standard</i> number formats (the default).</li>
    110     *       <li>AbstractNumberValidator.CURRENCY_FORMAT -to create
    111     *           <i>currency</i> number formats.</li>
    112     *       <li>AbstractNumberValidator.PERCENT_FORMAT -to create
    113     *           <i>percent</i> number formats (the default).</li>
    114     *    </ul>
    115     *
    116     * @param strict <code>true</code> if strict
    117     *        <code>Format</code> parsing should be used.
    118     * @param formatType The <code>NumberFormat</code> type to
    119     *        create for validation, default is STANDARD_FORMAT.
    120     * @param allowFractions <code>true</code> if fractions are
    121     *        allowed or <code>false</code> if integers only.
    122     */
    123    protected BigDecimalValidator(boolean strict, int formatType,
    124            boolean allowFractions) {
    125        super(strict, formatType, allowFractions);
    126    }
    127
    128    /**
    129     * <p>Validate/convert a <code>BigDecimal</code> using the default
    130     *    <code>Locale</code>.
    131     *
    132     * @param value The value validation is being performed on.
    133     * @return The parsed <code>BigDecimal</code> if valid or <code>null</code>
    134     *  if invalid.
    135     */
    136    public BigDecimal validate(String value) {
    137        return (BigDecimal)parse(value, (String)null, (Locale)null);
    138    }
    139
    140    /**
    141     * <p>Validate/convert a <code>BigDecimal</code> using the
    142     *    specified <i>pattern</i>.
    143     *
    144     * @param value The value validation is being performed on.
    145     * @param pattern The pattern used to validate the value against, or the
    146     *        default for the <code>Locale</code> if <code>null</code>.
    147     * @return The parsed <code>BigDecimal</code> if valid or <code>null</code> if invalid.
    148     */
    149    public BigDecimal validate(String value, String pattern) {
    150        return (BigDecimal)parse(value, pattern, (Locale)null);
    151    }
    152
    153    /**
    154     * <p>Validate/convert a <code>BigDecimal</code> using the
    155     *    specified <code>Locale</code>.
    156     *
    157     * @param value The value validation is being performed on.
    158     * @param locale The locale to use for the number format, system default if null.
    159     * @return The parsed <code>BigDecimal</code> if valid or <code>null</code> if invalid.
    160     */
    161    public BigDecimal validate(String value, Locale locale) {
    162        return (BigDecimal)parse(value, (String)null, locale);
    163    }
    164
    165    /**
    166     * <p>Validate/convert a <code>BigDecimal</code> using the
    167     *    specified pattern and/ or <code>Locale</code>.
    168     *
    169     * @param value The value validation is being performed on.
    170     * @param pattern The pattern used to validate the value against, or the
    171     *        default for the <code>Locale</code> if <code>null</code>.
    172     * @param locale The locale to use for the date format, system default if null.
    173     * @return The parsed <code>BigDecimal</code> if valid or <code>null</code> if invalid.
    174     */
    175    public BigDecimal validate(String value, String pattern, Locale locale) {
    176        return (BigDecimal)parse(value, pattern, locale);
    177    }
    178
    179    /**
    180     * Check if the value is within a specified range.
    181     *
    182     * @param value The <code>Number</code> value to check.
    183     * @param min The minimum value of the range.
    184     * @param max The maximum value of the range.
    185     * @return <code>true</code> if the value is within the
    186     *         specified range.
    187     */
    188    public boolean isInRange(BigDecimal value, double min, double max) {
    189        return (value.doubleValue() >= min && value.doubleValue() <= max);
    190    }
    191
    192    /**
    193     * Check if the value is greater than or equal to a minimum.
    194     *
    195     * @param value The value validation is being performed on.
    196     * @param min The minimum value.
    197     * @return <code>true</code> if the value is greater than
    198     *         or equal to the minimum.
    199     */
    200    public boolean minValue(BigDecimal value, double min) {
    201        return (value.doubleValue() >= min);
    202    }
    203
    204    /**
    205     * Check if the value is less than or equal to a maximum.
    206     *
    207     * @param value The value validation is being performed on.
    208     * @param max The maximum value.
    209     * @return <code>true</code> if the value is less than
    210     *         or equal to the maximum.
    211     */
    212    public boolean maxValue(BigDecimal value, double max) {
    213        return (value.doubleValue() <= max);
    214    }
    215
    216    /**
    217     * Convert the parsed value to a <code>BigDecimal</code>.
    218     *
    219     * @param value The parsed <code>Number</code> object created.
    220     * @param formatter The Format used to parse the value with.
    221     * @return The parsed <code>Number</code> converted to a
    222     *         <code>BigDecimal</code>.
    223     */
    224    @Override
    225    protected Object processParsedValue(Object value, Format formatter) {
    226        BigDecimal decimal = null;
    227        if (value instanceof Long) {
    228            decimal = BigDecimal.valueOf(((Long)value).longValue());
    229        } else {
    230            decimal = new BigDecimal(value.toString());
    231        }
    232
    233        int scale = determineScale((NumberFormat)formatter);
    234        if (scale >= 0) {
    235            decimal = decimal.setScale(scale, BigDecimal.ROUND_DOWN);
    236        }
    237
    238        return decimal;
    239    }
    240}