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