View Javadoc

1   /*
2    * $Id: ByteLocaleConverter.java,v 1.2 2004/04/09 18:44:53 eelco12 Exp $
3    * $Revision: 1.2 $
4    * $Date: 2004/04/09 18:44:53 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
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  	// ----------------------------------------------------------- Constructors
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 		// now return property Byte
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 }