KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > typeconverter > IntegerConverter


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 an Integer.
7  */

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