2
2
3
3
import java .lang .reflect .Array ;
4
4
import java .lang .reflect .Constructor ;
5
+ import java .util .HashMap ;
5
6
import java .util .Map ;
6
7
7
- import org .apache .commons .lang .StringUtils ;
8
-
9
8
import com .google .common .primitives .Primitives ;
10
9
11
- public class TypeCaster {
10
+ public final class TypeCaster {
11
+
12
+ private static final Map <String , String > types = new HashMap <String , String >();
13
+
14
+ static {
15
+ try {
16
+ putNumberType (Boolean .class );
17
+ putNumberType (Character .class );
18
+ putNumberType (Byte .class );
19
+ putNumberType (Short .class );
20
+ putNumberType (Integer .class );
21
+ putNumberType (Long .class );
22
+ putNumberType (Float .class );
23
+ putNumberType (Double .class );
24
+ } catch (final Exception e ) {
25
+ throw new IllegalStateException (e );
26
+ }
27
+ }
28
+
29
+ private static void putNumberType (final Class <?> primitiveClass ) throws IllegalArgumentException , SecurityException , IllegalAccessException , NoSuchFieldException {
30
+ types .put (primitiveClass .getField ("TYPE" ).get (null ).toString (), primitiveClass .getName ());
31
+ }
12
32
13
33
private TypeCaster () {
14
34
super ();
@@ -28,7 +48,8 @@ public static String getStringType(final Object object) {
28
48
} else if (object instanceof Boolean ) {
29
49
return GraphMLTokens .BOOLEAN ;
30
50
} else if (isArray (object )) {
31
- return object .getClass ().getSimpleName ();
51
+ final Object first = Array .get (object , 0 );
52
+ return getStringType (first ) + "[]" ;
32
53
} else {
33
54
return GraphMLTokens .STRING ;
34
55
}
@@ -37,45 +58,46 @@ public static String getStringType(final Object object) {
37
58
public static boolean isArray (Object obj ) {
38
59
return obj !=null && obj .getClass ().isArray ();
39
60
}
40
-
61
+
41
62
public static Object typeCastValue (String key , String value , Map <String , String > keyTypes ) {
42
63
String type = keyTypes .get (key );
43
- if (null == type || type .equals (GraphMLTokens .STRING ))
64
+ if (null == type || type .equals (GraphMLTokens .STRING )) {
44
65
return value ;
45
- else if (type .equals (GraphMLTokens .FLOAT ))
66
+ } else if (type .equals (GraphMLTokens .FLOAT )) {
46
67
return Float .valueOf (value );
47
- else if (type .equals (GraphMLTokens .INT ))
68
+ } else if (type .equals (GraphMLTokens .INT )) {
48
69
return Integer .valueOf (value );
49
- else if (type .equals (GraphMLTokens .DOUBLE ))
70
+ } else if (type .equals (GraphMLTokens .DOUBLE )) {
50
71
return Double .valueOf (value );
51
- else if (type .equals (GraphMLTokens .BOOLEAN ))
72
+ } else if (type .equals (GraphMLTokens .BOOLEAN )) {
52
73
return Boolean .valueOf (value );
53
- else if (type .equals (GraphMLTokens .LONG ))
74
+ } else if (type .equals (GraphMLTokens .LONG )) {
54
75
return Long .valueOf (value );
55
- else if (type .contains ("[]" ))
56
- return castToArray (type , value );
57
- else
76
+ } else if (type .contains ("[]" )) {
77
+ return castToArray (type , value );
78
+ } else {
58
79
return value ;
80
+ }
59
81
}
60
82
61
83
/**
62
- * Creates a primitive array of the specified type (eg. long[] or int[])
63
- * Using 'value' as a comma-delimited String.
64
- */
65
- static Object castToArray (final String type , final String value ) {
66
- try {
67
- final String [] values = value .split ("," );
68
- final String className = "java.lang." + StringUtils . capitalize (type .replace ("[]" , "" ));
69
- final Class <?> klass = Class .forName (className );
70
- final Constructor <?> konstructor = klass .getDeclaredConstructor (String .class );
71
- final Object array = Array .newInstance (Primitives .unwrap (klass ), values .length );
72
- for (int i = 0 ; i < values .length ; i ++) {
73
- Array .set (array , i , konstructor .newInstance (values [i ].trim ()));
74
- }
75
- return array ;
76
- } catch (final Exception e ) {
77
- throw new IllegalStateException ("Could not cast " + value + " to " + type , e );
78
- }
79
- }
80
-
84
+ * Creates a primitive array of the specified type (eg. long[] or int[])
85
+ * Using 'value' as a comma-delimited String.
86
+ */
87
+ static Object castToArray (final String type , final String value ) {
88
+ try {
89
+ final String [] values = value .split ("," );
90
+ final String className = types . get (type .replace ("[]" , "" ));
91
+ final Class <?> klass = Class .forName (className );
92
+ final Constructor <?> konstructor = klass .getDeclaredConstructor (String .class );
93
+ final Object array = Array .newInstance (Primitives .unwrap (klass ), values .length );
94
+ for (int i = 0 ; i < values .length ; i ++) {
95
+ Array .set (array , i , konstructor .newInstance (values [i ].trim ()));
96
+ }
97
+ return array ;
98
+ } catch (final Exception e ) {
99
+ throw new IllegalStateException ("Could not cast " + value + " to " + type , e );
100
+ }
101
+ }
102
+
81
103
}
0 commit comments