1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package nl.openedge.baritus.converters;
33
34
35 import java.util.Locale;
36 import java.text.ParseException;
37
38 /***
39 * localized byte converter
40 * @author Eelco Hillenius
41 */
42 public class ByteLocaleConverter extends DecimalLocaleConverter
43 {
44
45
46
47 /***
48 * Create a {@link LocaleConverter}
49 * that will throw a {@link ConversionException}
50 * if a conversion error occurs. The locale is the default locale for
51 * this instance of the Java Virtual Machine and an unlocalized pattern is used
52 * for the convertion.
53 *
54 */
55 public ByteLocaleConverter()
56 {
57 this(Locale.getDefault());
58 }
59
60
61 /***
62 * Create a {@link LocaleConverter}
63 * that will throw a {@link ConversionException}
64 * if a conversion error occurs. An unlocalized pattern is used for the convertion.
65 *
66 * @param locale The locale
67 */
68 public ByteLocaleConverter(Locale locale)
69 {
70 this(locale, null);
71 }
72
73
74 /***
75 * Create a {@link LocaleConverter}
76 * that will throw a {@link ConversionException}
77 * if a conversion error occurs. An unlocalized pattern is used for the convertion.
78 *
79 * @param locale The locale
80 * @param pattern The convertion pattern
81 */
82 public ByteLocaleConverter(Locale locale, String pattern)
83 {
84
85 this(locale, pattern, false);
86 }
87
88 /***
89 * Create a {@link LocaleConverter}
90 * that will throw a {@link ConversionException}
91 * if a conversion error occurs.
92 *
93 * @param locale The locale
94 * @param pattern The convertion pattern
95 * @param locPattern Indicate whether the pattern is localized or not
96 */
97 public ByteLocaleConverter(
98 Locale locale,
99 String pattern,
100 boolean locPattern)
101 {
102
103 super(locale, pattern, locPattern);
104 }
105
106 /***
107 * Convert the specified locale-sensitive input object into an output object of the
108 * specified type. This method will return values of type Byte.
109 *
110 * @param value The input object to be converted
111 * @param pattern The pattern is used for the convertion
112 *
113 * @exception ConversionException if conversion cannot be performed
114 * successfully
115 */
116 protected Object parse(Object value, String pattern) throws ParseException
117 {
118 final Number parsed = (Number) super.parse(value, pattern);
119 if (parsed.longValue() != parsed.byteValue())
120 {
121 throw new ConversionException(
122 "Supplied number is not of type Byte: " + parsed.longValue());
123 }
124
125 return new Byte(parsed.byteValue());
126 }
127
128 /***
129 * Convert the specified locale-sensitive input object into an output object of the
130 * specified type.
131 *
132 * @param type Data type to which this value should be converted
133 * @param value The input object to be converted
134 * @param pattern The pattern is used for the convertion
135 *
136 * @exception ConversionException if conversion cannot be performed
137 * successfully
138 */
139 public Object convert(Class type, Object value, String pattern)
140 {
141 if (value == null)
142 {
143 return null;
144 }
145
146 Number temp = getNumber(value, pattern);
147
148 return (temp instanceof Byte) ? (Byte)temp : new Byte(temp.byteValue());
149 }
150 }