    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.ParseException;
    021import java.util.Locale;
    022
    023/**
    024 * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
    025 * implementation that converts an incoming
    026 * locale-sensitive String into a <code>java.lang.Double</code> object,
    027 * optionally using a default value or throwing a
    028 * {@link org.apache.commons.beanutils.ConversionException}
    029 * if a conversion error occurs.</p>
    030 *
    031 */
    032
    033public class DoubleLocaleConverter extends DecimalLocaleConverter {
    034
    035    /**
    036     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    037     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    038     * if a conversion error occurs. The locale is the default locale for
    039     * this instance of the Java Virtual Machine and an unlocalized pattern is used
    040     * for the convertion.
    041     *
    042     */
    043    public DoubleLocaleConverter() {
    044
    045        this(false);
    046    }
    047
    048    /**
    049     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    050     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    051     * if a conversion error occurs. The locale is the default locale for
    052     * this instance of the Java Virtual Machine.
    053     *
    054     * @param locPattern    Indicate whether the pattern is localized or not
    055     */
    056    public DoubleLocaleConverter(final boolean locPattern) {
    057
    058        this(Locale.getDefault(), locPattern);
    059    }
    060
    061    /**
    062     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    063     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    064     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    065     *
    066     * @param locale        The locale
    067     */
    068    public DoubleLocaleConverter(final Locale locale) {
    069
    070        this(locale, false);
    071    }
    072
    073    /**
    074     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    075     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    076     * if a conversion error occurs.
    077     *
    078     * @param locale        The locale
    079     * @param locPattern    Indicate whether the pattern is localized or not
    080     */
    081    public DoubleLocaleConverter(final Locale locale, final boolean locPattern) {
    082
    083        this(locale, (String) null, locPattern);
    084    }
    085
    086    /**
    087     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    088     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    089     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    090     *
    091     * @param locale        The locale
    092     * @param pattern       The convertion pattern
    093     */
    094    public DoubleLocaleConverter(final Locale locale, final String pattern) {
    095
    096        this(locale, pattern, false);
    097    }
    098
    099    /**
    100     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    101     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
    102     * if a conversion error occurs.
    103     *
    104     * @param locale        The locale
    105     * @param pattern       The convertion pattern
    106     * @param locPattern    Indicate whether the pattern is localized or not
    107     */
    108    public DoubleLocaleConverter(final Locale locale, final String pattern, final boolean locPattern) {
    109
    110        super(locale, pattern, locPattern);
    111    }
    112
    113    /**
    114     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    115     * that will return the specified default value
    116     * if a conversion error occurs. The locale is the default locale for
    117     * this instance of the Java Virtual Machine and an unlocalized pattern is used
    118     * for the convertion.
    119     *
    120     * @param defaultValue  The default value to be returned
    121     */
    122    public DoubleLocaleConverter(final Object defaultValue) {
    123
    124        this(defaultValue, false);
    125    }
    126
    127    /**
    128     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    129     * that will return the specified default value
    130     * if a conversion error occurs. The locale is the default locale for
    131     * this instance of the Java Virtual Machine.
    132     *
    133     * @param defaultValue  The default value to be returned
    134     * @param locPattern    Indicate whether the pattern is localized or not
    135     */
    136    public DoubleLocaleConverter(final Object defaultValue, final boolean locPattern) {
    137
    138        this(defaultValue, Locale.getDefault(), locPattern);
    139    }
    140
    141    /**
    142     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    143     * that will return the specified default value
    144     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    145     *
    146     * @param defaultValue  The default value to be returned
    147     * @param locale        The locale
    148     */
    149    public DoubleLocaleConverter(final Object defaultValue, final Locale locale) {
    150
    151        this(defaultValue, locale, false);
    152    }
    153
    154    /**
    155     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    156     * that will return the specified default value
    157     * if a conversion error occurs.
    158     *
    159     * @param defaultValue  The default value to be returned
    160     * @param locale        The locale
    161     * @param locPattern    Indicate whether the pattern is localized or not
    162     */
    163    public DoubleLocaleConverter(final Object defaultValue, final Locale locale, final boolean locPattern) {
    164
    165        this(defaultValue, locale, null, locPattern);
    166    }
    167
    168    /**
    169     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    170     * that will return the specified default value
    171     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    172     *
    173     * @param defaultValue  The default value to be returned
    174     * @param locale        The locale
    175     * @param pattern       The convertion pattern
    176     */
    177    public DoubleLocaleConverter(final Object defaultValue, final Locale locale, final String pattern) {
    178
    179        this(defaultValue, locale, pattern, false);
    180    }
    181
    182    /**
    183     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    184     * that will return the specified default value
    185     * if a conversion error occurs.
    186     *
    187     * @param defaultValue  The default value to be returned
    188     * @param locale        The locale
    189     * @param pattern       The convertion pattern
    190     * @param locPattern    Indicate whether the pattern is localized or not
    191     */
    192    public DoubleLocaleConverter(final Object defaultValue, final Locale locale, final String pattern, final boolean locPattern) {
    193
    194        super(defaultValue, locale, pattern, locPattern);
    195    }
    196
    197    /**
    198     * Convert the specified locale-sensitive input object into an output object of the
    199     * specified type. This method will return Double type.
    200     *
    201     * @param value The input object to be converted
    202     * @param pattern The pattern is used for the convertion
    203     * @return The converted value
    204     * @throws org.apache.commons.beanutils.ConversionException if conversion cannot be performed
    205     *  successfully
    206     * @throws ParseException if an error occurs parsing a String to a Number
    207     */
    208    @Override
    209    protected Object parse(final Object value, final String pattern) throws ParseException {
    210        final Number result = (Number) super.parse(value, pattern);
    211        if (result instanceof Long) {
    212            return Double.valueOf(result.doubleValue());
    213        }
    214        return result;
    215    }
    216
    217}
    218