Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DecimalLocaleConverter |
|
| 1.3076923076923077;1.308 |
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 | * @author Yauheny Mikulski | |
38 | * @author Yoav Shapira | |
39 | * @since 1.7 | |
40 | */ | |
41 | ||
42 | public class DecimalLocaleConverter extends BaseLocaleConverter { | |
43 | ||
44 | ||
45 | // ----------------------------------------------------- Instance Variables | |
46 | ||
47 | /** All logging goes through this logger */ | |
48 | 406 | private Log log = LogFactory.getLog(DecimalLocaleConverter.class); |
49 | ||
50 | // ----------------------------------------------------------- Constructors | |
51 | ||
52 | /** | |
53 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
54 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
55 | * if a conversion error occurs. The locale is the default locale for | |
56 | * this instance of the Java Virtual Machine and an unlocalized pattern is used | |
57 | * for the convertion. | |
58 | * | |
59 | */ | |
60 | public DecimalLocaleConverter() { | |
61 | ||
62 | 0 | this(false); |
63 | 0 | } |
64 | ||
65 | /** | |
66 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
67 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
68 | * if a conversion error occurs. The locale is the default locale for | |
69 | * this instance of the Java Virtual Machine. | |
70 | * | |
71 | * @param locPattern Indicate whether the pattern is localized or not | |
72 | */ | |
73 | public DecimalLocaleConverter(boolean locPattern) { | |
74 | ||
75 | 0 | this(Locale.getDefault(), locPattern); |
76 | 0 | } |
77 | ||
78 | /** | |
79 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
80 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
81 | * if a conversion error occurs. An unlocalized pattern is used for the convertion. | |
82 | * | |
83 | * @param locale The locale | |
84 | */ | |
85 | public DecimalLocaleConverter(Locale locale) { | |
86 | ||
87 | 0 | this(locale, false); |
88 | 0 | } |
89 | ||
90 | /** | |
91 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
92 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
93 | * if a conversion error occurs. | |
94 | * | |
95 | * @param locale The locale | |
96 | * @param locPattern Indicate whether the pattern is localized or not | |
97 | */ | |
98 | public DecimalLocaleConverter(Locale locale, boolean locPattern) { | |
99 | ||
100 | 0 | this(locale, (String) null, locPattern); |
101 | 0 | } |
102 | ||
103 | /** | |
104 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
105 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
106 | * if a conversion error occurs. An unlocalized pattern is used for the convertion. | |
107 | * | |
108 | * @param locale The locale | |
109 | * @param pattern The convertion pattern | |
110 | */ | |
111 | public DecimalLocaleConverter(Locale locale, String pattern) { | |
112 | ||
113 | 0 | this(locale, pattern, false); |
114 | 0 | } |
115 | ||
116 | /** | |
117 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
118 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
119 | * if a conversion error occurs. | |
120 | * | |
121 | * @param locale The locale | |
122 | * @param pattern The convertion pattern | |
123 | * @param locPattern Indicate whether the pattern is localized or not | |
124 | */ | |
125 | public DecimalLocaleConverter(Locale locale, String pattern, boolean locPattern) { | |
126 | ||
127 | 374 | super(locale, pattern, locPattern); |
128 | 374 | } |
129 | ||
130 | /** | |
131 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
132 | * that will return the specified default value | |
133 | * if a conversion error occurs. The locale is the default locale for | |
134 | * this instance of the Java Virtual Machine and an unlocalized pattern is used | |
135 | * for the convertion. | |
136 | * | |
137 | * @param defaultValue The default value to be returned | |
138 | */ | |
139 | public DecimalLocaleConverter(Object defaultValue) { | |
140 | ||
141 | 0 | this(defaultValue, false); |
142 | 0 | } |
143 | ||
144 | /** | |
145 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
146 | * that will return the specified default value | |
147 | * if a conversion error occurs. The locale is the default locale for | |
148 | * this instance of the Java Virtual Machine. | |
149 | * | |
150 | * @param defaultValue The default value to be returned | |
151 | * @param locPattern Indicate whether the pattern is localized or not | |
152 | */ | |
153 | public DecimalLocaleConverter(Object defaultValue, boolean locPattern) { | |
154 | ||
155 | 0 | this(defaultValue, Locale.getDefault(), locPattern); |
156 | 0 | } |
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 | 0 | this(defaultValue, locale, false); |
169 | 0 | } |
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, boolean locPattern) { | |
181 | ||
182 | 0 | this(defaultValue, locale, null, locPattern); |
183 | 0 | } |
184 | ||
185 | /** | |
186 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
187 | * that will return the specified default value | |
188 | * if a conversion error occurs. An unlocalized pattern is used for the convertion. | |
189 | * | |
190 | * @param defaultValue The default value to be returned | |
191 | * @param locale The locale | |
192 | * @param pattern The convertion pattern | |
193 | */ | |
194 | public DecimalLocaleConverter(Object defaultValue, Locale locale, String pattern) { | |
195 | ||
196 | 0 | this(defaultValue, locale, pattern, false); |
197 | 0 | } |
198 | ||
199 | /** | |
200 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
201 | * that will return the specified default value | |
202 | * if a conversion error occurs. | |
203 | * | |
204 | * @param defaultValue The default value to be returned | |
205 | * @param locale The locale | |
206 | * @param pattern The convertion pattern | |
207 | * @param locPattern Indicate whether the pattern is localized or not | |
208 | */ | |
209 | public DecimalLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) { | |
210 | ||
211 | 32 | super(defaultValue, locale, pattern, locPattern); |
212 | ||
213 | 32 | } |
214 | ||
215 | // --------------------------------------------------------- Methods | |
216 | ||
217 | /** | |
218 | * Convert the specified locale-sensitive input object into an output | |
219 | * object of the specified type. | |
220 | * | |
221 | * @param value The input object to be converted | |
222 | * @param pattern The pattern is used for the convertion | |
223 | * @return The converted value | |
224 | * | |
225 | * @exception org.apache.commons.beanutils.ConversionException if conversion | |
226 | * cannot be performed successfully | |
227 | * @throws ParseException if an error occurs parsing a String to a Number | |
228 | */ | |
229 | protected Object parse(Object value, String pattern) throws ParseException { | |
230 | ||
231 | 305 | if (value instanceof Number) { |
232 | 2 | 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 | 303 | 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 | 303 | if (pattern != null) { |
244 | 181 | if (locPattern) { |
245 | 97 | formatter.applyLocalizedPattern(pattern); |
246 | } else { | |
247 | 84 | formatter.applyPattern(pattern); |
248 | } | |
249 | } else { | |
250 | 122 | log.debug("No pattern provided, using default."); |
251 | } | |
252 | ||
253 | 300 | return formatter.parse((String) value); |
254 | } | |
255 | } |