forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAny.java
More file actions
243 lines (217 loc) · 7.07 KB
/
Any.java
File metadata and controls
243 lines (217 loc) · 7.07 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.jsoniter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class Any {
private final Object val;
public Object lastAccessed;
public Any(Object val) {
this.val = val;
}
public ValueType getValueType(Object... keys) {
try {
lastAccessed = getPath(val, keys);
if (lastAccessed == null) {
return ValueType.NULL;
}
Class<?> clazz = lastAccessed.getClass();
if (clazz == String.class) {
return ValueType.STRING;
}
if (clazz.isArray()) {
return ValueType.ARRAY;
}
if (lastAccessed instanceof Number) {
return ValueType.NUMBER;
}
if (lastAccessed instanceof List) {
return ValueType.ARRAY;
}
return ValueType.OBJECT;
} catch (ClassCastException e) {
return ValueType.INVALID;
} catch (IndexOutOfBoundsException e) {
return ValueType.INVALID;
}
}
public Map<String, Object> getMap(Object... keys) {
return get(keys);
}
public List<Object> getList(Object... keys) {
return get(keys);
}
public <T> T get(Object... keys) {
try {
return (T) (lastAccessed = getPath(val, keys));
} catch (ClassCastException e) {
return null;
} catch (IndexOutOfBoundsException e) {
return null;
}
}
public boolean exists(Object... keys) {
try {
lastAccessed = getPath(val, keys);
return true;
} catch (ClassCastException e) {
return false;
} catch (IndexOutOfBoundsException e) {
return false;
}
}
public String toString() {
return toString(new Object[0]);
}
public String toString(Object... keys) {
get(keys);
if (lastAccessed == null) {
return "null";
}
return lastAccessed.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Any any = (Any) o;
return val != null ? val.equals(any.val) : any.val == null;
}
@Override
public int hashCode() {
return val != null ? val.hashCode() : 0;
}
public int toInt(Object... keys) {
get(keys);
if (lastAccessed == null) {
return 0;
}
if (lastAccessed.getClass() == String.class) {
return Integer.valueOf((String) lastAccessed);
}
Number number = (Number) lastAccessed;
return number.intValue();
}
public short toShort(Object... keys) {
get(keys);
if (lastAccessed == null) {
return 0;
}
if (lastAccessed.getClass() == String.class) {
return Short.valueOf((String) lastAccessed);
}
Number number = (Number) lastAccessed;
return number.shortValue();
}
public long toLong(Object... keys) {
get(keys);
if (lastAccessed == null) {
return 0;
}
if (lastAccessed.getClass() == String.class) {
return Long.valueOf((String) lastAccessed);
}
Number number = (Number) lastAccessed;
return number.longValue();
}
public float toFloat(Object... keys) {
get(keys);
if (lastAccessed == null) {
return 0;
}
if (lastAccessed.getClass() == String.class) {
return Float.valueOf((String) lastAccessed);
}
Number number = (Number) lastAccessed;
return number.floatValue();
}
public double toDouble(Object... keys) {
get(keys);
if (lastAccessed == null) {
return 0;
}
if (lastAccessed.getClass() == String.class) {
return Double.valueOf((String) lastAccessed);
}
Number number = (Number) lastAccessed;
return number.doubleValue();
}
public boolean toBoolean(Object... keys) {
get(keys);
if (lastAccessed == null) {
return false;
}
if (lastAccessed instanceof Number) {
Number number = (Number) lastAccessed;
return number.intValue() != 0;
}
if (lastAccessed.getClass().isArray()) {
return Array.getLength(lastAccessed) != 0;
}
if (lastAccessed instanceof Collection) {
Collection col = (Collection) lastAccessed;
return col.size() != 0;
}
if (lastAccessed instanceof Map) {
Map map = (Map) lastAccessed;
return map.size() != 0;
}
return true;
}
private static Object getPath(Object val, Object... keys) {
if (keys.length == 0) {
return val;
}
Object key = keys[0];
if ("*".equals(key)) {
if (val.getClass().isArray()) {
ArrayList result = new ArrayList(Array.getLength(val));
for (int i = 0; i < Array.getLength(val); i++) {
Object nextVal = Array.get(val, i);
Object[] nextKeys = new Object[keys.length - 1];
System.arraycopy(keys, 1, nextKeys, 0, nextKeys.length);
result.add(getPath(nextVal, nextKeys));
}
return result;
} else {
List list = (List) val;
ArrayList result = new ArrayList(list.size());
for (Object e : list) {
Object nextVal = e;
Object[] nextKeys = new Object[keys.length - 1];
System.arraycopy(keys, 1, nextKeys, 0, nextKeys.length);
result.add(getPath(nextVal, nextKeys));
}
return result;
}
}
if (key instanceof Integer) {
Object nextVal = getFromArrayOrList(val, (Integer) key);
Object[] nextKeys = new Object[keys.length - 1];
System.arraycopy(keys, 1, nextKeys, 0, nextKeys.length);
return getPath(nextVal, nextKeys);
}
if (key instanceof String) {
Object nextVal = getFromMap(val, (String) key);
Object[] nextKeys = new Object[keys.length - 1];
System.arraycopy(keys, 1, nextKeys, 0, nextKeys.length);
return getPath(nextVal, nextKeys);
}
throw new RuntimeException("invalid key type: " + key);
}
private static Object getFromMap(Object val, String key) {
Map map = (Map) val;
if (!map.containsKey(key)) {
throw new IndexOutOfBoundsException(key + " not in " + map);
}
return map.get(key);
}
private static Object getFromArrayOrList(Object val, Integer key) {
if (val.getClass().isArray()) {
return Array.get(val, key);
}
List list = (List) val;
return list.get(key);
}
}