View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.beanutils.locale.converters;
18  
19  import org.apache.commons.beanutils.locale.BaseLocaleConverter;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  import java.text.DecimalFormat;
24  import java.text.ParseException;
25  import java.util.Locale;
26  
27  
28  /**
29   * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter} 
30   * implementation that converts an incoming
31   * locale-sensitive String into a <code>java.lang.Decimal</code> object,
32   * optionally using a default value or throwing a 
33   * {@link org.apache.commons.beanutils.ConversionException}
34   * if a conversion error occurs.</p>
35   *
36   * @author Yauheny Mikulski
37   * @author Yoav Shapira
38   * @since 1.7
39   */
40  
41  public class DecimalLocaleConverter extends BaseLocaleConverter {
42  
43  
44      // ----------------------------------------------------- Instance Variables
45  
46      /** All logging goes through this logger */
47      private static 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(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(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(Locale locale, 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(Locale locale, 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(Locale locale, String pattern, boolean locPattern) {
125 
126         this(null, 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, 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(Object defaultValue, 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(Object defaultValue, Locale locale, 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(Object defaultValue, Locale locale, 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(Object defaultValue, Locale locale, String pattern, 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 object of the
218      * specified type.
219      *
220      * @param value The input object to be converted
221      * @param pattern The pattern is used for the convertion
222      *
223      * @exception ConversionException if conversion cannot be performed
224      *  successfully
225      */
226     protected Object parse(Object value, String pattern) throws ParseException {
227         // DecimalFormat is not thread safe so best to construct one each time
228         DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(locale);
229         // if some constructors default pattern to null, it makes only sense to handle null pattern gracefully
230         if (pattern != null) {
231             if (locPattern) {
232                 formatter.applyLocalizedPattern(pattern);
233             } else {
234                 formatter.applyPattern(pattern);
235             }
236         } else {
237             log.warn("No pattern provided, using default.");
238         }
239 
240         return formatter.parse((String) value);
241     }
242 }