KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > typeconverter > ShortConverter


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 Short. Given object (if not already instance of
7  * Short) is first converted to String and then analyzed.
8  */

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