Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BigDecimalLocaleConverter |
|
| 1.4615384615384615;1.462 |
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 java.util.Locale; | |
21 | import java.math.BigDecimal; | |
22 | import java.text.ParseException; | |
23 | import org.apache.commons.beanutils.ConversionException; | |
24 | ||
25 | /** | |
26 | * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
27 | * implementation that converts an incoming | |
28 | * locale-sensitive String into a <code>java.math.BigDecimal</code> object, | |
29 | * optionally using a default value or throwing a | |
30 | * {@link org.apache.commons.beanutils.ConversionException} | |
31 | * if a conversion error occurs.</p> | |
32 | * | |
33 | * @author Yauheny Mikulski | |
34 | */ | |
35 | ||
36 | public class BigDecimalLocaleConverter extends DecimalLocaleConverter { | |
37 | ||
38 | // ----------------------------------------------------------- Constructors | |
39 | ||
40 | /** | |
41 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
42 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
43 | * if a conversion error occurs. The locale is the default locale for | |
44 | * this instance of the Java Virtual Machine and an unlocalized pattern is used | |
45 | * for the convertion. | |
46 | * | |
47 | */ | |
48 | public BigDecimalLocaleConverter() { | |
49 | ||
50 | 1 | this(false); |
51 | 1 | } |
52 | ||
53 | /** | |
54 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
55 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
56 | * if a conversion error occurs. The locale is the default locale for | |
57 | * this instance of the Java Virtual Machine. | |
58 | * | |
59 | * @param locPattern Indicate whether the pattern is localized or not | |
60 | */ | |
61 | public BigDecimalLocaleConverter(boolean locPattern) { | |
62 | ||
63 | 2 | this(Locale.getDefault(), locPattern); |
64 | 2 | } |
65 | ||
66 | /** | |
67 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
68 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
69 | * if a conversion error occurs. An unlocalized pattern is used for the convertion. | |
70 | * | |
71 | * @param locale The locale | |
72 | */ | |
73 | public BigDecimalLocaleConverter(Locale locale) { | |
74 | ||
75 | 1 | this(locale, false); |
76 | 1 | } |
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. | |
82 | * | |
83 | * @param locale The locale | |
84 | * @param locPattern Indicate whether the pattern is localized or not | |
85 | */ | |
86 | public BigDecimalLocaleConverter(Locale locale, boolean locPattern) { | |
87 | ||
88 | 27 | this(locale, (String) null, locPattern); |
89 | 27 | } |
90 | ||
91 | /** | |
92 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
93 | * that will throw a {@link org.apache.commons.beanutils.ConversionException} | |
94 | * if a conversion error occurs. An unlocalized pattern is used for the convertion. | |
95 | * | |
96 | * @param locale The locale | |
97 | * @param pattern The convertion pattern | |
98 | */ | |
99 | public BigDecimalLocaleConverter(Locale locale, String pattern) { | |
100 | ||
101 | 1 | this(locale, pattern, false); |
102 | 1 | } |
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 BigDecimalLocaleConverter(Locale locale, String pattern, boolean locPattern) { | |
114 | ||
115 | 29 | super(locale, pattern, locPattern); |
116 | 29 | } |
117 | ||
118 | /** | |
119 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
120 | * that will return the specified default value | |
121 | * if a conversion error occurs. The locale is the default locale for | |
122 | * this instance of the Java Virtual Machine and an unlocalized pattern is used | |
123 | * for the convertion. | |
124 | * | |
125 | * @param defaultValue The default value to be returned | |
126 | */ | |
127 | public BigDecimalLocaleConverter(Object defaultValue) { | |
128 | ||
129 | 1 | this(defaultValue, false); |
130 | 1 | } |
131 | ||
132 | /** | |
133 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
134 | * that will return the specified default value | |
135 | * if a conversion error occurs. The locale is the default locale for | |
136 | * this instance of the Java Virtual Machine. | |
137 | * | |
138 | * @param defaultValue The default value to be returned | |
139 | * @param locPattern Indicate whether the pattern is localized or not | |
140 | */ | |
141 | public BigDecimalLocaleConverter(Object defaultValue, boolean locPattern) { | |
142 | ||
143 | 2 | this(defaultValue, Locale.getDefault(), locPattern); |
144 | 2 | } |
145 | ||
146 | /** | |
147 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
148 | * that will return the specified default value | |
149 | * if a conversion error occurs. An unlocalized pattern is used for the convertion. | |
150 | * | |
151 | * @param defaultValue The default value to be returned | |
152 | * @param locale The locale | |
153 | */ | |
154 | public BigDecimalLocaleConverter(Object defaultValue, Locale locale) { | |
155 | ||
156 | 0 | this(defaultValue, locale, false); |
157 | 0 | } |
158 | ||
159 | /** | |
160 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
161 | * that will return the specified default value | |
162 | * if a conversion error occurs. | |
163 | * | |
164 | * @param defaultValue The default value to be returned | |
165 | * @param locale The locale | |
166 | * @param locPattern Indicate whether the pattern is localized or not | |
167 | */ | |
168 | public BigDecimalLocaleConverter(Object defaultValue, Locale locale, boolean locPattern) { | |
169 | ||
170 | 2 | this(defaultValue, locale, null, locPattern); |
171 | 2 | } |
172 | ||
173 | /** | |
174 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
175 | * that will return the specified default value | |
176 | * if a conversion error occurs. An unlocalized pattern is used for the convertion. | |
177 | * | |
178 | * @param defaultValue The default value to be returned | |
179 | * @param locale The locale | |
180 | * @param pattern The convertion pattern | |
181 | */ | |
182 | public BigDecimalLocaleConverter(Object defaultValue, Locale locale, String pattern) { | |
183 | ||
184 | 0 | this(defaultValue, locale, pattern, false); |
185 | 0 | } |
186 | ||
187 | /** | |
188 | * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} | |
189 | * that will return the specified default value | |
190 | * if a conversion error occurs. | |
191 | * | |
192 | * @param defaultValue The default value to be returned | |
193 | * @param locale The locale | |
194 | * @param pattern The convertion pattern | |
195 | * @param locPattern Indicate whether the pattern is localized or not | |
196 | */ | |
197 | public BigDecimalLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) { | |
198 | ||
199 | 4 | super(defaultValue, locale, pattern, locPattern); |
200 | 4 | } |
201 | ||
202 | /** | |
203 | * Convert the specified locale-sensitive input object into an output object of | |
204 | * BigDecimal type. | |
205 | * | |
206 | * @param value The input object to be converted | |
207 | * @param pattern The pattern is used for the convertion | |
208 | * @return The converted value | |
209 | * | |
210 | * @exception ConversionException if conversion cannot be performed | |
211 | * successfully | |
212 | * @throws ParseException if an error occurs parsing a String to a Number | |
213 | * @since 1.8.0 | |
214 | */ | |
215 | protected Object parse(Object value, String pattern) throws ParseException { | |
216 | ||
217 | 33 | Object result = super.parse(value, pattern); |
218 | ||
219 | 22 | if (result == null || result instanceof BigDecimal) { |
220 | 0 | return result; |
221 | } | |
222 | ||
223 | try { | |
224 | 22 | return new BigDecimal(result.toString()); |
225 | } | |
226 | 0 | catch (NumberFormatException ex) { |
227 | 0 | throw new ConversionException("Suplied number is not of type BigDecimal: " + result); |
228 | } | |
229 | ||
230 | } | |
231 | ||
232 | } |