KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > typeconverter > ShortArrayConverter


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

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