View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.beanutils.locale.converters;
19  
20  import org.apache.commons.beanutils.locale.BaseLocaleConverter;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import java.text.DecimalFormat;
25  import java.text.ParseException;
26  import java.util.Locale;
27  
28  
29  /**
30   * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
31   * implementation that converts an incoming
32   * locale-sensitive String into a <code>java.lang.Number</code> object,
33   * optionally using a default value or throwing a
34   * {@link org.apache.commons.beanutils.ConversionException}
35   * if a conversion error occurs.</p>
36   *
37   * @since 1.7
38   * @version $Id$
39   */
40  
41  public class DecimalLocaleConverter extends BaseLocaleConverter {
42  
43  
44      // ----------------------------------------------------- Instance Variables
45  
46      /** All logging goes through this logger */
47      private final Log log = LogFactory.getLog(DecimalLocaleConverter.class);
48  
49      // ----------------------------------------------------------- Constructors
50  
51      /**
52       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
53       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
54       * if a conversion error occurs. The locale is the default locale for
55       * this instance of the Java Virtual Machine and an unlocalized pattern is used
56       * for the convertion.
57       *
58       */
59      public DecimalLocaleConverter() {
60  
61          this(false);
62      }
63  
64      /**
65       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
66       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
67       * if a conversion error occurs. The locale is the default locale for
68       * this instance of the Java Virtual Machine.
69       *
70       * @param locPattern    Indicate whether the pattern is localized or not
71       */
72      public DecimalLocaleConverter(final boolean locPattern) {
73  
74          this(Locale.getDefault(), locPattern);
75      }
76  
77      /**
78       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
79       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
80       * if a conversion error occurs. An unlocalized pattern is used for the convertion.
81       *
82       * @param locale        The locale
83       */
84      public DecimalLocaleConverter(final Locale locale) {
85  
86          this(locale, false);
87      }
88  
89      /**
90       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
91       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
92       * if a conversion error occurs.
93       *
94       * @param locale        The locale
95       * @param locPattern    Indicate whether the pattern is localized or not
96       */
97      public DecimalLocaleConverter(final Locale locale, final boolean locPattern) {
98  
99          this(locale, (String) null, locPattern);
100     }
101 
102     /**
103      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
104      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
105      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
106      *
107      * @param locale        The locale
108      * @param pattern       The convertion pattern
109      */
110     public DecimalLocaleConverter(final Locale locale, final String pattern) {
111 
112         this(locale, pattern, false);
113     }
114 
115     /**
116      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
117      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
118      * if a conversion error occurs.
119      *
120      * @param locale        The locale
121      * @param pattern       The convertion pattern
122      * @param locPattern    Indicate whether the pattern is localized or not
123      */
124     public DecimalLocaleConverter(final Locale locale, final String pattern, final 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(final 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(final Object defaultValue, final boolean locPattern) {
153 
154         this(defaultValue, Locale.getDefault(), locPattern);
155     }
156 
157     /**
158      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
159      * that will return the specified default value
160      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
161      *
162      * @param defaultValue  The default value to be returned
163      * @param locale        The locale
164      */
165     public DecimalLocaleConverter(final Object defaultValue, final Locale locale) {
166 
167         this(defaultValue, locale, false);
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.
174      *
175      * @param defaultValue  The default value to be returned
176      * @param locale        The locale
177      * @param locPattern    Indicate whether the pattern is localized or not
178      */
179     public DecimalLocaleConverter(final Object defaultValue, final Locale locale, final boolean locPattern) {
180 
181         this(defaultValue, locale, null, locPattern);
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. An unlocalized pattern is used for the convertion.
188      *
189      * @param defaultValue  The default value to be returned
190      * @param locale        The locale
191      * @param pattern       The convertion pattern
192      */
193     public DecimalLocaleConverter(final Object defaultValue, final Locale locale, final String pattern) {
194 
195         this(defaultValue, locale, pattern, false);
196     }
197 
198     /**
199      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
200      * that will return the specified default value
201      * if a conversion error occurs.
202      *
203      * @param defaultValue  The default value to be returned
204      * @param locale        The locale
205      * @param pattern       The convertion pattern
206      * @param locPattern    Indicate whether the pattern is localized or not
207      */
208     public DecimalLocaleConverter(final Object defaultValue, final Locale locale, final String pattern, final boolean locPattern) {
209 
210         super(defaultValue, locale, pattern, locPattern);
211 
212     }
213 
214     // --------------------------------------------------------- Methods
215 
216     /**
217      * Convert the specified locale-sensitive input object into an output
218      * object of the specified type.
219      *
220      * @param value The input object to be converted
221      * @param pattern The pattern is used for the convertion
222      * @return The converted value
223      *
224      * @throws org.apache.commons.beanutils.ConversionException if conversion
225      * cannot be performed successfully
226      * @throws ParseException if an error occurs parsing a String to a Number
227      */
228     @Override
229     protected Object parse(final Object value, final 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         final 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 }