1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils.locale.converters;
18
19 import org.apache.commons.beanutils.ConversionException;
20
21 import java.util.Locale;
22 import java.text.ParseException;
23
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.lang.Integer</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 IntegerLocaleConverter extends DecimalLocaleConverter {
37
38
39
40
41 /**
42 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
43 * that will throw a {@link org.apache.commons.beanutils.ConversionException}
44 * if a conversion error occurs. The locale is the default locale for
45 * this instance of the Java Virtual Machine and an unlocalized pattern is used
46 * for the convertion.
47 *
48 */
49
50 public IntegerLocaleConverter() {
51
52 this(false);
53 }
54
55 /**
56 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
57 * that will throw a {@link org.apache.commons.beanutils.ConversionException}
58 * if a conversion error occurs. The locale is the default locale for
59 * this instance of the Java Virtual Machine.
60 *
61 * @param locPattern Indicate whether the pattern is localized or not
62 */
63 public IntegerLocaleConverter(boolean locPattern) {
64
65 this(Locale.getDefault(), locPattern);
66 }
67
68 /**
69 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
70 * that will throw a {@link org.apache.commons.beanutils.ConversionException}
71 * if a conversion error occurs. An unlocalized pattern is used for the convertion.
72 *
73 * @param locale The locale
74 */
75 public IntegerLocaleConverter(Locale locale) {
76
77 this(locale, false);
78 }
79
80 /**
81 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
82 * that will throw a {@link org.apache.commons.beanutils.ConversionException}
83 * if a conversion error occurs.
84 *
85 * @param locale The locale
86 * @param locPattern Indicate whether the pattern is localized or not
87 */
88 public IntegerLocaleConverter(Locale locale, boolean locPattern) {
89
90 this(locale, (String) null, locPattern);
91 }
92
93 /**
94 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
95 * that will throw a {@link org.apache.commons.beanutils.ConversionException}
96 * if a conversion error occurs. An unlocalized pattern is used for the convertion.
97 *
98 * @param locale The locale
99 * @param pattern The convertion pattern
100 */
101 public IntegerLocaleConverter(Locale locale, String pattern) {
102
103 this(locale, pattern, false);
104 }
105
106 /**
107 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
108 * that will throw a {@link org.apache.commons.beanutils.ConversionException}
109 * if a conversion error occurs.
110 *
111 * @param locale The locale
112 * @param pattern The convertion pattern
113 * @param locPattern Indicate whether the pattern is localized or not
114 */
115 public IntegerLocaleConverter(Locale locale, String pattern, boolean locPattern) {
116
117 super(locale, pattern, locPattern);
118 }
119
120 /**
121 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
122 * that will return the specified default value
123 * if a conversion error occurs. The locale is the default locale for
124 * this instance of the Java Virtual Machine and an unlocalized pattern is used
125 * for the convertion.
126 *
127 * @param defaultValue The default value to be returned
128 */
129 public IntegerLocaleConverter(Object defaultValue) {
130
131 this(defaultValue, false);
132 }
133
134 /**
135 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
136 * that will return the specified default value
137 * if a conversion error occurs. The locale is the default locale for
138 * this instance of the Java Virtual Machine.
139 *
140 * @param defaultValue The default value to be returned
141 * @param locPattern Indicate whether the pattern is localized or not
142 */
143 public IntegerLocaleConverter(Object defaultValue, boolean locPattern) {
144
145 this(defaultValue, Locale.getDefault(), locPattern);
146 }
147
148 /**
149 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
150 * that will return the specified default value
151 * if a conversion error occurs. An unlocalized pattern is used for the convertion.
152 *
153 * @param defaultValue The default value to be returned
154 * @param locale The locale
155 */
156 public IntegerLocaleConverter(Object defaultValue, Locale locale) {
157
158 this(defaultValue, locale, false);
159 }
160
161 /**
162 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
163 * that will return the specified default value
164 * if a conversion error occurs.
165 *
166 * @param defaultValue The default value to be returned
167 * @param locale The locale
168 * @param locPattern Indicate whether the pattern is localized or not
169 */
170 public IntegerLocaleConverter(Object defaultValue, Locale locale, boolean locPattern) {
171
172 this(defaultValue, locale, null, locPattern);
173 }
174
175 /**
176 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
177 * that will return the specified default value
178 * if a conversion error occurs. An unlocalized pattern is used for the convertion.
179 *
180 * @param defaultValue The default value to be returned
181 * @param locale The locale
182 * @param pattern The convertion pattern
183 */
184 public IntegerLocaleConverter(Object defaultValue, Locale locale, String pattern) {
185
186 this(defaultValue, locale, pattern, false);
187 }
188
189 /**
190 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
191 * that will return the specified default value
192 * if a conversion error occurs.
193 *
194 * @param defaultValue The default value to be returned
195 * @param locale The locale
196 * @param pattern The convertion pattern
197 * @param locPattern Indicate whether the pattern is localized or not
198 */
199 public IntegerLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) {
200
201 super(defaultValue, locale, pattern, locPattern);
202 }
203
204 /**
205 * Convert the specified locale-sensitive input object into an output object of the
206 * specified type. This method will return Integer type.
207 *
208 * @param value The input object to be converted
209 * @param pattern The pattern is used for the convertion
210 *
211 * @exception ConversionException if conversion cannot be performed
212 * successfully
213 */
214 protected Object parse(Object value, String pattern) throws ParseException {
215 final Number parsed = (Number) super.parse(value, pattern);
216 if (parsed.longValue() != parsed.intValue()) {
217 throw new ConversionException("Suplied number is not of type Integer: " + parsed.longValue());
218 }
219 return new Integer(parsed.intValue());
220 }
221 }