source: josm/trunk/src/org/glassfish/json/JsonArrayBuilderImpl.java@ 13661

Last change on this file since 13661 was 13231, checked in by Don-vip, 6 years ago

see #15682 - upgrade to JSR 374 (JSON Processing) API 1.1.2

File size: 14.4 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can
10 * obtain a copy of the License at
11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1
12 * or LICENSE.txt. See the License for the specific
13 * language governing permissions and limitations under the License.
14 *
15 * When distributing the software, include this License Header Notice in each
16 * file and include the License file at LICENSE.txt.
17 *
18 * GPL Classpath Exception:
19 * Oracle designates this particular file as subject to the "Classpath"
20 * exception as provided by Oracle in the GPL Version 2 section of the License
21 * file that accompanied this code.
22 *
23 * Modifications:
24 * If applicable, add the following below the License Header, with the fields
25 * enclosed by brackets [] replaced by your own identifying information:
26 * "Portions Copyright [year] [name of copyright owner]"
27 *
28 * Contributor(s):
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41package org.glassfish.json;
42
43import org.glassfish.json.api.BufferPool;
44
45import javax.json.*;
46import java.io.StringWriter;
47import java.math.BigDecimal;
48import java.math.BigInteger;
49import java.util.AbstractList;
50import java.util.ArrayList;
51import java.util.Collection;
52import java.util.Collections;
53import java.util.List;
54import java.util.Optional;
55
56/**
57 * JsonArrayBuilder implementation
58 *
59 * @author Jitendra Kotamraju
60 * @author Kin-man Chung
61 */
62
63class JsonArrayBuilderImpl implements JsonArrayBuilder {
64 private ArrayList<JsonValue> valueList;
65 private final BufferPool bufferPool;
66
67 JsonArrayBuilderImpl(BufferPool bufferPool) {
68 this.bufferPool = bufferPool;
69 }
70
71 JsonArrayBuilderImpl(JsonArray array, BufferPool bufferPool) {
72 this.bufferPool = bufferPool;
73 valueList = new ArrayList<>();
74 valueList.addAll(array);
75 }
76
77 JsonArrayBuilderImpl(Collection<?> collection, BufferPool bufferPool) {
78 this.bufferPool = bufferPool;
79 valueList = new ArrayList<>();
80 populate(collection);
81 }
82
83 @Override
84 public JsonArrayBuilder add(JsonValue value) {
85 validateValue(value);
86 addValueList(value);
87 return this;
88 }
89
90 @Override
91 public JsonArrayBuilder add(String value) {
92 validateValue(value);
93 addValueList(new JsonStringImpl(value));
94 return this;
95 }
96
97 @Override
98 public JsonArrayBuilder add(BigDecimal value) {
99 validateValue(value);
100 addValueList(JsonNumberImpl.getJsonNumber(value));
101 return this;
102 }
103
104 @Override
105 public JsonArrayBuilder add(BigInteger value) {
106 validateValue(value);
107 addValueList(JsonNumberImpl.getJsonNumber(value));
108 return this;
109 }
110
111 @Override
112 public JsonArrayBuilder add(int value) {
113 addValueList(JsonNumberImpl.getJsonNumber(value));
114 return this;
115 }
116
117 @Override
118 public JsonArrayBuilder add(long value) {
119 addValueList(JsonNumberImpl.getJsonNumber(value));
120 return this;
121 }
122
123 @Override
124 public JsonArrayBuilder add(double value) {
125 addValueList(JsonNumberImpl.getJsonNumber(value));
126 return this;
127 }
128
129 @Override
130 public JsonArrayBuilder add(boolean value) {
131 addValueList(value ? JsonValue.TRUE : JsonValue.FALSE);
132 return this;
133 }
134
135 @Override
136 public JsonArrayBuilder addNull() {
137 addValueList(JsonValue.NULL);
138 return this;
139 }
140
141 @Override
142 public JsonArrayBuilder add(JsonObjectBuilder builder) {
143 if (builder == null) {
144 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
145 }
146 addValueList(builder.build());
147 return this;
148 }
149
150 @Override
151 public JsonArrayBuilder add(JsonArrayBuilder builder) {
152 if (builder == null) {
153 throw new NullPointerException(JsonMessages.ARRBUILDER_ARRAY_BUILDER_NULL());
154 }
155 addValueList(builder.build());
156 return this;
157 }
158
159 @Override
160 public JsonArrayBuilder addAll(JsonArrayBuilder builder) {
161 if (builder == null) {
162 throw new NullPointerException(JsonMessages.ARRBUILDER_ARRAY_BUILDER_NULL());
163 }
164 if (valueList == null) {
165 valueList = new ArrayList<>();
166 }
167 valueList.addAll(builder.build());
168 return this;
169 }
170
171 @Override
172 public JsonArrayBuilder add(int index, JsonValue value) {
173 validateValue(value);
174 addValueList(index, value);
175 return this;
176 }
177
178 @Override
179 public JsonArrayBuilder add(int index, String value) {
180 validateValue(value);
181 addValueList(index, new JsonStringImpl(value));
182 return this;
183 }
184
185 @Override
186 public JsonArrayBuilder add(int index, BigDecimal value) {
187 validateValue(value);
188 addValueList(index, JsonNumberImpl.getJsonNumber(value));
189 return this;
190 }
191
192 @Override
193 public JsonArrayBuilder add(int index, BigInteger value) {
194 validateValue(value);
195 addValueList(index, JsonNumberImpl.getJsonNumber(value));
196 return this;
197 }
198
199 @Override
200 public JsonArrayBuilder add(int index, int value) {
201 addValueList(index, JsonNumberImpl.getJsonNumber(value));
202 return this;
203 }
204
205 @Override
206 public JsonArrayBuilder add(int index, long value) {
207 addValueList(index, JsonNumberImpl.getJsonNumber(value));
208 return this;
209 }
210
211 @Override
212 public JsonArrayBuilder add(int index, double value) {
213 addValueList(index, JsonNumberImpl.getJsonNumber(value));
214 return this;
215 }
216
217 @Override
218 public JsonArrayBuilder add(int index, boolean value) {
219 addValueList(index, value ? JsonValue.TRUE : JsonValue.FALSE);
220 return this;
221 }
222
223 @Override
224 public JsonArrayBuilder addNull(int index) {
225 addValueList(index, JsonValue.NULL);
226 return this;
227 }
228
229 @Override
230 public JsonArrayBuilder add(int index, JsonObjectBuilder builder) {
231 if (builder == null) {
232 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
233 }
234 addValueList(index, builder.build());
235 return this;
236 }
237
238 @Override
239 public JsonArrayBuilder add(int index, JsonArrayBuilder builder) {
240 if (builder == null) {
241 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
242 }
243 addValueList(index, builder.build());
244 return this;
245 }
246
247 @Override
248 public JsonArrayBuilder set(int index, JsonValue value) {
249 validateValue(value);
250 setValueList(index, value);
251 return this;
252 }
253
254 @Override
255 public JsonArrayBuilder set(int index, String value) {
256 validateValue(value);
257 setValueList(index, new JsonStringImpl(value));
258 return this;
259 }
260
261 @Override
262 public JsonArrayBuilder set(int index, BigDecimal value) {
263 validateValue(value);
264 setValueList(index, JsonNumberImpl.getJsonNumber(value));
265 return this;
266 }
267
268 @Override
269 public JsonArrayBuilder set(int index, BigInteger value) {
270 validateValue(value);
271 setValueList(index, JsonNumberImpl.getJsonNumber(value));
272 return this;
273 }
274
275 @Override
276 public JsonArrayBuilder set(int index, int value) {
277 setValueList(index, JsonNumberImpl.getJsonNumber(value));
278 return this;
279 }
280
281 @Override
282 public JsonArrayBuilder set(int index, long value) {
283 setValueList(index, JsonNumberImpl.getJsonNumber(value));
284 return this;
285 }
286
287 @Override
288 public JsonArrayBuilder set(int index, double value) {
289 setValueList(index, JsonNumberImpl.getJsonNumber(value));
290 return this;
291 }
292
293 @Override
294 public JsonArrayBuilder set(int index, boolean value) {
295 setValueList(index, value ? JsonValue.TRUE : JsonValue.FALSE);
296 return this;
297 }
298
299 @Override
300 public JsonArrayBuilder setNull(int index) {
301 setValueList(index, JsonValue.NULL);
302 return this;
303 }
304
305 @Override
306 public JsonArrayBuilder set(int index, JsonObjectBuilder builder) {
307 if (builder == null) {
308 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
309 }
310 setValueList(index, builder.build());
311 return this;
312 }
313
314 @Override
315 public JsonArrayBuilder set(int index, JsonArrayBuilder builder) {
316 if (builder == null) {
317 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
318 }
319 setValueList(index, builder.build());
320 return this;
321 }
322
323 @Override
324 public JsonArrayBuilder remove(int index) {
325 if (valueList == null) {
326 throw new IndexOutOfBoundsException(JsonMessages.ARRBUILDER_VALUELIST_NULL(index, 0));
327 }
328 valueList.remove(index);
329 return this;
330 }
331
332 @Override
333 public JsonArray build() {
334 List<JsonValue> snapshot;
335 if (valueList == null) {
336 snapshot = Collections.emptyList();
337 } else {
338 // Should we trim to minimize storage ?
339 // valueList.trimToSize();
340 snapshot = Collections.unmodifiableList(valueList);
341 }
342 valueList = null;
343 return new JsonArrayImpl(snapshot, bufferPool);
344 }
345
346 private void populate(Collection<?> collection) {
347 for (Object value : collection) {
348 if (value != null && value instanceof Optional) {
349 ((Optional<?>) value).ifPresent(v ->
350 this.valueList.add(MapUtil.handle(v, bufferPool)));
351 } else {
352 this.valueList.add(MapUtil.handle(value, bufferPool));
353 }
354 }
355 }
356
357 private void addValueList(JsonValue value) {
358 if (valueList == null) {
359 valueList = new ArrayList<>();
360 }
361 valueList.add(value);
362 }
363
364 private void addValueList(int index, JsonValue value) {
365 if (valueList == null) {
366 valueList = new ArrayList<>();
367 }
368 valueList.add(index, value);
369 }
370
371 private void setValueList(int index, JsonValue value) {
372 if (valueList == null) {
373 throw new IndexOutOfBoundsException(JsonMessages.ARRBUILDER_VALUELIST_NULL(index, 0));
374 }
375 valueList.set(index, value);
376 }
377
378 private void validateValue(Object value) {
379 if (value == null) {
380 throw new NullPointerException(JsonMessages.ARRBUILDER_VALUE_NULL());
381 }
382 }
383
384 private static final class JsonArrayImpl extends AbstractList<JsonValue> implements JsonArray {
385 private final List<JsonValue> valueList; // Unmodifiable
386 private final BufferPool bufferPool;
387
388 JsonArrayImpl(List<JsonValue> valueList, BufferPool bufferPool) {
389 this.valueList = valueList;
390 this.bufferPool = bufferPool;
391 }
392
393 @Override
394 public int size() {
395 return valueList.size();
396 }
397
398 @Override
399 public JsonObject getJsonObject(int index) {
400 return (JsonObject)valueList.get(index);
401 }
402
403 @Override
404 public JsonArray getJsonArray(int index) {
405 return (JsonArray)valueList.get(index);
406 }
407
408 @Override
409 public JsonNumber getJsonNumber(int index) {
410 return (JsonNumber)valueList.get(index);
411 }
412
413 @Override
414 public JsonString getJsonString(int index) {
415 return (JsonString)valueList.get(index);
416 }
417
418 @Override
419 @SuppressWarnings("unchecked")
420 public <T extends JsonValue> List<T> getValuesAs(Class<T> clazz) {
421 return (List<T>)valueList;
422 }
423
424 @Override
425 public String getString(int index) {
426 return getJsonString(index).getString();
427 }
428
429 @Override
430 public String getString(int index, String defaultValue) {
431 try {
432 return getString(index);
433 } catch (Exception e) {
434 return defaultValue;
435 }
436 }
437
438 @Override
439 public int getInt(int index) {
440 return getJsonNumber(index).intValue();
441 }
442
443 @Override
444 public int getInt(int index, int defaultValue) {
445 try {
446 return getInt(index);
447 } catch (Exception e) {
448 return defaultValue;
449 }
450 }
451
452 @Override
453 public boolean getBoolean(int index) {
454 JsonValue jsonValue = get(index);
455 if (jsonValue == JsonValue.TRUE) {
456 return true;
457 } else if (jsonValue == JsonValue.FALSE) {
458 return false;
459 } else {
460 throw new ClassCastException();
461 }
462 }
463
464 @Override
465 public boolean getBoolean(int index, boolean defaultValue) {
466 try {
467 return getBoolean(index);
468 } catch (Exception e) {
469 return defaultValue;
470 }
471 }
472
473 @Override
474 public boolean isNull(int index) {
475 return valueList.get(index).equals(JsonValue.NULL);
476 }
477
478 @Override
479 public ValueType getValueType() {
480 return ValueType.ARRAY;
481 }
482
483 @Override
484 public JsonValue get(int index) {
485 return valueList.get(index);
486 }
487
488 @Override
489 public String toString() {
490 StringWriter sw = new StringWriter();
491 try (JsonWriter jw = new JsonWriterImpl(sw, bufferPool)) {
492 jw.write(this);
493 }
494 return sw.toString();
495 }
496
497 @Override
498 public JsonArray asJsonArray() {
499 return this;
500 }
501 }
502}
503
Note: See TracBrowser for help on using the repository browser.