Debian Codegraph @

src/jakarta/json/JsonArrayBuilder.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2013, 2021 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 jakarta.json;
18
19 import java.math.BigDecimal;
20 import java.math.BigInteger;
21
22 /**
23 * A builder for creating {@link JsonArray} models from scratch, and for
24 * modifying a existing {@code JsonArray}.
25 * <p>A {@code JsonArrayBuilder} can start with an empty or a non-empty
26 * JSON array model. This interface provides methods to add, insert, remove
27 * and replace values in the JSON array model.</p>
28 * <p>Methods in this class can be chained to perform multiple values to
29 * the array.</p>
30 *
31 * <p>The class {@link jakarta.json.Json} contains methods to create the builder
32 * object. The example code below shows how to build an empty {@code JsonArray}
33 * instance.
34 * <pre>
35 * <code>
36 * JsonArray array = Json.createArrayBuilder().build();
37 * </code>
38 * </pre>
39 *
40 * <p>The class {@link JsonBuilderFactory} also contains methods to create
41 * {@code JsonArrayBuilder} instances. A factory instance can be used to create
42 * multiple builder instances with the same configuration. This the preferred
43 * way to create multiple instances.
44 *
45 * The example code below shows how to build a {@code JsonArray} object
46 * that represents the following JSON array:
47 *
48 * <pre>
49 * <code>
50 * [
51 * { "type": "home", "number": "212 555-1234" },
52 * { "type": "fax", "number": "646 555-4567" }
53 * ]
54 * </code>
55 * </pre>
56 *
57 * <p>The following code creates the JSON array above:
58 *
59 * <pre>
60 * <code>
61 * JsonBuilderFactory factory = Json.createBuilderFactory(config);
62 * JsonArray value = factory.createArrayBuilder()
63 * .add(factory.createObjectBuilder()
64 * .add("type", "home")
65 * .add("number", "212 555-1234"))
66 * .add(factory.createObjectBuilder()
67 * .add("type", "fax")
68 * .add("number", "646 555-4567"))
69 * .build();
70 * </code>
71 * </pre>
72 *
73 * <p>This class does <em>not</em> allow <code>null</code> to be used as a
74 * value while building the JSON array
75 *
76 * @see JsonObjectBuilder
77 */
78 public interface JsonArrayBuilder {
79
80 /**
81 * Adds a value to the array.
82 *
83 * @param value the JSON value
84 * @return this array builder
85 * @throws NullPointerException if the specified value is null
86 */
87 JsonArrayBuilder add(JsonValue value);
88
89 /**
90 * Adds a value to the array as a {@link JsonString}.
91 *
92 * @param value the string value
93 * @return this array builder
94 * @throws NullPointerException if the specified value is null
95 */
96 JsonArrayBuilder add(String value);
97
98 /**
99 * Adds a value to the array as a {@link JsonNumber}.
100 *
101 * @param value the number value
102 * @return this array builder
103 * @throws NullPointerException if the specified value is null
104 *
105 * @see JsonNumber
106 */
107 JsonArrayBuilder add(BigDecimal value);
108
109 /**
110 * Adds a value to the array as a {@link JsonNumber}.
111 *
112 * @param value the number value
113 * @return this array builder
114 * @throws NullPointerException if the specified value is null
115 *
116 * @see JsonNumber
117 */
118 JsonArrayBuilder add(BigInteger value);
119
120 /**
121 * Adds a value to the array as a {@link JsonNumber}.
122 *
123 * @param value the number value
124 * @return this array builder
125 *
126 * @see JsonNumber
127 */
128 JsonArrayBuilder add(int value);
129
130 /**
131 * Adds a value to the array as a {@link JsonNumber}.
132 *
133 * @param value the number value
134 * @return this array builder
135 *
136 * @see JsonNumber
137 */
138 JsonArrayBuilder add(long value);
139
140 /**
141 * Adds a value to the array as a {@link JsonNumber}.
142 *
143 * @param value the number value
144 * @return this array builder
145 * @throws NumberFormatException if the value is Not-a-Number (NaN) or
146 * infinity
147 *
148 * @see JsonNumber
149 */
150 JsonArrayBuilder add(double value);
151
152 /**
153 * Adds a {@link JsonValue#TRUE} or {@link JsonValue#FALSE} value to the
154 * array.
155 *
156 * @param value the boolean value
157 * @return this array builder
158 */
159 JsonArrayBuilder add(boolean value);
160
161 /**
162 * Adds a {@link JsonValue#NULL} value to the array.
163 *
164 * @return this array builder
165 */
166 JsonArrayBuilder addNull();
167
168 /**
169 * Adds a {@link JsonObject} from an object builder to the array.
170 *
171 * @param builder the object builder
172 * @return this array builder
173 * @throws NullPointerException if the specified builder is null
174 */
175 JsonArrayBuilder add(JsonObjectBuilder builder);
176
177 /**
178 * Adds a {@link JsonArray} from an array builder to the array.
179 *
180 * @param builder the array builder
181 * @return this array builder
182 * @throws NullPointerException if the specified builder is null
183 */
184 JsonArrayBuilder add(JsonArrayBuilder builder);
185
186 /**
187 * Adds all elements of the array in the specified array builder to the array.
188 *
189 * @param builder the array builder
190 * @return this array builder
191 * @throws NullPointerException if the specified builder is null
192 *
193 @since 1.1
194 */
195 default JsonArrayBuilder addAll(JsonArrayBuilder builder) {
196 throw new UnsupportedOperationException();
197 }
198
199 /**
200 * Inserts a value to the array at the specified position. Shifts the value
201 * currently at that position (if any) and any subsequent values to the right
202 * (adds one to their indices). Index starts with 0.
203 *
204 * @param index the position in the array
205 * @param value the JSON value
206 * @return this array builder
207 * @throws NullPointerException if the specified value is null
208 * @throws IndexOutOfBoundsException if the index is out of range
209 * {@code (index < 0 || index > array size)}
210 *
211 * @since 1.1
212 */
213 default JsonArrayBuilder add(int index, JsonValue value) {
214 throw new UnsupportedOperationException();
215 }
216
217 /**
218 * Adds a value to the array as a {@link JsonString} at the specified position.
219 * Shifts the value currently at that position (if any) and any subsequent values
220 * to the right (adds one to their indices). Index starts with 0.
221 *
222 * @param index the position in the array
223 * @param value the string value
224 * @return this array builder
225 * @throws NullPointerException if the specified value is null
226 * @throws IndexOutOfBoundsException if the index is out of range
227 * {@code (index < 0 || index > array size)}
228 *
229 * @since 1.1
230 */
231 default JsonArrayBuilder add(int index, String value) {
232 throw new UnsupportedOperationException();
233 }
234
235 /**
236 * Adds a value to the array as a {@link JsonNumber} at the specified position.
237 * Shifts the value currently at that position (if any) and any subsequent values
238 * to the right (adds one to their indices). Index starts with 0.
239 *
240 * @param index the position in the array
241 * @param value the number value
242 * @return this array builder
243 * @throws NullPointerException if the specified value is null
244 * @throws IndexOutOfBoundsException if the index is out of range
245 * {@code (index < 0 || index > array size)}
246 *
247 * @see JsonNumber
248 *
249 * @since 1.1
250 */
251 default JsonArrayBuilder add(int index, BigDecimal value) {
252 throw new UnsupportedOperationException();
253 }
254
255 /**
256 * Adds a value to the array as a {@link JsonNumber} at the specified position.
257 * Shifts the value currently at that position (if any) and any subsequent values
258 * to the right (adds one to their indices). Index starts with 0.
259 *
260 * @param index the position in the array
261 * @param value the number value
262 * @return this array builder
263 * @throws NullPointerException if the specified value is null
264 * @throws IndexOutOfBoundsException if the index is out of range
265 * {@code (index < 0 || index > array size)}
266 *
267 * @see JsonNumber
268 *
269 * @since 1.1
270 */
271 default JsonArrayBuilder add(int index, BigInteger value) {
272 throw new UnsupportedOperationException();
273 }
274
275 /**
276 * Adds a value to the array as a {@link JsonNumber} at the specified position.
277 * Shifts the value currently at that position (if any) and any subsequent values
278 * to the right (adds one to their indices). Index starts with 0.
279 *
280 * @param index the position in the array
281 * @param value the number value
282 * @return this array builder
283 * @throws IndexOutOfBoundsException if the index is out of range
284 * {@code (index < 0 || index > array size)}
285 *
286 * @see JsonNumber
287 *
288 * @since 1.1
289 */
290 default JsonArrayBuilder add(int index, int value) {
291 throw new UnsupportedOperationException();
292 }
293
294 /**
295 * Adds a value to the array as a {@link JsonNumber} at the specified position.
296 * Shifts the value currently at that position (if any) and any subsequent values
297 * to the right (adds one to their indices). Index starts with 0.
298 *
299 * @param index the position in the array
300 * @param value the number value
301 * @return this array builder
302 * @throws IndexOutOfBoundsException if the index is out of range
303 * {@code (index < 0 || index > array size)}
304 *
305 * @see JsonNumber
306 *
307 * @since 1.1
308 */
309 default JsonArrayBuilder add(int index, long value) {
310 throw new UnsupportedOperationException();
311 }
312
313 /**
314 * Adds a value to the array as a {@link JsonNumber} at the specified position.
315 * Shifts the value currently at that position (if any) and any subsequent values
316 * to the right (adds one to their indices). Index starts with 0.
317 *
318 * @param index the position in the array
319 * @param value the number value
320 * @return this array builder
321 * @throws NumberFormatException if the value is Not-a-Number (NaN) or
322 * infinity
323 * @throws IndexOutOfBoundsException if the index is out of range
324 * {@code (index < 0 || index > array size)}
325 *
326 * @see JsonNumber
327 *
328 * @since 1.1
329 */
330 default JsonArrayBuilder add(int index, double value) {
331 throw new UnsupportedOperationException();
332 }
333
334 /**
335 * Adds a {@link JsonValue#TRUE} or {@link JsonValue#FALSE} value to the
336 * array at the specified position.
337 * Shifts the value currently at that position (if any) and any subsequent values
338 * to the right (adds one to their indices). Index starts with 0.
339 *
340 * @param index the position in the array
341 * @param value the boolean value
342 * @return this array builder
343 * @throws IndexOutOfBoundsException if the index is out of range
344 * {@code (index < 0 || index > array size)}
345 *
346 * @since 1.1
347 */
348 default JsonArrayBuilder add(int index, boolean value) {
349 throw new UnsupportedOperationException();
350 }
351
352 /**
353 * Adds a {@link JsonValue#NULL} value to the array at the specified position.
354 * Shifts the value currently at that position (if any) and any subsequent values
355 * to the right (adds one to their indices). Index starts with 0.
356 *
357 * @param index the position in the array
358 * @return this array builder
359 * @throws IndexOutOfBoundsException if the index is out of range
360 * {@code (index < 0 || index > array size)}
361 *
362 * @since 1.1
363 */
364 default JsonArrayBuilder addNull(int index) {
365 return add(index, JsonValue.NULL);
366 }
367
368 /**
369 * Adds a {@link JsonObject} from an object builder to the array at the specified position.
370 * Shifts the value currently at that position (if any) and any subsequent values
371 * to the right (adds one to their indices). Index starts with 0.
372 *
373 * @param index the position in the array
374 * @param builder the object builder
375 * @return this array builder
376 * @throws NullPointerException if the specified builder is null
377 * @throws IndexOutOfBoundsException if the index is out of range
378 * {@code (index < 0 || index > array size)}
379 *
380 * @since 1.1
381 */
382 default JsonArrayBuilder add(int index, JsonObjectBuilder builder) {
383 throw new UnsupportedOperationException();
384 }
385
386 /**
387 * Adds a {@link JsonArray} from an array builder to the array at the specified position.
388 * Shifts the value currently at that position (if any) and any subsequent values
389 * to the right (adds one to their indices). Index starts with 0.
390 *
391 * @param index the position in the array
392 * @param builder the array builder
393 * @return this array builder
394 * @throws NullPointerException if the specified builder is null
395 * @throws IndexOutOfBoundsException if the index is out of range
396 * {@code (index < 0 || index > array size)}
397 *
398 * @since 1.1
399 */
400 default JsonArrayBuilder add(int index, JsonArrayBuilder builder) {
401 throw new UnsupportedOperationException();
402 }
403
404 /**
405 * Replaces a value in the array with the specified value at the
406 * specified position.
407 *
408 * @param index the position in the array
409 * @param value the JSON value
410 * @return this array builder
411 * @throws NullPointerException if the specified value is null
412 * @throws IndexOutOfBoundsException if the index is out of range
413 * {@code (index < 0 || index >= array size)}
414 *
415 * @since 1.1
416 */
417 default JsonArrayBuilder set(int index, JsonValue value) {
418 throw new UnsupportedOperationException();
419 }
420
421 /**
422 * Replaces a value in the array with the specified value as a
423 * {@link JsonString} at the specified position.
424 *
425 * @param index the position in the array
426 * @param value the string value
427 * @return this array builder
428 * @throws NullPointerException if the specified value is null
429 * @throws IndexOutOfBoundsException if the index is out of range
430 * {@code (index < 0 || index >= array size)}
431 *
432 * @since 1.1
433 */
434 default JsonArrayBuilder set(int index, String value) {
435 throw new UnsupportedOperationException();
436 }
437
438 /**
439 * Replaces a value in the array with the specified value as a
440 * {@link JsonNumber} at the specified position.
441 *
442 * @param index the position in the array
443 * @param value the number value
444 * @return this array builder
445 * @throws NullPointerException if the specified value is null
446 * @throws IndexOutOfBoundsException if the index is out of range
447 * {@code (index < 0 || index >= array size)}
448 *
449 * @see JsonNumber
450 *
451 * @since 1.1
452 */
453 default JsonArrayBuilder set(int index, BigDecimal value) {
454 throw new UnsupportedOperationException();
455 }
456
457 /**
458 * Replaces a value in the array with the specified value as a
459 * {@link JsonNumber} at the specified position.
460 *
461 * @param index the position in the array
462 * @param value the number value
463 * @return this array builder
464 * @throws NullPointerException if the specified value is null
465 * @throws IndexOutOfBoundsException if the index is out of range
466 * {@code (index < 0 || index >= array size)}
467 *
468 * @see JsonNumber
469 *
470 * @since 1.1
471 */
472 default JsonArrayBuilder set(int index, BigInteger value) {
473 throw new UnsupportedOperationException();
474 }
475
476 /**
477 * Replaces a value in the array with the specified value as a
478 * {@link JsonNumber} at the specified position.
479 *
480 * @param index the position in the array
481 * @param value the number value
482 * @return this array builder
483 * @throws IndexOutOfBoundsException if the index is out of range
484 * {@code (index < 0 || index >= array size)}
485 *
486 * @see JsonNumber
487 *
488 * @since 1.1
489 */
490 default JsonArrayBuilder set(int index, int value) {
491 throw new UnsupportedOperationException();
492 }
493
494 /**
495 * Replaces a value in the array with the specified value as a
496 * {@link JsonNumber} at the specified position.
497 *
498 * @param index the position in the array
499 * @param value the number value
500 * @return this array builder
501 * @throws IndexOutOfBoundsException if the index is out of range
502 * {@code (index < 0 || index >= array size)}
503 *
504 * @see JsonNumber
505 *
506 * @since 1.1
507 */
508 default JsonArrayBuilder set(int index, long value) {
509 throw new UnsupportedOperationException();
510 }
511
512 /**
513 * Replaces a value in the array with the specified value as a
514 * {@link JsonNumber} at the specified position.
515 *
516 * @param index the position in the array
517 * @param value the number value
518 * @return this array builder
519 * @throws NumberFormatException if the value is Not-a-Number (NaN) or
520 * infinity
521 * @throws IndexOutOfBoundsException if the index is out of range
522 * {@code (index < 0 || index >= array size)}
523 *
524 * @see JsonNumber
525 *
526 * @since 1.1
527 */
528 default JsonArrayBuilder set(int index, double value) {
529 throw new UnsupportedOperationException();
530 }
531
532 /**
533 * Replaces a value in the array with
534 * a {@link JsonValue#TRUE} or {@link JsonValue#FALSE} value
535 * at the specified position.
536 *
537 * @param index the position in the array
538 * @param value the boolean value
539 * @return this array builder
540 * @throws IndexOutOfBoundsException if the index is out of range
541 * {@code (index < 0 || index >= array size)}
542 *
543 * @since 1.1
544 */
545 default JsonArrayBuilder set(int index, boolean value) {
546 throw new UnsupportedOperationException();
547 }
548
549 /**
550 * Replaces a value in the array with
551 * a {@link JsonValue#NULL} value at the specified position.
552 *
553 * @param index the position in the array
554 * @return this array builder
555 * @throws IndexOutOfBoundsException if the index is out of range
556 * {@code (index < 0 || index >= array size)}
557 *
558 * @since 1.1
559 */
560 default JsonArrayBuilder setNull(int index) {
561 return set(index, JsonValue.NULL);
562 }
563
564 /**
565 * Replaces a value in the array with the specified value as a
566 * {@link JsonObject} from an object builder at the specified position.
567 *
568 * @param index the position in the array
569 * @param builder the object builder
570 * @return this array builder
571 * @throws NullPointerException if the specified builder is null
572 * @throws IndexOutOfBoundsException if the index is out of range
573 * {@code (index < 0 || index >= array size)}
574 *
575 * @since 1.1
576 */
577 default JsonArrayBuilder set(int index, JsonObjectBuilder builder) {
578 throw new UnsupportedOperationException();
579 }
580
581 /**
582 * Replaces a value in the array with the specified value as a
583 * {@link JsonArray} from an array builder at the specified position.
584 *
585 * @param index the position in the array
586 * @param builder the array builder
587 * @return this array builder
588 * @throws NullPointerException if the specified builder is null
589 * @throws IndexOutOfBoundsException if the index is out of range
590 * {@code (index < 0 || index >= array size)}
591 *
592 * @since 1.1
593 */
594 default JsonArrayBuilder set(int index, JsonArrayBuilder builder) {
595 throw new UnsupportedOperationException();
596 }
597
598 /**
599 * Remove the value in the array at the specified position.
600 * Shift any subsequent values to the left (subtracts one from their
601 * indices.
602 *
603 * @param index the position in the array
604 * @return this array builder
605 * @throws IndexOutOfBoundsException if the index is out of range
606 * {@code (index < 0 || index >= array size)}
607 *
608 * @since 1.1
609 */
610 default JsonArrayBuilder remove(int index) {
611 throw new UnsupportedOperationException();
612 }
613
614 /**
615 * Returns the current array.
616 *
617 * @return the current JSON array
618 */
619 JsonArray build();
620
621 }
622