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