forked from jonfhancock/JsonToJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeUtils.java
More file actions
100 lines (82 loc) · 2.58 KB
/
Copy pathTypeUtils.java
File metadata and controls
100 lines (82 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.jsontojava;
import java.util.Arrays;
public class TypeUtils {
public static final String PRIMITIVE_LONG = "long";
public static final String PRIMITIVE_DOUBLE = "double";
public static final String PRIMITIVE_INT = "int";
public static final String PRIMITIVE_BOOLEAN = "boolean";
public static final String TYPE_LONG = "Long";
public static final String TYPE_STRING = "String";
public static final String TYPE_DOUBLE = "Double";
public static final String TYPE_INTEGER = "Integer";
public static final String TYPE_BOOLEAN = "Boolean";
public static final String TYPE_NULL = "Null";
public static final String[] PRIMITIVE_TYPES = new String[]{PRIMITIVE_LONG,PRIMITIVE_DOUBLE,PRIMITIVE_INT,PRIMITIVE_BOOLEAN,TYPE_LONG,TYPE_STRING,TYPE_DOUBLE,TYPE_INTEGER,TYPE_BOOLEAN};
public static boolean isPrimitiveType(Object current) {
String clazz = current.getClass().getSimpleName();
return isPrimitiveType(clazz);
}
public static boolean isPrimitiveType(String clazz) {
return Arrays.asList(PRIMITIVE_TYPES).contains(clazz);
}
public static String getPrimitiveClassType(Object current) {
String clazz = current.getClass().getSimpleName();
if (clazz.equals(TYPE_BOOLEAN)) {
clazz = TYPE_BOOLEAN;
}
if (clazz.equals(TYPE_INTEGER)) {
clazz = TYPE_INTEGER;
}
if (clazz.equals(TYPE_DOUBLE)) {
clazz = TYPE_DOUBLE;
}
if (clazz.equals(TYPE_STRING)) {
if(((String) current).matches("^[0-9]+(\\.[0-9]+)?$")){
try {
long l = Long.parseLong((String) current);
clazz = TYPE_LONG;
if (Math.abs(l) < Integer.MAX_VALUE / 2) {
clazz = TYPE_INTEGER;
}
} catch (NumberFormatException e) {
try {
Double.parseDouble((String) current);
clazz = TYPE_DOUBLE;
} catch (NumberFormatException e2) {
}
}
}
}
return clazz;
}
public static String getPrimitiveType(Object current) {
String clazz = current.getClass().getSimpleName();
if (clazz.equals(TYPE_BOOLEAN)) {
clazz = PRIMITIVE_BOOLEAN;
}
if (clazz.equals(TYPE_INTEGER)) {
clazz = PRIMITIVE_INT;
}
if (clazz.equals(TYPE_DOUBLE)) {
clazz = PRIMITIVE_DOUBLE;
}
if (clazz.equals(TYPE_STRING)) {
if(((String) current).matches("^[0-9]+(\\.[0-9]+)?$")){
try {
long l = Long.parseLong((String) current);
clazz = PRIMITIVE_LONG;
if (Math.abs(l) < Integer.MAX_VALUE / 2) {
clazz = PRIMITIVE_INT;
}
} catch (NumberFormatException e) {
try {
Double.parseDouble((String) current);
clazz = PRIMITIVE_DOUBLE;
} catch (NumberFormatException e2) {
}
}
}
}
return clazz;
}
}