Debian Codegraph @

src/jakarta/json/EmptyArray.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2017, 2022 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.io.Serializable;
20 import java.util.AbstractList;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.RandomAccess;
24
25 /**
26 * Private implementation of immutable {@link JsonArray}.
27 *
28 * @author Lukas Jungmann
29 */
30 final class EmptyArray extends AbstractList<JsonValue> implements JsonArray, Serializable, RandomAccess {
31
32 /** for serialization */
33 private static final long serialVersionUID = 7295439472061642859L;
34
35 /** Default constructor. */
36 EmptyArray() {}
37
38 @Override
39 public JsonValue get(int index) {
40 throw new IndexOutOfBoundsException("Index: " + index);
41 }
42
43 @Override
44 public int size() {
45 return 0;
46 }
47
48 @Override
49 public JsonObject getJsonObject(int index) {
50 return (JsonObject) get(index);
51 }
52
53 @Override
54 public JsonArray getJsonArray(int index) {
55 return (JsonArray) get(index);
56 }
57
58 @Override
59 public JsonNumber getJsonNumber(int index) {
60 return (JsonNumber) get(index);
61 }
62
63 @Override
64 public JsonString getJsonString(int index) {
65 return (JsonString) get(index);
66 }
67
68 @Override
69 public <T extends JsonValue> List<T> getValuesAs(Class<T> clazz) {
70 return Collections.emptyList();
71 }
72
73 @Override
74 public String getString(int index) {
75 return getJsonString(index).getString();
76 }
77
78 @Override
79 public String getString(int index, String defaultValue) {
80 return defaultValue;
81 }
82
83 @Override
84 public int getInt(int index) {
85 return getJsonNumber(index).intValue();
86 }
87
88 @Override
89 public int getInt(int index, int defaultValue) {
90 return defaultValue;
91 }
92
93 @Override
94 public boolean getBoolean(int index) {
95 return get(index) == JsonValue.TRUE;
96 }
97
98 @Override
99 public boolean getBoolean(int index, boolean defaultValue) {
100 return defaultValue;
101 }
102
103 @Override
104 public boolean isNull(int index) {
105 return get(index) == JsonValue.NULL;
106 }
107
108 @Override
109 public ValueType getValueType() {
110 return ValueType.ARRAY;
111 }
112
113 /**
114 * Preserves singleton property
115 * @return {@link JsonValue#EMPTY_JSON_ARRAY}
116 */
117 private Object readResolve() {
118 return JsonValue.EMPTY_JSON_ARRAY;
119 }
120 }