1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.util; |
17 | |
|
18 | |
|
19 | |
import java.lang.reflect.Array; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public final class ArrayUtils { |
28 | |
|
29 | 0 | private ArrayUtils() { |
30 | 0 | throw new UnsupportedOperationException("do not call"); |
31 | |
} |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public static void setArrayValue(Object array, Class type, Object value, int index) { |
41 | 0 | if (!type.isPrimitive()) |
42 | 0 | Array.set(array, index, value); |
43 | 0 | else if (type.isAssignableFrom(Boolean.TYPE)) |
44 | 0 | Array.setBoolean(array, index, (Boolean) value); |
45 | 0 | else if (type.isAssignableFrom(Character.TYPE)) |
46 | 0 | Array.setChar(array, index, (Character) value); |
47 | 0 | else if (type.isAssignableFrom(Byte.TYPE)) |
48 | 0 | Array.setByte(array, index, (Byte) value); |
49 | 0 | else if (type.isAssignableFrom(Integer.TYPE)) |
50 | 0 | Array.setInt(array, index, (Integer) value); |
51 | 0 | else if (type.isAssignableFrom(Short.TYPE)) |
52 | 0 | Array.setShort(array, index, (Short) value); |
53 | 0 | else if (type.isAssignableFrom(Long.TYPE)) |
54 | 0 | Array.setLong(array, index, (Long) value); |
55 | 0 | else if (type.isAssignableFrom(Float.TYPE)) |
56 | 0 | Array.setFloat(array, index, (Float) value); |
57 | 0 | else if (type.isAssignableFrom(Double.TYPE)) |
58 | 0 | Array.setDouble(array, index, (Double) value); |
59 | 0 | } |
60 | |
|
61 | |
public static Object toObject(Object value) { |
62 | 0 | if (!value.getClass().isArray()) |
63 | 0 | return value; |
64 | |
|
65 | 0 | Class type = value.getClass().getComponentType(); |
66 | 0 | if (Array.getLength(value) == 0) |
67 | 0 | return null; |
68 | 0 | if (!type.isPrimitive()) |
69 | 0 | return Array.get(value, 0); |
70 | 0 | if (boolean.class.isAssignableFrom(type)) |
71 | 0 | return Array.getBoolean(value, 0); |
72 | 0 | if (char.class.isAssignableFrom(type)) |
73 | 0 | return new Character(Array.getChar(value, 0)); |
74 | 0 | if (byte.class.isAssignableFrom(type)) |
75 | 0 | return new Byte(Array.getByte(value, 0)); |
76 | 0 | if (int.class.isAssignableFrom(type)) |
77 | 0 | return new Integer(Array.getInt(value, 0)); |
78 | 0 | if (long.class.isAssignableFrom(type)) |
79 | 0 | return new Long(Array.getLong(value, 0)); |
80 | 0 | if (short.class.isAssignableFrom(type)) |
81 | 0 | return new Short(Array.getShort(value, 0)); |
82 | 0 | if (double.class.isAssignableFrom(type)) |
83 | 0 | return new Double(Array.getDouble(value, 0)); |
84 | 0 | if (float.class.isAssignableFrom(type)) |
85 | 0 | return new Float(Array.getFloat(value, 0)); |
86 | |
|
87 | 0 | return null; |
88 | |
} |
89 | |
|
90 | |
public static Object toString(Object array, Class type) { |
91 | 0 | if (boolean.class.isAssignableFrom(type)) |
92 | 0 | return Boolean.toString(((boolean[]) array)[0]); |
93 | 0 | if (char.class.isAssignableFrom(type)) |
94 | 0 | return Character.toString(((char[]) array)[0]); |
95 | 0 | if (byte.class.isAssignableFrom(type)) |
96 | 0 | return Byte.toString(((byte[]) array)[0]); |
97 | 0 | if (int.class.isAssignableFrom(type)) |
98 | 0 | return Integer.toString(((int[]) array)[0]); |
99 | 0 | if (long.class.isAssignableFrom(type)) |
100 | 0 | return Long.toString(((long[]) array)[0]); |
101 | 0 | if (short.class.isAssignableFrom(type)) |
102 | 0 | return Short.toString(((short[]) array)[0]); |
103 | 0 | if (double.class.isAssignableFrom(type)) |
104 | 0 | return Double.toString(((double[]) array)[0]); |
105 | 0 | if (float.class.isAssignableFrom(type)) |
106 | 0 | return Float.toString(((float[]) array)[0]); |
107 | |
|
108 | 0 | return ((String[]) array)[0]; |
109 | |
} |
110 | |
} |