001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils.locale.converters;
019:
020: import org.apache.commons.beanutils.ConversionException;
021:
022: import java.util.Locale;
023: import java.text.ParseException;
024:
025: /**
026: * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
027: * implementation that converts an incoming
028: * locale-sensitive String into a <code>java.lang.Integer</code> object,
029: * optionally using a default value or throwing a
030: * {@link org.apache.commons.beanutils.ConversionException}
031: * if a conversion error occurs.</p>
032: *
033: * @author Yauheny Mikulski
034: */
035:
036: public class IntegerLocaleConverter extends DecimalLocaleConverter {
037:
038: // ----------------------------------------------------------- Constructors
039:
040: /**
041: * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
042: * that will throw a {@link org.apache.commons.beanutils.ConversionException}
043: * if a conversion error occurs. The locale is the default locale for
044: * this instance of the Java Virtual Machine and an unlocalized pattern is used
045: * for the convertion.
046: *
047: */
048:
049: public IntegerLocaleConverter() {
050:
051: this (false);
052: }
053:
054: /**
055: * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
056: * that will throw a {@link org.apache.commons.beanutils.ConversionException}
057: * if a conversion error occurs. The locale is the default locale for
058: * this instance of the Java Virtual Machine.
059: *
060: * @param locPattern Indicate whether the pattern is localized or not
061: */
062: public IntegerLocaleConverter(boolean locPattern) {
063:
064: this (Locale.getDefault(), locPattern);
065: }
066:
067: /**
068: * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
069: * that will throw a {@link org.apache.commons.beanutils.ConversionException}
070: * if a conversion error occurs. An unlocalized pattern is used for the convertion.
071: *
072: * @param locale The locale
073: */
074: public IntegerLocaleConverter(Locale locale) {
075:
076: this (locale, false);
077: }
078:
079: /**
080: * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
081: * that will throw a {@link org.apache.commons.beanutils.ConversionException}
082: * if a conversion error occurs.
083: *
084: * @param locale The locale
085: * @param locPattern Indicate whether the pattern is localized or not
086: */
087: public IntegerLocaleConverter(Locale locale, boolean locPattern) {
088:
089: this (locale, (String) null, locPattern);
090: }
091:
092: /**
093: * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
094: * that will throw a {@link org.apache.commons.beanutils.ConversionException}
095: * if a conversion error occurs. An unlocalized pattern is used for the convertion.
096: *
097: * @param locale The locale
098: * @param pattern The convertion pattern
099: */
100: public IntegerLocaleConverter(Locale locale, String pattern) {
101:
102: this (locale, pattern, false);
103: }
104:
105: /**
106: * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
107: * that will throw a {@link org.apache.commons.beanutils.ConversionException}
108: * if a conversion error occurs.
109: *
110: * @param locale The locale
111: * @param pattern The convertion pattern
112: * @param locPattern Indicate whether the pattern is localized or not
113: */
114: public IntegerLocaleConverter(Locale locale, String pattern,
115: 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,
144: 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(Object defaultValue, 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(Object defaultValue, Locale locale,
172: boolean locPattern) {
173:
174: this (defaultValue, locale, null, locPattern);
175: }
176:
177: /**
178: * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
179: * that will return the specified default value
180: * if a conversion error occurs. An unlocalized pattern is used for the convertion.
181: *
182: * @param defaultValue The default value to be returned
183: * @param locale The locale
184: * @param pattern The convertion pattern
185: */
186: public IntegerLocaleConverter(Object defaultValue, Locale locale,
187: String pattern) {
188:
189: this (defaultValue, locale, pattern, false);
190: }
191:
192: /**
193: * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
194: * that will return the specified default value
195: * if a conversion error occurs.
196: *
197: * @param defaultValue The default value to be returned
198: * @param locale The locale
199: * @param pattern The convertion pattern
200: * @param locPattern Indicate whether the pattern is localized or not
201: */
202: public IntegerLocaleConverter(Object defaultValue, Locale locale,
203: String pattern, boolean locPattern) {
204:
205: super (defaultValue, locale, pattern, locPattern);
206: }
207:
208: /**
209: * Convert the specified locale-sensitive input object into an output object of the
210: * specified type. This method will return Integer type.
211: *
212: * @param value The input object to be converted
213: * @param pattern The pattern is used for the convertion
214: * @return The converted value
215: *
216: * @exception ConversionException if conversion cannot be performed
217: * successfully
218: * @throws ParseException if an error occurs parsing a String to a Number
219: */
220: protected Object parse(Object value, String pattern)
221: throws ParseException {
222: final Number parsed = (Number) super .parse(value, pattern);
223: if (parsed.longValue() != parsed.intValue()) {
224: throw new ConversionException(
225: "Suplied number is not of type Integer: "
226: + parsed.longValue());
227: }
228: return new Integer(parsed.intValue()); // unlike superclass it will return proper Integer
229: }
230: }
|