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