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

Last change on this file since 12355 was 6756, checked in by Don-vip, 11 years ago

fix #9590 - replace org.json with GPL-compliant jsonp + remove mention of old world image removed in r1680

File size: 8.4 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2012-2013 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://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12 * or packager/legal/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 packager/legal/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.Collections;
52import java.util.List;
53
54/**
55 * JsonArrayBuilder impl
56 *
57 * @author Jitendra Kotamraju
58 */
59class JsonArrayBuilderImpl implements JsonArrayBuilder {
60 private ArrayList<JsonValue> valueList;
61 private final BufferPool bufferPool;
62
63 JsonArrayBuilderImpl(BufferPool bufferPool) {
64 this.bufferPool = bufferPool;
65 }
66
67 public JsonArrayBuilder add(JsonValue value) {
68 validateValue(value);
69 addValueList(value);
70 return this;
71 }
72
73 public JsonArrayBuilder add(String value) {
74 validateValue(value);
75 addValueList(new JsonStringImpl(value));
76 return this;
77 }
78
79 public JsonArrayBuilder add(BigDecimal value) {
80 validateValue(value);
81 addValueList(JsonNumberImpl.getJsonNumber(value));
82 return this;
83 }
84
85 public JsonArrayBuilder add(BigInteger value) {
86 validateValue(value);
87 addValueList(JsonNumberImpl.getJsonNumber(value));
88 return this;
89 }
90
91 public JsonArrayBuilder add(int value) {
92 addValueList(JsonNumberImpl.getJsonNumber(value));
93 return this;
94 }
95
96 public JsonArrayBuilder add(long value) {
97 addValueList(JsonNumberImpl.getJsonNumber(value));
98 return this;
99 }
100
101 public JsonArrayBuilder add(double value) {
102 addValueList(JsonNumberImpl.getJsonNumber(value));
103 return this;
104 }
105
106 public JsonArrayBuilder add(boolean value) {
107 addValueList(value ? JsonValue.TRUE : JsonValue.FALSE);
108 return this;
109 }
110
111 public JsonArrayBuilder addNull() {
112 addValueList(JsonValue.NULL);
113 return this;
114 }
115
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 public JsonArrayBuilder add(JsonArrayBuilder builder) {
125 if (builder == null) {
126 throw new NullPointerException(JsonMessages.ARRBUILDER_ARRAY_BUILDER_NULL());
127 }
128 addValueList(builder.build());
129 return this;
130 }
131
132 public JsonArray build() {
133 List<JsonValue> snapshot;
134 if (valueList == null) {
135 snapshot = Collections.emptyList();
136 } else {
137 // Should we trim to minimize storage ?
138 // valueList.trimToSize();
139 snapshot = Collections.unmodifiableList(valueList);
140 }
141 valueList = null;
142 return new JsonArrayImpl(snapshot, bufferPool);
143 }
144
145 private void addValueList(JsonValue value) {
146 if (valueList == null) {
147 valueList = new ArrayList<JsonValue>();
148 }
149 valueList.add(value);
150 }
151
152 private void validateValue(Object value) {
153 if (value == null) {
154 throw new NullPointerException(JsonMessages.ARRBUILDER_VALUE_NULL());
155 }
156 }
157
158 private static final class JsonArrayImpl extends AbstractList<JsonValue> implements JsonArray {
159 private final List<JsonValue> valueList; // Unmodifiable
160 private final BufferPool bufferPool;
161
162 JsonArrayImpl(List<JsonValue> valueList, BufferPool bufferPool) {
163 this.valueList = valueList;
164 this.bufferPool = bufferPool;
165 }
166
167 @Override
168 public int size() {
169 return valueList.size();
170 }
171
172 @Override
173 public JsonObject getJsonObject(int index) {
174 return (JsonObject)valueList.get(index);
175 }
176
177 @Override
178 public JsonArray getJsonArray(int index) {
179 return (JsonArray)valueList.get(index);
180 }
181
182 @Override
183 public JsonNumber getJsonNumber(int index) {
184 return (JsonNumber)valueList.get(index);
185 }
186
187 @Override
188 public JsonString getJsonString(int index) {
189 return (JsonString)valueList.get(index);
190 }
191
192 @Override
193 @SuppressWarnings("unchecked")
194 public <T extends JsonValue> List<T> getValuesAs(Class<T> clazz) {
195 return (List<T>)valueList;
196 }
197
198 @Override
199 public String getString(int index) {
200 return getJsonString(index).getString();
201 }
202
203 @Override
204 public String getString(int index, String defaultValue) {
205 try {
206 return getString(index);
207 } catch (Exception e) {
208 return defaultValue;
209 }
210 }
211
212 @Override
213 public int getInt(int index) {
214 return getJsonNumber(index).intValue();
215 }
216
217 @Override
218 public int getInt(int index, int defaultValue) {
219 try {
220 return getInt(index);
221 } catch (Exception e) {
222 return defaultValue;
223 }
224 }
225
226 @Override
227 public boolean getBoolean(int index) {
228 JsonValue jsonValue = get(index);
229 if (jsonValue == JsonValue.TRUE) {
230 return true;
231 } else if (jsonValue == JsonValue.FALSE) {
232 return false;
233 } else {
234 throw new ClassCastException();
235 }
236 }
237
238 @Override
239 public boolean getBoolean(int index, boolean defaultValue) {
240 try {
241 return getBoolean(index);
242 } catch (Exception e) {
243 return defaultValue;
244 }
245 }
246
247 @Override
248 public boolean isNull(int index) {
249 return valueList.get(index).equals(JsonValue.NULL);
250 }
251
252 @Override
253 public ValueType getValueType() {
254 return ValueType.ARRAY;
255 }
256
257 @Override
258 public JsonValue get(int index) {
259 return valueList.get(index);
260 }
261
262 @Override
263 public String toString() {
264 StringWriter sw = new StringWriter();
265 JsonWriter jw = new JsonWriterImpl(sw, bufferPool);
266 jw.write(this);
267 jw.close();
268 return sw.toString();
269 }
270 }
271
272}
273
274
275
Note: See TracBrowser for help on using the repository browser.