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.Byte</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 ByteLocaleConverter 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      public ByteLocaleConverter() {
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 ByteLocaleConverter(final 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 ByteLocaleConverter(final 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 ByteLocaleConverter(final Locale locale, final 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 ByteLocaleConverter(final Locale locale, final 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 ByteLocaleConverter(final Locale locale, final String pattern, final 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 ByteLocaleConverter(final 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 ByteLocaleConverter(final Object defaultValue, final 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 ByteLocaleConverter(final Object defaultValue, final 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 ByteLocaleConverter(final Object defaultValue, final Locale locale, final 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 ByteLocaleConverter(final Object defaultValue, final Locale locale, final 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 ByteLocaleConverter(final Object defaultValue, final Locale locale, final String pattern, final 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 values of type Byte.
207      *
208      * @param value The input object to be converted
209      * @param pattern The pattern is used for the convertion
210      * @return The converted value
211      *
212      * @throws org.apache.commons.beanutils.ConversionException if conversion cannot be performed
213      *  successfully
214      * @throws ParseException if an error occurs parsing a String to a Number
215      */
216     @Override
217     protected Object parse(final Object value, final String pattern) throws ParseException {
218         final Number parsed = (Number) super.parse(value, pattern);
219         if (parsed.longValue() != parsed.byteValue()) {
220             throw new ConversionException("Supplied number is not of type Byte: " + parsed.longValue());
221         }
222         // now returns property Byte
223         return new Byte(parsed.byteValue());
224     }
225 }