Debian Codegraph @

src/org/eclipse/parsson/JsonArrayBuilderImpl.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17 package org.eclipse.parsson;
18
19 import jakarta.json.*;
20 import java.io.StringWriter;
21 import java.math.BigDecimal;
22 import java.math.BigInteger;
23 import java.util.AbstractList;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Optional;
29
30 /**
31 * JsonArrayBuilder implementation
32 *
33 * @author Jitendra Kotamraju
34 * @author Kin-man Chung
35 */
36 class JsonArrayBuilderImpl implements JsonArrayBuilder {
37
38 private ArrayList<JsonValue> valueList;
39 private final JsonContext jsonContext;
40
41 JsonArrayBuilderImpl(JsonContext jsonContext) {
42 this.jsonContext = jsonContext;
43 }
44
45 JsonArrayBuilderImpl(JsonArray array, JsonContext jsonContext) {
46 this(jsonContext);
47 valueList = new ArrayList<>();
48 valueList.addAll(array);
49 }
50
51 JsonArrayBuilderImpl(Collection<?> collection, JsonContext jsonContext) {
52 this(jsonContext);
53 valueList = new ArrayList<>();
54 populate(collection);
55 }
56
57 @Override
58 public JsonArrayBuilder add(JsonValue value) {
59 validateValue(value);
60 addValueList(value);
61 return this;
62 }
63
64 @Override
65 public JsonArrayBuilder add(String value) {
66 validateValue(value);
67 addValueList(new JsonStringImpl(value));
68 return this;
69 }
70
71 @Override
72 public JsonArrayBuilder add(BigDecimal value) {
73 validateValue(value);
74 addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
75 return this;
76 }
77
78 @Override
79 public JsonArrayBuilder add(BigInteger value) {
80 validateValue(value);
81 addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
82 return this;
83 }
84
85 @Override
86 public JsonArrayBuilder add(int value) {
87 addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
88 return this;
89 }
90
91 @Override
92 public JsonArrayBuilder add(long value) {
93 addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
94 return this;
95 }
96
97 @Override
98 public JsonArrayBuilder add(double value) {
99 addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
100 return this;
101 }
102
103 @Override
104 public JsonArrayBuilder add(boolean value) {
105 addValueList(value ? JsonValue.TRUE : JsonValue.FALSE);
106 return this;
107 }
108
109 @Override
110 public JsonArrayBuilder addNull() {
111 addValueList(JsonValue.NULL);
112 return this;
113 }
114
115 @Override
116 public JsonArrayBuilder add(JsonObjectBuilder builder) {
117 if (builder == null) {
118 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
119 }
120 addValueList(builder.build());
121 return this;
122 }
123
124 @Override
125 public JsonArrayBuilder add(JsonArrayBuilder builder) {
126 if (builder == null) {
127 throw new NullPointerException(JsonMessages.ARRBUILDER_ARRAY_BUILDER_NULL());
128 }
129 addValueList(builder.build());
130 return this;
131 }
132
133 @Override
134 public JsonArrayBuilder addAll(JsonArrayBuilder builder) {
135 if (builder == null) {
136 throw new NullPointerException(JsonMessages.ARRBUILDER_ARRAY_BUILDER_NULL());
137 }
138 if (valueList == null) {
139 valueList = new ArrayList<>();
140 }
141 valueList.addAll(builder.build());
142 return this;
143 }
144
145 @Override
146 public JsonArrayBuilder add(int index, JsonValue value) {
147 validateValue(value);
148 addValueList(index, value);
149 return this;
150 }
151
152 @Override
153 public JsonArrayBuilder add(int index, String value) {
154 validateValue(value);
155 addValueList(index, new JsonStringImpl(value));
156 return this;
157 }
158
159 @Override
160 public JsonArrayBuilder add(int index, BigDecimal value) {
161 validateValue(value);
162 addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
163 return this;
164 }
165
166 @Override
167 public JsonArrayBuilder add(int index, BigInteger value) {
168 validateValue(value);
169 addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
170 return this;
171 }
172
173 @Override
174 public JsonArrayBuilder add(int index, int value) {
175 addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
176 return this;
177 }
178
179 @Override
180 public JsonArrayBuilder add(int index, long value) {
181 addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
182 return this;
183 }
184
185 @Override
186 public JsonArrayBuilder add(int index, double value) {
187 addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
188 return this;
189 }
190
191 @Override
192 public JsonArrayBuilder add(int index, boolean value) {
193 addValueList(index, value ? JsonValue.TRUE : JsonValue.FALSE);
194 return this;
195 }
196
197 @Override
198 public JsonArrayBuilder addNull(int index) {
199 addValueList(index, JsonValue.NULL);
200 return this;
201 }
202
203 @Override
204 public JsonArrayBuilder add(int index, JsonObjectBuilder builder) {
205 if (builder == null) {
206 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
207 }
208 addValueList(index, builder.build());
209 return this;
210 }
211
212 @Override
213 public JsonArrayBuilder add(int index, JsonArrayBuilder builder) {
214 if (builder == null) {
215 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
216 }
217 addValueList(index, builder.build());
218 return this;
219 }
220
221 @Override
222 public JsonArrayBuilder set(int index, JsonValue value) {
223 validateValue(value);
224 setValueList(index, value);
225 return this;
226 }
227
228 @Override
229 public JsonArrayBuilder set(int index, String value) {
230 validateValue(value);
231 setValueList(index, new JsonStringImpl(value));
232 return this;
233 }
234
235 @Override
236 public JsonArrayBuilder set(int index, BigDecimal value) {
237 validateValue(value);
238 setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
239 return this;
240 }
241
242 @Override
243 public JsonArrayBuilder set(int index, BigInteger value) {
244 validateValue(value);
245 setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
246 return this;
247 }
248
249 @Override
250 public JsonArrayBuilder set(int index, int value) {
251 setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
252 return this;
253 }
254
255 @Override
256 public JsonArrayBuilder set(int index, long value) {
257 setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
258 return this;
259 }
260
261 @Override
262 public JsonArrayBuilder set(int index, double value) {
263 setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
264 return this;
265 }
266
267 @Override
268 public JsonArrayBuilder set(int index, boolean value) {
269 setValueList(index, value ? JsonValue.TRUE : JsonValue.FALSE);
270 return this;
271 }
272
273 @Override
274 public JsonArrayBuilder setNull(int index) {
275 setValueList(index, JsonValue.NULL);
276 return this;
277 }
278
279 @Override
280 public JsonArrayBuilder set(int index, JsonObjectBuilder builder) {
281 if (builder == null) {
282 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
283 }
284 setValueList(index, builder.build());
285 return this;
286 }
287
288 @Override
289 public JsonArrayBuilder set(int index, JsonArrayBuilder builder) {
290 if (builder == null) {
291 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
292 }
293 setValueList(index, builder.build());
294 return this;
295 }
296
297 @Override
298 public JsonArrayBuilder remove(int index) {
299 if (valueList == null) {
300 throw new IndexOutOfBoundsException(JsonMessages.ARRBUILDER_VALUELIST_NULL(index, 0));
301 }
302 valueList.remove(index);
303 return this;
304 }
305
306 @Override
307 public JsonArray build() {
308 List<JsonValue> snapshot;
309 if (valueList == null) {
310 snapshot = Collections.emptyList();
311 } else {
312 // Should we trim to minimize storage ?
313 // valueList.trimToSize();
314 snapshot = Collections.unmodifiableList(valueList);
315 }
316 valueList = null;
317 return new JsonArrayImpl(snapshot, jsonContext);
318 }
319
320 private void populate(Collection<?> collection) {
321 for (Object value : collection) {
322 if (value instanceof Optional) {
323 ((Optional<?>) value).ifPresent(v ->
324 this.valueList.add(MapUtil.handle(v, jsonContext)));
325 } else {
326 this.valueList.add(MapUtil.handle(value, jsonContext));
327 }
328 }
329 }
330
331 private void addValueList(JsonValue value) {
332 if (valueList == null) {
333 valueList = new ArrayList<>();
334 }
335 valueList.add(value);
336 }
337
338 private void addValueList(int index, JsonValue value) {
339 if (valueList == null) {
340 valueList = new ArrayList<>();
341 }
342 valueList.add(index, value);
343 }
344
345 private void setValueList(int index, JsonValue value) {
346 if (valueList == null) {
347 throw new IndexOutOfBoundsException(JsonMessages.ARRBUILDER_VALUELIST_NULL(index, 0));
348 }
349 valueList.set(index, value);
350 }
351
352 private void validateValue(Object value) {
353 if (value == null) {
354 throw new NullPointerException(JsonMessages.ARRBUILDER_VALUE_NULL());
355 }
356 }
357
358 private static final class JsonArrayImpl extends AbstractList<JsonValue> implements JsonArray {
359 private final List<JsonValue> valueList; // Unmodifiable
360 private final JsonContext jsonContext;
361 private int hashCode;
362
363 JsonArrayImpl(List<JsonValue> valueList, JsonContext jsonContext) {
364 this.valueList = valueList;
365 this.jsonContext = jsonContext;
366 }
367
368 @Override
369 public int size() {
370 return valueList.size();
371 }
372
373 @Override
374 public JsonObject getJsonObject(int index) {
375 return (JsonObject)valueList.get(index);
376 }
377
378 @Override
379 public JsonArray getJsonArray(int index) {
380 return (JsonArray)valueList.get(index);
381 }
382
383 @Override
384 public JsonNumber getJsonNumber(int index) {
385 return (JsonNumber)valueList.get(index);
386 }
387
388 @Override
389 public JsonString getJsonString(int index) {
390 return (JsonString)valueList.get(index);
391 }
392
393 @Override
394 @SuppressWarnings("unchecked")
395 public <T extends JsonValue> List<T> getValuesAs(Class<T> clazz) {
396 return (List<T>)valueList;
397 }
398
399 @Override
400 public String getString(int index) {
401 return getJsonString(index).getString();
402 }
403
404 @Override
405 public String getString(int index, String defaultValue) {
406 try {
407 return getString(index);
408 } catch (Exception e) {
409 return defaultValue;
410 }
411 }
412
413 @Override
414 public int getInt(int index) {
415 return getJsonNumber(index).intValue();
416 }
417
418 @Override
419 public int getInt(int index, int defaultValue) {
420 try {
421 return getInt(index);
422 } catch (Exception e) {
423 return defaultValue;
424 }
425 }
426
427 @Override
428 public boolean getBoolean(int index) {
429 JsonValue jsonValue = get(index);
430 if (jsonValue == JsonValue.TRUE) {
431 return true;
432 } else if (jsonValue == JsonValue.FALSE) {
433 return false;
434 } else {
435 throw new ClassCastException();
436 }
437 }
438
439 @Override
440 public boolean getBoolean(int index, boolean defaultValue) {
441 try {
442 return getBoolean(index);
443 } catch (Exception e) {
444 return defaultValue;
445 }
446 }
447
448 @Override
449 public boolean isNull(int index) {
450 return valueList.get(index).equals(JsonValue.NULL);
451 }
452
453 @Override
454 public ValueType getValueType() {
455 return ValueType.ARRAY;
456 }
457
458 @Override
459 public JsonValue get(int index) {
460 return valueList.get(index);
461 }
462
463 @Override
464 public int hashCode() {
465 if (hashCode == 0) {
466 hashCode = super.hashCode();
467 }
468 return hashCode;
469 }
470
471 @Override
472 public String toString() {
473 StringWriter sw = new StringWriter();
474 try (JsonWriter jw = new JsonWriterImpl(sw, jsonContext)) {
475 jw.write(this);
476 }
477 return sw.toString();
478 }
479
480 @Override
481 public JsonArray asJsonArray() {
482 return this;
483 }
484 }
485 }
486