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