source: josm/trunk/src/org/glassfish/json/JsonObjectBuilderImpl.java@ 13430

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

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

File size: 10.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.JsonArrayBuilder;
46import javax.json.*;
47import java.io.StringWriter;
48import java.math.BigDecimal;
49import java.math.BigInteger;
50import java.util.*;
51
52/**
53 * JsonObjectBuilder implementation
54 *
55 * @author Jitendra Kotamraju
56 * @author Kin-man Chung
57 */
58class JsonObjectBuilderImpl implements JsonObjectBuilder {
59
60 private Map<String, JsonValue> valueMap;
61 private final BufferPool bufferPool;
62
63 JsonObjectBuilderImpl(BufferPool bufferPool) {
64 this.bufferPool = bufferPool;
65 }
66
67 JsonObjectBuilderImpl(JsonObject object, BufferPool bufferPool) {
68 this.bufferPool = bufferPool;
69 valueMap = new LinkedHashMap<>();
70 valueMap.putAll(object);
71 }
72
73 JsonObjectBuilderImpl(Map<String, Object> map, BufferPool bufferPool) {
74 this.bufferPool = bufferPool;
75 valueMap = new LinkedHashMap<>();
76 populate(map);
77 }
78
79 @Override
80 public JsonObjectBuilder add(String name, JsonValue value) {
81 validateName(name);
82 validateValue(value);
83 putValueMap(name, value);
84 return this;
85 }
86
87 @Override
88 public JsonObjectBuilder add(String name, String value) {
89 validateName(name);
90 validateValue(value);
91 putValueMap(name, new JsonStringImpl(value));
92 return this;
93 }
94
95 @Override
96 public JsonObjectBuilder add(String name, BigInteger value) {
97 validateName(name);
98 validateValue(value);
99 putValueMap(name, JsonNumberImpl.getJsonNumber(value));
100 return this;
101 }
102
103 @Override
104 public JsonObjectBuilder add(String name, BigDecimal value) {
105 validateName(name);
106 validateValue(value);
107 putValueMap(name, JsonNumberImpl.getJsonNumber(value));
108 return this;
109 }
110
111 @Override
112 public JsonObjectBuilder add(String name, int value) {
113 validateName(name);
114 putValueMap(name, JsonNumberImpl.getJsonNumber(value));
115 return this;
116 }
117
118 @Override
119 public JsonObjectBuilder add(String name, long value) {
120 validateName(name);
121 putValueMap(name, JsonNumberImpl.getJsonNumber(value));
122 return this;
123 }
124
125 @Override
126 public JsonObjectBuilder add(String name, double value) {
127 validateName(name);
128 putValueMap(name, JsonNumberImpl.getJsonNumber(value));
129 return this;
130 }
131
132 @Override
133 public JsonObjectBuilder add(String name, boolean value) {
134 validateName(name);
135 putValueMap(name, value ? JsonValue.TRUE : JsonValue.FALSE);
136 return this;
137 }
138
139 @Override
140 public JsonObjectBuilder addNull(String name) {
141 validateName(name);
142 putValueMap(name, JsonValue.NULL);
143 return this;
144 }
145
146 @Override
147 public JsonObjectBuilder add(String name, JsonObjectBuilder builder) {
148 validateName(name);
149 if (builder == null) {
150 throw new NullPointerException(JsonMessages.OBJBUILDER_OBJECT_BUILDER_NULL());
151 }
152 putValueMap(name, builder.build());
153 return this;
154 }
155
156 @Override
157 public JsonObjectBuilder add(String name, JsonArrayBuilder builder) {
158 validateName(name);
159 if (builder == null) {
160 throw new NullPointerException(JsonMessages.OBJBUILDER_ARRAY_BUILDER_NULL());
161 }
162 putValueMap(name, builder.build());
163 return this;
164 }
165
166 @Override
167 public JsonObjectBuilder addAll(JsonObjectBuilder builder) {
168 if (builder == null) {
169 throw new NullPointerException(JsonMessages.OBJBUILDER_OBJECT_BUILDER_NULL());
170 }
171 if (valueMap == null) {
172 this.valueMap = new LinkedHashMap<>();
173 }
174 this.valueMap.putAll(builder.build());
175 return this;
176 }
177
178 @Override
179 public JsonObjectBuilder remove(String name) {
180 validateName(name);
181 this.valueMap.remove(name);
182 return this;
183 }
184
185 @Override
186 public JsonObject build() {
187 Map<String, JsonValue> snapshot = (valueMap == null)
188 ? Collections.<String, JsonValue>emptyMap()
189 : Collections.unmodifiableMap(valueMap);
190 valueMap = null;
191 return new JsonObjectImpl(snapshot, bufferPool);
192 }
193
194 private void populate(Map<String, Object> map) {
195 final Set<String> fields = map.keySet();
196 for (String field : fields) {
197 Object value = map.get(field);
198 if (value != null && value instanceof Optional) {
199 ((Optional<?>) value).ifPresent(v ->
200 this.valueMap.put(field, MapUtil.handle(v, bufferPool)));
201 } else {
202 this.valueMap.put(field, MapUtil.handle(value, bufferPool));
203 }
204 }
205 }
206
207 private void putValueMap(String name, JsonValue value) {
208 if (valueMap == null) {
209 this.valueMap = new LinkedHashMap<>();
210 }
211 valueMap.put(name, value);
212 }
213
214 private void validateName(String name) {
215 if (name == null) {
216 throw new NullPointerException(JsonMessages.OBJBUILDER_NAME_NULL());
217 }
218 }
219
220 private void validateValue(Object value) {
221 if (value == null) {
222 throw new NullPointerException(JsonMessages.OBJBUILDER_VALUE_NULL());
223 }
224 }
225
226 private static final class JsonObjectImpl extends AbstractMap<String, JsonValue> implements JsonObject {
227 private final Map<String, JsonValue> valueMap; // unmodifiable
228 private final BufferPool bufferPool;
229
230 JsonObjectImpl(Map<String, JsonValue> valueMap, BufferPool bufferPool) {
231 this.valueMap = valueMap;
232 this.bufferPool = bufferPool;
233 }
234
235 @Override
236 public JsonArray getJsonArray(String name) {
237 return (JsonArray)get(name);
238 }
239
240 @Override
241 public JsonObject getJsonObject(String name) {
242 return (JsonObject)get(name);
243 }
244
245 @Override
246 public JsonNumber getJsonNumber(String name) {
247 return (JsonNumber)get(name);
248 }
249
250 @Override
251 public JsonString getJsonString(String name) {
252 return (JsonString)get(name);
253 }
254
255 @Override
256 public String getString(String name) {
257 return getJsonString(name).getString();
258 }
259
260 @Override
261 public String getString(String name, String defaultValue) {
262 try {
263 return getString(name);
264 } catch (Exception e) {
265 return defaultValue;
266 }
267 }
268
269 @Override
270 public int getInt(String name) {
271 return getJsonNumber(name).intValue();
272 }
273
274 @Override
275 public int getInt(String name, int defaultValue) {
276 try {
277 return getInt(name);
278 } catch (Exception e) {
279 return defaultValue;
280 }
281 }
282
283 @Override
284 public boolean getBoolean(String name) {
285 JsonValue value = get(name);
286 if (value == null) {
287 throw new NullPointerException();
288 } else if (value == JsonValue.TRUE) {
289 return true;
290 } else if (value == JsonValue.FALSE) {
291 return false;
292 } else {
293 throw new ClassCastException();
294 }
295 }
296
297 @Override
298 public boolean getBoolean(String name, boolean defaultValue) {
299 try {
300 return getBoolean(name);
301 } catch (Exception e) {
302 return defaultValue;
303 }
304 }
305
306 @Override
307 public boolean isNull(String name) {
308 return get(name).equals(JsonValue.NULL);
309 }
310
311 @Override
312 public ValueType getValueType() {
313 return ValueType.OBJECT;
314 }
315
316 @Override
317 public Set<Entry<String, JsonValue>> entrySet() {
318 return valueMap.entrySet();
319 }
320
321 @Override
322 public String toString() {
323 StringWriter sw = new StringWriter();
324 try (JsonWriter jw = new JsonWriterImpl(sw, bufferPool)) {
325 jw.write(this);
326 }
327 return sw.toString();
328 }
329
330 @Override
331 public JsonObject asJsonObject() {
332 return this;
333 }
334
335 @Override
336 public int size() {
337 return valueMap.size();
338 }
339
340 @Override
341 public JsonValue get(Object key) {
342 return valueMap.get(key);
343 }
344
345 @Override
346 public boolean containsKey(Object key) {
347 return valueMap.containsKey(key);
348 }
349 }
350
351}
Note: See TracBrowser for help on using the repository browser.