KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > typeconverter > ByteConverter


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.typeconverter;
4
5 /**
6  * Converts given object to Byte. Given object (if not already instance of
7  * Byte) is first converted to String and then analyzed.
8  */

9 public class ByteConverter implements TypeConverter {
10
11     public static Byte JavaDoc valueOf(Object JavaDoc value) {
12         if (value == null) {
13             return null;
14         }
15         if (value instanceof Byte JavaDoc) {
16             return (Byte JavaDoc) value;
17         }
18         if (value instanceof Number JavaDoc) {
19             return new Byte JavaDoc(((Number JavaDoc)value).byteValue());
20         }
21         try {
22             return (new Byte JavaDoc(value.toString()));
23         } catch (NumberFormatException JavaDoc nfex) {
24             throw new TypeConversionException(nfex);
25         }
26     }
27
28     public Object JavaDoc convert(Object JavaDoc value) {
29         return valueOf(value);
30     }
31 }
32
Popular Tags