KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > typeconverter > IntegerArrayConverter


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 int[].
7  */

8 public class IntegerArrayConverter implements TypeConverter {
9
10     public static int[] valueOf(Object JavaDoc value) {
11
12         if (value == null) {
13             return null;
14         }
15
16         Class JavaDoc type = value.getClass();
17         if (type.isArray() == false) {
18             if (value instanceof Number JavaDoc) {
19                 return new int[] {((Number JavaDoc) value).intValue()};
20             }
21             try {
22                 return new int[] {Integer.parseInt(value.toString())};
23             } catch (NumberFormatException JavaDoc nfex) {
24                 throw new TypeConversionException(nfex);
25             }
26         }
27
28         if (type == int[].class) {
29             return (int[]) value;
30         }
31         if (type == long[].class) {
32             long[] values = (long[]) value;
33             int results[] = new int[values.length];
34             for (int i = 0; i < values.length; i++) {
35                 results[i] = (int) values[i];
36             }
37             return results;
38         }
39         if (type == double[].class) {
40             double[] values = (double[]) value;
41             int results[] = new int[values.length];
42             for (int i = 0; i < values.length; i++) {
43                 results[i] = (int) values[i];
44             }
45             return results;
46         }
47         if (type == byte[].class) {
48             byte[] values = (byte[]) value;
49             int results[] = new int[values.length];
50             for (int i = 0; i < values.length; i++) {
51                 results[i] = values[i];
52             }
53             return results;
54         }
55         if (type == float[].class) {
56             float[] values = (float[]) value;
57             int results[] = new int[values.length];
58             for (int i = 0; i < values.length; i++) {
59                 results[i] = (int) values[i];
60             }
61             return results;
62         }
63         if (type == boolean[].class) {
64             boolean[] values = (boolean[]) value;
65             int results[] = new int[values.length];
66             for (int i = 0; i < values.length; i++) {
67                 results[i] = (values[i] == true ? 1 : 0);
68             }
69             return results;
70         }
71         if (type == short[].class) {
72             short[] values = (short[]) value;
73             int results[] = new int[values.length];
74             for (int i = 0; i < values.length; i++) {
75                 results[i] = values[i];
76             }
77             return results;
78         }
79
80
81         Object JavaDoc[] values = (Object JavaDoc[]) value;
82         int[] results = new int[values.length];
83         try {
84             for (int i = 0; i < values.length; i++) {
85                 if (values[i] != null) {
86                     if (values[i] instanceof Number JavaDoc) {
87                         results[i] = ((Number JavaDoc) values[i]).intValue();
88                     } else {
89                         results[i] = Integer.parseInt(values[i].toString());
90                     }
91                 }
92             }
93         } catch (NumberFormatException JavaDoc nfex) {
94             throw new TypeConversionException(nfex);
95         }
96         return results;
97     }
98
99     public Object JavaDoc convert(Object JavaDoc value) {
100         return valueOf(value);
101     }
102 }
103
Popular Tags