-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJson5Array.java
More file actions
484 lines (440 loc) · 17.5 KB
/
Copy pathJson5Array.java
File metadata and controls
484 lines (440 loc) · 17.5 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* Copyright (C) 2008 Google Inc.
* Copyright (C) 2022 - 2025 Marcel Haßlinger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.marhali.json5;
import de.marhali.json5.internal.NonNullElementWrapperList;
import de.marhali.json5.internal.RadixNumber;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
/**
* A class representing an array type in Json5. An array is a list of {@link Json5Element}s each of
* which can be of a different type. This is an ordered list, meaning that the order in which
* elements are added is preserved. This class does not support {@code null} values. If {@code null}
* is provided as element argument to any of the methods, it is converted to a {@link Json5Null}.
*
* <p>{@code Json5Array} only implements the {@link Iterable} interface but not the {@link List}
* interface. A {@code List} view of it can be obtained with {@link #asList()}.
*
* <p>See the {@link Json5} documentation for details on how to convert {@code Json5Array} and
* generally any {@code Json5Element} from and to Json5.
*
* @author Inderjeet Singh
* @author Joel Leitch
* @author Marcel Haßlinger
*/
public final class Json5Array extends Json5Element implements Iterable<Json5Element> {
private final ArrayList<Json5Element> elements;
/**
* Creates an empty Json5Array.
*/
public Json5Array() {
elements = new ArrayList<>();
}
/**
* Creates an empty Json5Array with the desired initial capacity.
*
* @param capacity initial capacity.
* @throws IllegalArgumentException if the {@code capacity} is negative
*/
public Json5Array(int capacity) {
elements = new ArrayList<>(capacity);
}
/**
* Creates a deep copy of this element and all its children.
*/
@Override
public Json5Array deepCopy() {
Json5Array result = new Json5Array(elements.size());
for (Json5Element element : elements) {
result.add(element.deepCopy());
}
result.setComment(comment);
return result;
}
/**
* Adds the specified {@link Instant} to self.
*
* @param instant the {@link Instant} that needs to be added to the array.
*/
public void add(Instant instant) {
elements.add(instant == null ? Json5Primitive.fromNull() : Json5Primitive.fromInstant(instant));
}
/**
* Adds the specified boolean to self.
*
* @param bool the boolean that needs to be added to the array.
*/
public void add(Boolean bool) {
elements.add(bool == null ? Json5Primitive.fromNull() : Json5Primitive.fromBoolean(bool));
}
/**
* Adds the specified character to self.
*
* @param character the character that needs to be added to the array.
*/
public void add(Character character) {
elements.add(character == null ? Json5Primitive.fromNull() : Json5Primitive.fromCharacter(character));
}
/**
* Adds the specified number to self.
*
* @param number the number that needs to be added to the array.
*/
public void add(Number number) {
elements.add(number == null ? Json5Primitive.fromNull() : Json5Primitive.fromNumber(number));
}
public void add(Number number, int radix) {
elements.add(number == null ? Json5Primitive.fromNull() : Json5Primitive.fromNumber(number, radix));
}
/**
* Adds the specified string to self.
*
* @param string the string that needs to be added to the array.
*/
public void add(String string) {
elements.add(string == null ? Json5Primitive.fromNull() : Json5Primitive.fromString(string));
}
/**
* Adds the specified element to self.
*
* @param element the element that needs to be added to the array.
*/
public void add(Json5Element element) {
if (element == null) {
element = Json5Primitive.fromNull();
}
elements.add(element);
}
/**
* Adds all the elements of the specified array to self.
*
* @param array the array whose elements need to be added to the array.
*/
public void addAll(Json5Array array) {
elements.addAll(array.elements);
}
/**
* Replaces the element at the specified position in this array with the specified element.
*
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException if the specified index is outside the array bounds
*/
public Json5Element set(int index, Json5Element element) {
return elements.set(index, element == null ? Json5Primitive.fromNull() : element);
}
/**
* Removes the first occurrence of the specified element from this array, if it is present. If the
* array does not contain the element, it is unchanged.
*
* @param element element to be removed from this array, if present
* @return true if this array contained the specified element, false otherwise
*/
public boolean remove(Json5Element element) {
return elements.remove(element);
}
/**
* Removes the element at the specified position in this array. Shifts any subsequent elements to
* the left (subtracts one from their indices). Returns the element that was removed from the
* array.
*
* @param index index the index of the element to be removed
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException if the specified index is outside the array bounds
*/
public Json5Element remove(int index) {
return elements.remove(index);
}
/**
* Returns true if this array contains the specified element.
*
* @param element whose presence in this array is to be tested
* @return true if this array contains the specified element.
*/
public boolean contains(Json5Element element) {
return elements.contains(element);
}
/**
* Returns the number of elements in the array.
*
* @return the number of elements in the array.
*/
public int size() {
return elements.size();
}
/**
* Returns true if the array is empty.
*
* @return true if the array is empty.
*/
public boolean isEmpty() {
return elements.isEmpty();
}
/**
* Returns an iterator to navigate the elements of the array. Since the array is an ordered list,
* the iterator navigates the elements in the order they were inserted.
*
* @return an iterator to navigate the elements of the array.
*/
@Override
public Iterator<Json5Element> iterator() {
return elements.iterator();
}
/**
* Returns the i-th element of the array.
*
* @param i the index of the element that is being sought.
* @return the element present at the i-th index.
* @throws IndexOutOfBoundsException if {@code i} is negative or greater than or equal to the
* {@link #size()} of the array.
*/
public Json5Element get(int i) {
return elements.get(i);
}
private Json5Element getAsSingleElement() {
int size = elements.size();
if (size == 1) {
return elements.get(0);
}
throw new IllegalStateException("Array must have size 1, but has size " + size);
}
@Override
public Json5Null getAsJson5Null() {
return getAsSingleElement().getAsJson5Null();
}
/**
* Convenience method to get this array as a {@link Number} if it contains a single element. This
* method calls {@link Json5Element#getAsNumber()} on the element, therefore any of the exceptions
* declared by that method can occur.
*
* @return this element as a number if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public Number getAsNumber() {
return getAsSingleElement().getAsNumber();
}
/**
* Convenience method to get this array as a {@link RadixNumber} if it contains a single element. This
* method calls {@link Json5Element#getAsRadixNumber()} on the element, therefore any of the exceptions
* declared by that method can occur.
*
* @return this element as a radix number if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public RadixNumber getAsRadixNumber() {
return getAsSingleElement().getAsRadixNumber();
}
/**
* Convenience method to get this array as a binary number string if it contains a single element. This
* method calls {@link Json5Element#getAsBinaryString()} on the element, therefore any of the exceptions
* declared by that method can occur.
*
* @return this element as a binary number string if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public String getAsBinaryString() {
return getAsSingleElement().getAsBinaryString();
}
/**
* Convenience method to get this array as a octal number string if it contains a single element. This
* method calls {@link Json5Element#getAsOctalString()} on the element, therefore any of the exceptions
* declared by that method can occur.
*
* @return this element as a octal number string if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public String getAsOctalString() {
return getAsSingleElement().getAsOctalString();
}
/**
* Convenience method to get this array as a hex number string if it contains a single element. This
* method calls {@link Json5Element#getAsHexString()} on the element, therefore any of the exceptions
* declared by that method can occur.
*
* @return this element as a hex number string if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public String getAsHexString() {
return getAsSingleElement().getAsHexString();
}
/**
* Convenience method to get this array as a {@link String} if it contains a single element. This
* method calls {@link Json5Element#getAsString()} on the element, therefore any of the exceptions
* declared by that method can occur.
*
* @return this element as a String if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public String getAsString() {
return getAsSingleElement().getAsString();
}
/**
* Convenience method to get this array as a double if it contains a single element. This method
* calls {@link Json5Element#getAsDouble()} on the element, therefore any of the exceptions
* declared by that method can occur.
*
* @return this element as a double if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public double getAsDouble() {
return getAsSingleElement().getAsDouble();
}
/**
* Convenience method to get this array as a {@link BigDecimal} if it contains a single element.
* This method calls {@link Json5Element#getAsBigDecimal()} on the element, therefore any of the
* exceptions declared by that method can occur.
*
* @return this element as a {@link BigDecimal} if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public BigDecimal getAsBigDecimal() {
return getAsSingleElement().getAsBigDecimal();
}
/**
* Convenience method to get this array as a {@link BigInteger} if it contains a single element.
* This method calls {@link Json5Element#getAsBigInteger()} on the element, therefore any of the
* exceptions declared by that method can occur.
*
* @return this element as a {@link BigInteger} if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public BigInteger getAsBigInteger() {
return getAsSingleElement().getAsBigInteger();
}
/**
* Convenience method to get this array as a float if it contains a single element. This method
* calls {@link Json5Element#getAsFloat()} on the element, therefore any of the exceptions declared
* by that method can occur.
*
* @return this element as a float if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public float getAsFloat() {
return getAsSingleElement().getAsFloat();
}
/**
* Convenience method to get this array as a long if it contains a single element. This method
* calls {@link Json5Element#getAsLong()} on the element, therefore any of the exceptions declared
* by that method can occur.
*
* @return this element as a long if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public long getAsLong() {
return getAsSingleElement().getAsLong();
}
/**
* Convenience method to get this array as an integer if it contains a single element. This method
* calls {@link Json5Element#getAsInt()} on the element, therefore any of the exceptions declared
* by that method can occur.
*
* @return this element as an integer if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public int getAsInt() {
return getAsSingleElement().getAsInt();
}
/**
* Convenience method to get this array as a primitive byte if it contains a single element. This
* method calls {@link Json5Element#getAsByte()} on the element, therefore any of the exceptions
* declared by that method can occur.
*
* @return this element as a primitive byte if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public byte getAsByte() {
return getAsSingleElement().getAsByte();
}
/**
* Convenience method to get this array as a primitive short if it contains a single element. This
* method calls {@link Json5Element#getAsShort()} on the element, therefore any of the exceptions
* declared by that method can occur.
*
* @return this element as a primitive short if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public short getAsShort() {
return getAsSingleElement().getAsShort();
}
/**
* Convenience method to get this array as a Insta if it contains a single element. This method
* calls {@link Json5Element#getAsBoolean()} on the element, therefore any of the exceptions
* declared by that method can occur.
*
* @return this element as a boolean if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public Instant getAsInstant() {
return getAsSingleElement().getAsInstant();
}
/**
* Convenience method to get this array as a boolean if it contains a single element. This method
* calls {@link Json5Element#getAsBoolean()} on the element, therefore any of the exceptions
* declared by that method can occur.
*
* @return this element as a boolean if it is single element array.
* @throws IllegalStateException if the array is empty or has more than one element.
*/
@Override
public boolean getAsBoolean() {
return getAsSingleElement().getAsBoolean();
}
/**
* Returns a mutable {@link List} view of this {@code Json5Array}. Changes to the {@code List} are
* visible in this {@code Json5Array} and the other way around.
*
* <p>The {@code List} does not permit {@code null} elements. Unlike {@code Json5Array}'s {@code
* null} handling, a {@link NullPointerException} is thrown when trying to add {@code null}. Use
* {@link Json5Null} for Json5 null values.
*
* @return mutable {@code List} view
*/
public List<Json5Element> asList() {
return new NonNullElementWrapperList<>(elements);
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Json5Array that = (Json5Array) o;
return Objects.equals(elements, that.elements);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), elements);
}
}