    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 */
    017
    018package org.apache.commons.beanutils.locale.converters;
    019
    020import java.text.DecimalFormat;
    021import java.text.NumberFormat;
    022import java.text.ParseException;
    023import java.util.Locale;
    024
    025import org.apache.commons.beanutils.locale.BaseLocaleConverter;
    026import org.apache.commons.logging.Log;
    027import org.apache.commons.logging.LogFactory;
    028
    029/**
    030 * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
    031 * implementation that converts an incoming
    032 * locale-sensitive String into a <code>java.lang.Number</code> object,
    033 * optionally using a default value or throwing a
    034 * {@link org.apache.commons.beanutils.ConversionException}
    035 * if a conversion error occurs.</p>
    036 *
    037 * @since 1.7
    038 */
    039
    040public class DecimalLocaleConverter extends BaseLocaleConverter {
    041
    042    /** All logging goes through this logger */
    043    private final Log log = LogFactory.getLog(DecimalLocaleConverter.class);
    044
    045    /**
    046     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    047     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    048     * if a conversion error occurs. The locale is the default locale for
    049     * this instance of the Java Virtual Machine and an unlocalized pattern is used
    050     * for the convertion.
    051     *
    052     */
    053    public DecimalLocaleConverter() {
    054
    055        this(false);
    056    }
    057
    058    /**
    059     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    060     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    061     * if a conversion error occurs. The locale is the default locale for
    062     * this instance of the Java Virtual Machine.
    063     *
    064     * @param locPattern    Indicate whether the pattern is localized or not
    065     */
    066    public DecimalLocaleConverter(final boolean locPattern) {
    067
    068        this(Locale.getDefault(), locPattern);
    069    }
    070
    071    /**
    072     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    073     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    074     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    075     *
    076     * @param locale        The locale
    077     */
    078    public DecimalLocaleConverter(final Locale locale) {
    079
    080        this(locale, false);
    081    }
    082
    083    /**
    084     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    085     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    086     * if a conversion error occurs.
    087     *
    088     * @param locale        The locale
    089     * @param locPattern    Indicate whether the pattern is localized or not
    090     */
    091    public DecimalLocaleConverter(final Locale locale, final boolean locPattern) {
    092
    093        this(locale, (String) null, locPattern);
    094    }
    095
    096    /**
    097     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    098     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    099     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    100     *
    101     * @param locale        The locale
    102     * @param pattern       The convertion pattern
    103     */
    104    public DecimalLocaleConverter(final Locale locale, final String pattern) {
    105
    106        this(locale, pattern, false);
    107    }
    108
    109    /**
    110     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    111     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    112     * if a conversion error occurs.
    113     *
    114     * @param locale        The locale
    115     * @param pattern       The convertion pattern
    116     * @param locPattern    Indicate whether the pattern is localized or not
    117     */
    118    public DecimalLocaleConverter(final Locale locale, final String pattern, final boolean locPattern) {
    119
    120        super(locale, pattern, locPattern);
    121    }
    122
    123    /**
    124     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    125     * that will return the specified default value
    126     * if a conversion error occurs. The locale is the default locale for
    127     * this instance of the Java Virtual Machine and an unlocalized pattern is used
    128     * for the convertion.
    129     *
    130     * @param defaultValue  The default value to be returned
    131     */
    132    public DecimalLocaleConverter(final Object defaultValue) {
    133
    134        this(defaultValue, false);
    135    }
    136
    137    /**
    138     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    139     * that will return the specified default value
    140     * if a conversion error occurs. The locale is the default locale for
    141     * this instance of the Java Virtual Machine.
    142     *
    143     * @param defaultValue  The default value to be returned
    144     * @param locPattern    Indicate whether the pattern is localized or not
    145     */
    146    public DecimalLocaleConverter(final Object defaultValue, final boolean locPattern) {
    147
    148        this(defaultValue, Locale.getDefault(), locPattern);
    149    }
    150
    151    /**
    152     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    153     * that will return the specified default value
    154     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    155     *
    156     * @param defaultValue  The default value to be returned
    157     * @param locale        The locale
    158     */
    159    public DecimalLocaleConverter(final Object defaultValue, final Locale locale) {
    160
    161        this(defaultValue, locale, false);
    162    }
    163
    164    /**
    165     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    166     * that will return the specified default value
    167     * if a conversion error occurs.
    168     *
    169     * @param defaultValue  The default value to be returned
    170     * @param locale        The locale
    171     * @param locPattern    Indicate whether the pattern is localized or not
    172     */
    173    public DecimalLocaleConverter(final Object defaultValue, final Locale locale, final boolean locPattern) {
    174
    175        this(defaultValue, locale, null, locPattern);
    176    }
    177
    178    /**
    179     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    180     * that will return the specified default value
    181     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    182     *
    183     * @param defaultValue  The default value to be returned
    184     * @param locale        The locale
    185     * @param pattern       The convertion pattern
    186     */
    187    public DecimalLocaleConverter(final Object defaultValue, final Locale locale, final String pattern) {
    188
    189        this(defaultValue, locale, pattern, false);
    190    }
    191
    192    /**
    193     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    194     * that will return the specified default value
    195     * if a conversion error occurs.
    196     *
    197     * @param defaultValue  The default value to be returned
    198     * @param locale        The locale
    199     * @param pattern       The convertion pattern
    200     * @param locPattern    Indicate whether the pattern is localized or not
    201     */
    202    public DecimalLocaleConverter(final Object defaultValue, final Locale locale, final String pattern, final boolean locPattern) {
    203
    204        super(defaultValue, locale, pattern, locPattern);
    205
    206    }
    207
    208    /**
    209     * Convert the specified locale-sensitive input object into an output
    210     * object of the specified type.
    211     *
    212     * @param value The input object to be converted
    213     * @param pattern The pattern is used for the convertion
    214     * @return The converted value
    215     * @throws org.apache.commons.beanutils.ConversionException if conversion
    216     * cannot be performed successfully
    217     * @throws ParseException if an error occurs parsing a String to a Number
    218     */
    219    @Override
    220    protected Object parse(final Object value, final String pattern) throws ParseException {
    221
    222        if (value instanceof Number) {
    223            return value;
    224        }
    225
    226        // Note that despite the ambiguous "getInstance" name, and despite the
    227        // fact that objects returned from this method have the same toString
    228        // representation, each call to getInstance actually returns a new
    229        // object.
    230        final DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(locale);
    231
    232        // if some constructors default pattern to null, it makes only sense
    233        // to handle null pattern gracefully
    234        if (pattern != null) {
    235            if (locPattern) {
    236                formatter.applyLocalizedPattern(pattern);
    237            } else {
    238                formatter.applyPattern(pattern);
    239            }
    240        } else {
    241            log.debug("No pattern provided, using default.");
    242        }
    243
    244        return formatter.parse((String) value);
    245    }
    246}