Repository navigation
Expand file tree
/
Copy pathAbstractArray.java
More file actions
383 lines (307 loc) · 11.1 KB
/
Copy pathAbstractArray.java
File metadata and controls
383 lines (307 loc) · 11.1 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package org.javawebstack.abstractdata;
import org.javawebstack.abstractdata.collector.AbstractArrayCollector;
import org.javawebstack.abstractdata.exception.AbstractCoercingException;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Stream;
public class AbstractArray implements AbstractElement, Iterable<AbstractElement> {
private final List<AbstractElement> elements;
public boolean isArray() {
return true;
}
public AbstractArray array(boolean strict) throws AbstractCoercingException {
return this;
}
public AbstractObject object(boolean strict) throws AbstractCoercingException {
if(strict)
throw new AbstractCoercingException(Type.OBJECT, Type.ARRAY);
AbstractObject object = new AbstractObject();
for(int i=0; i< elements.size(); i++)
object.set(String.valueOf(i), elements.get(i));
return object;
}
public AbstractObject object(String key) throws AbstractCoercingException {
return query(key).object();
}
public AbstractArray array(String key) throws AbstractCoercingException {
return query(key).array();
}
public AbstractPrimitive primitive(String key) throws AbstractCoercingException {
return query(key).primitive();
}
public String string(String key) throws AbstractCoercingException {
return query(key).string();
}
public Boolean bool(String key) throws AbstractCoercingException {
return query(key).bool();
}
public Number number(String key) throws AbstractCoercingException {
return query(key).number();
}
public AbstractObject object(String key, AbstractObject orElse) throws AbstractCoercingException {
return query(key, orElse).object();
}
public AbstractArray array(String key, AbstractArray orElse) throws AbstractCoercingException {
return query(key, orElse).array();
}
public AbstractPrimitive primitive(String key, AbstractPrimitive orElse) throws AbstractCoercingException {
return query(key, orElse).primitive();
}
public String string(String key, String orElse) throws AbstractCoercingException {
return query(key, new AbstractPrimitive(orElse)).string();
}
public Boolean bool(String key, Boolean orElse) throws AbstractCoercingException {
return query(key, new AbstractPrimitive(orElse)).bool();
}
public Number number(String key, Number orElse) throws AbstractCoercingException {
return query(key, new AbstractPrimitive(orElse)).number();
}
public AbstractObject object(int index) throws AbstractCoercingException {
return get(index).object();
}
public AbstractArray array(int index) throws AbstractCoercingException {
return get(index).array();
}
public AbstractPrimitive primitive(int index) throws AbstractCoercingException {
return get(index).primitive();
}
public String string(int index) throws AbstractCoercingException {
return get(index).string();
}
public Boolean bool(int index) throws AbstractCoercingException {
return get(index).bool();
}
public Number number(int index) throws AbstractCoercingException {
return get(index).number();
}
public AbstractObject object(int index, AbstractObject orElse) throws AbstractCoercingException {
return get(index, orElse).object();
}
public AbstractArray array(int index, AbstractArray orElse) throws AbstractCoercingException {
return get(index, orElse).array();
}
public AbstractPrimitive primitive(int index, AbstractPrimitive orElse) throws AbstractCoercingException {
return get(index, orElse).primitive();
}
public String string(int index, String orElse) throws AbstractCoercingException {
return get(index, new AbstractPrimitive(orElse)).string();
}
public Boolean bool(int index, Boolean orElse) throws AbstractCoercingException {
return get(index, new AbstractPrimitive(orElse)).bool();
}
public Number number(int index, Number orElse) throws AbstractCoercingException {
return get(index, new AbstractPrimitive(orElse)).number();
}
public AbstractArray add(AbstractElement element) {
if (element == null)
element = AbstractNull.VALUE;
elements.add(element);
return this;
}
public AbstractArray() {
elements = new ArrayList<>();
}
public AbstractArray(Object[] objects) {
this();
for (Object o : objects) {
if (o instanceof AbstractElement)
elements.add((AbstractElement) o);
else
elements.add(AbstractElement.fromAbstractObject(o));
}
}
public AbstractArray(Collection<Object> abstractElements) {
this(abstractElements.toArray());
}
public AbstractArray addNull() {
return add(AbstractNull.VALUE);
}
public AbstractArray add(Number value) {
if (value == null)
return addNull();
return add(new AbstractPrimitive(value));
}
public AbstractArray add(Boolean value) {
if (value == null)
return addNull();
return add(new AbstractPrimitive(value));
}
public AbstractArray add(String value) {
if (value == null)
return addNull();
return add(new AbstractPrimitive(value));
}
public AbstractArray setNull(int i) {
return set(i, AbstractNull.VALUE);
}
public AbstractArray set(int i, AbstractElement element) {
if (element == null)
return setNull(i);
while (elements.size() <= i)
addNull();
elements.set(i, element);
return this;
}
public AbstractArray set(int i, Number value) {
if (value == null)
return setNull(i);
return set(i, new AbstractPrimitive(value));
}
public AbstractArray set(int i, Boolean value) {
if (value == null)
return setNull(i);
return set(i, new AbstractPrimitive(value));
}
public AbstractArray set(int i, String value) {
if (value == null)
return setNull(i);
return set(i, new AbstractPrimitive(value));
}
public AbstractArray remove(int i) {
elements.remove(i);
return this;
}
public AbstractElement[] toArray() {
return elements.toArray(new AbstractElement[0]);
}
public AbstractElement get(int i) {
return elements.get(i);
}
public AbstractElement get(int index, AbstractElement orElse) {
AbstractElement value = get(index);
return (index >= 0 && index < size() && !value.isNull()) ? value : orElse;
}
public AbstractElement query(String query) {
String[] q = query.split("\\.", 2);
try {
int index = Integer.parseInt(q[0]);
AbstractElement e = get(index);
if(e == null || q.length == 1)
return e;
if(e.isObject())
return e.object().query(q[1]);
if(e.isArray())
return e.array().query(q[1]);
return null;
} catch (NumberFormatException nfe) {
return null;
}
}
public AbstractElement query(String query, AbstractElement orElse) {
AbstractElement value = query(query);
return (value != null && !value.isNull()) ? value : orElse;
}
public Stream<AbstractElement> stream() {
return elements.stream();
}
public int size() {
return elements.size();
}
public boolean contains(Object o) {
for (AbstractElement element : elements) {
if (o instanceof AbstractElement ? ((AbstractElement) o).toObject().equals(element.toObject()) : element.toObject().equals(o))
return true;
}
return false;
}
public AbstractArray clear() {
elements.clear();
return this;
}
public Iterator<AbstractElement> iterator() {
return elements.iterator();
}
public Object toObject() {
List<Object> list = new ArrayList<>();
elements.forEach(e -> list.add(e.toObject()));
return list;
}
public List<String> toStringList() {
return toStringList(false);
}
public List<String> toStringList(boolean strict) {
List<String> list = new ArrayList<>();
for(AbstractElement e : elements)
list.add(e.string(strict));
return list;
}
public List<AbstractObject> toObjectList() {
return toObjectList(false);
}
public List<AbstractObject> toObjectList(boolean strict) {
List<AbstractObject> list = new ArrayList<>();
for(AbstractElement e : elements)
list.add(e.object(strict));
return list;
}
public static AbstractArray fromArray(Object[] objects) {
return new AbstractArray(objects);
}
public static AbstractArray fromList(Collection collection) {
return new AbstractArray(collection);
}
public Type getType() {
return Type.ARRAY;
}
public AbstractObject object() {
AbstractObject o = new AbstractObject();
for (int i = 0; i < size(); i++)
o.set(String.valueOf(i), get(i));
return o;
}
public Map<String[], Object> toTree() {
return object().toTree();
}
public Map<String, Object> toTree(String keySeparator) {
return object().toTree(keySeparator);
}
public AbstractArray use(Consumer<AbstractArray> consumer) {
consumer.accept(this);
return this;
}
public AbstractElement clone() {
AbstractArray array = new AbstractArray();
forEach(e -> array.add(e.clone()));
return array;
}
public static <T> Collector<T, ?, AbstractArray> collect(Function<T, AbstractElement> mappingFunction) {
return new AbstractArrayCollector<>(mappingFunction);
}
public static <T extends AbstractElement> Collector<T, ?, AbstractArray> collect() {
return new AbstractArrayCollector<>(e -> e);
}
public boolean equals(Object obj, boolean strict) {
if (obj == null)
return false;
if (!(obj instanceof AbstractElement))
return false;
try {
AbstractArray arr = ((AbstractElement) obj).array(strict);
if (arr.size() != arr.size())
return false;
for (int i = 0; i < size(); i++) {
if (!(get(i).equals(arr.get(i), strict)))
return false;
}
return true;
} catch (AbstractCoercingException ignored) {
return false;
}
}
public boolean equals (Object obj) {
return equals(obj, false);
}
public AbstractArray addAll(AbstractArray array) {
elements.addAll(array.elements);
return this;
}
public AbstractArray concat(AbstractArray... array) {
AbstractArray newArr = new AbstractArray();
newArr.addAll(this);
for (AbstractArray arr : array)
newArr.addAll(arr);
return newArr;
}
}