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: * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
030: * implementation that converts an incoming
031: * locale-sensitive String into a <code>java.lang.Number</code> object,
032: * optionally using a default value or throwing a
033: * {@link org.apache.commons.beanutils.ConversionException}
034: * if a conversion error occurs.</p>
035: *
036: * @author Yauheny Mikulski
037: * @author Yoav Shapira
038: * @since 1.7
039: */
040:
041: public class DecimalLocaleConverter extends BaseLocaleConverter {
042:
043: // ----------------------------------------------------- Instance Variables
044:
045: /** All logging goes through this logger */
046: private Log log = LogFactory.getLog(DecimalLocaleConverter.class);
047:
048: // ----------------------------------------------------------- Constructors
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 and an unlocalized pattern is used
055: * for the convertion.
056: *
057: */
058: public DecimalLocaleConverter() {
059:
060: this (false);
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. The locale is the default locale for
067: * this instance of the Java Virtual Machine.
068: *
069: * @param locPattern Indicate whether the pattern is localized or not
070: */
071: public DecimalLocaleConverter(boolean locPattern) {
072:
073: this (Locale.getDefault(), locPattern);
074: }
075:
076: /**
077: * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
078: * that will throw a {@link org.apache.commons.beanutils.ConversionException}
079: * if a conversion error occurs. An unlocalized pattern is used for the convertion.
080: *
081: * @param locale The locale
082: */
083: public DecimalLocaleConverter(Locale locale) {
084:
085: this (locale, false);
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.
092: *
093: * @param locale The locale
094: * @param locPattern Indicate whether the pattern is localized or not
095: */
096: public DecimalLocaleConverter(Locale locale, boolean locPattern) {
097:
098: this (locale, (String) null, locPattern);
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. An unlocalized pattern is used for the convertion.
105: *
106: * @param locale The locale
107: * @param pattern The convertion pattern
108: */
109: public DecimalLocaleConverter(Locale locale, String pattern) {
110:
111: this (locale, pattern, false);
112: }
113:
114: /**
115: * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
116: * that will throw a {@link org.apache.commons.beanutils.ConversionException}
117: * if a conversion error occurs.
118: *
119: * @param locale The locale
120: * @param pattern The convertion pattern
121: * @param locPattern Indicate whether the pattern is localized or not
122: */
123: public DecimalLocaleConverter(Locale locale, String pattern,
124: boolean locPattern) {
125:
126: super (locale, pattern, locPattern);
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 and an unlocalized pattern is used
134: * for the convertion.
135: *
136: * @param defaultValue The default value to be returned
137: */
138: public DecimalLocaleConverter(Object defaultValue) {
139:
140: this (defaultValue, false);
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. The locale is the default locale for
147: * this instance of the Java Virtual Machine.
148: *
149: * @param defaultValue The default value to be returned
150: * @param locPattern Indicate whether the pattern is localized or not
151: */
152: public DecimalLocaleConverter(Object defaultValue,
153: 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,
181: boolean locPattern) {
182:
183: this (defaultValue, locale, null, locPattern);
184: }
185:
186: /**
187: * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
188: * that will return the specified default value
189: * if a conversion error occurs. An unlocalized pattern is used for the convertion.
190: *
191: * @param defaultValue The default value to be returned
192: * @param locale The locale
193: * @param pattern The convertion pattern
194: */
195: public DecimalLocaleConverter(Object defaultValue, Locale locale,
196: String pattern) {
197:
198: this (defaultValue, locale, pattern, false);
199: }
200:
201: /**
202: * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
203: * that will return the specified default value
204: * if a conversion error occurs.
205: *
206: * @param defaultValue The default value to be returned
207: * @param locale The locale
208: * @param pattern The convertion pattern
209: * @param locPattern Indicate whether the pattern is localized or not
210: */
211: public DecimalLocaleConverter(Object defaultValue, Locale locale,
212: String pattern, boolean locPattern) {
213:
214: super (defaultValue, locale, pattern, locPattern);
215:
216: }
217:
218: // --------------------------------------------------------- Methods
219:
220: /**
221: * Convert the specified locale-sensitive input object into an output
222: * object of the specified type.
223: *
224: * @param value The input object to be converted
225: * @param pattern The pattern is used for the convertion
226: * @return The converted value
227: *
228: * @exception org.apache.commons.beanutils.ConversionException if conversion
229: * cannot be performed successfully
230: * @throws ParseException if an error occurs parsing a String to a Number
231: */
232: protected Object parse(Object value, String pattern)
233: throws ParseException {
234:
235: if (value instanceof Number) {
236: return value;
237: }
238:
239: // Note that despite the ambiguous "getInstance" name, and despite the
240: // fact that objects returned from this method have the same toString
241: // representation, each call to getInstance actually returns a new
242: // object.
243: DecimalFormat formatter = (DecimalFormat) DecimalFormat
244: .getInstance(locale);
245:
246: // if some constructors default pattern to null, it makes only sense
247: // to handle null pattern gracefully
248: if (pattern != null) {
249: if (locPattern) {
250: formatter.applyLocalizedPattern(pattern);
251: } else {
252: formatter.applyPattern(pattern);
253: }
254: } else {
255: log.info("No pattern provided, using default.");
256: }
257:
258: return formatter.parse((String) value);
259: }
260: }
|