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

Last change on this file since 10399 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.7 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.JsonArrayBuilder;
46import javax.json.*;
47import java.io.StringWriter;
48import java.math.BigDecimal;
49import java.math.BigInteger;
50import java.util.*;
51
52/**
53 * JsonObjectBuilder impl
54 *
55 * @author Jitendra Kotamraju
56 */
57class JsonObjectBuilderImpl implements JsonObjectBuilder {
58 private Map<String, JsonValue> valueMap;
59 private final BufferPool bufferPool;
60
61 JsonObjectBuilderImpl(BufferPool bufferPool) {
62 this.bufferPool = bufferPool;
63 }
64
65 public JsonObjectBuilder add(String name, JsonValue value) {
66 validateName(name);
67 validateValue(value);
68 putValueMap(name, value);
69 return this;
70 }
71
72 public JsonObjectBuilder add(String name, String value) {
73 validateName(name);
74 validateValue(value);
75 putValueMap(name, new JsonStringImpl(value));
76 return this;
77 }
78
79 public JsonObjectBuilder add(String name, BigInteger value) {
80 validateName(name);
81 validateValue(value);
82 putValueMap(name, JsonNumberImpl.getJsonNumber(value));
83 return this;
84 }
85
86 public JsonObjectBuilder add(String name, BigDecimal value) {
87 validateName(name);
88 validateValue(value);
89 putValueMap(name, JsonNumberImpl.getJsonNumber(value));
90 return this;
91 }
92
93 public JsonObjectBuilder add(String name, int value) {
94 validateName(name);
95 putValueMap(name, JsonNumberImpl.getJsonNumber(value));
96 return this;
97 }
98
99 public JsonObjectBuilder add(String name, long value) {
100 validateName(name);
101 putValueMap(name, JsonNumberImpl.getJsonNumber(value));
102 return this;
103 }
104
105 public JsonObjectBuilder add(String name, double value) {
106 validateName(name);
107 putValueMap(name, JsonNumberImpl.getJsonNumber(value));
108 return this;
109 }
110
111 public JsonObjectBuilder add(String name, boolean value) {
112 validateName(name);
113 putValueMap(name, value ? JsonValue.TRUE : JsonValue.FALSE);
114 return this;
115 }
116
117 public JsonObjectBuilder addNull(String name) {
118 validateName(name);
119 putValueMap(name, JsonValue.NULL);
120 return this;
121 }
122
123 public JsonObjectBuilder add(String name, JsonObjectBuilder builder) {
124 validateName(name);
125 if (builder == null) {
126 throw new NullPointerException(JsonMessages.OBJBUILDER_OBJECT_BUILDER_NULL());
127 }
128 putValueMap(name, builder.build());
129 return this;
130 }
131
132 public JsonObjectBuilder add(String name, JsonArrayBuilder builder) {
133 validateName(name);
134 if (builder == null) {
135 throw new NullPointerException(JsonMessages.OBJBUILDER_ARRAY_BUILDER_NULL());
136 }
137 putValueMap(name, builder.build());
138 return this;
139 }
140
141 public JsonObject build() {
142 Map<String, JsonValue> snapshot = (valueMap == null)
143 ? Collections.<String, JsonValue>emptyMap()
144 : Collections.unmodifiableMap(valueMap);
145 valueMap = null;
146 return new JsonObjectImpl(snapshot, bufferPool);
147 }
148
149 private void putValueMap(String name, JsonValue value) {
150 if (valueMap == null) {
151 this.valueMap = new LinkedHashMap<String, JsonValue>();
152 }
153 valueMap.put(name, value);
154 }
155
156 private void validateName(String name) {
157 if (name == null) {
158 throw new NullPointerException(JsonMessages.OBJBUILDER_NAME_NULL());
159 }
160 }
161
162 private void validateValue(Object value) {
163 if (value == null) {
164 throw new NullPointerException(JsonMessages.OBJBUILDER_VALUE_NULL());
165 }
166 }
167
168 private static final class JsonObjectImpl extends AbstractMap<String, JsonValue> implements JsonObject {
169 private final Map<String, JsonValue> valueMap; // unmodifiable
170 private final BufferPool bufferPool;
171
172 JsonObjectImpl(Map<String, JsonValue> valueMap, BufferPool bufferPool) {
173 this.valueMap = valueMap;
174 this.bufferPool = bufferPool;
175 }
176
177 @Override
178 public JsonArray getJsonArray(String name) {
179 return (JsonArray)get(name);
180 }
181
182 @Override
183 public JsonObject getJsonObject(String name) {
184 return (JsonObject)get(name);
185 }
186
187 @Override
188 public JsonNumber getJsonNumber(String name) {
189 return (JsonNumber)get(name);
190 }
191
192 @Override
193 public JsonString getJsonString(String name) {
194 return (JsonString)get(name);
195 }
196
197 @Override
198 public String getString(String name) {
199 return getJsonString(name).getString();
200 }
201
202 @Override
203 public String getString(String name, String defaultValue) {
204 try {
205 return getString(name);
206 } catch (Exception e) {
207 return defaultValue;
208 }
209 }
210
211 @Override
212 public int getInt(String name) {
213 return getJsonNumber(name).intValue();
214 }
215
216 @Override
217 public int getInt(String name, int defaultValue) {
218 try {
219 return getInt(name);
220 } catch (Exception e) {
221 return defaultValue;
222 }
223 }
224
225 @Override
226 public boolean getBoolean(String name) {
227 JsonValue value = get(name);
228 if (value == null) {
229 throw new NullPointerException();
230 } else if (value == JsonValue.TRUE) {
231 return true;
232 } else if (value == JsonValue.FALSE) {
233 return false;
234 } else {
235 throw new ClassCastException();
236 }
237 }
238
239 @Override
240 public boolean getBoolean(String name, boolean defaultValue) {
241 try {
242 return getBoolean(name);
243 } catch (Exception e) {
244 return defaultValue;
245 }
246 }
247
248 @Override
249 public boolean isNull(String name) {
250 return get(name).equals(JsonValue.NULL);
251 }
252
253 @Override
254 public ValueType getValueType() {
255 return ValueType.OBJECT;
256 }
257
258 @Override
259 public Set<Entry<String, JsonValue>> entrySet() {
260 return valueMap.entrySet();
261 }
262
263 @Override
264 public String toString() {
265 StringWriter sw = new StringWriter();
266 JsonWriter jw = new JsonWriterImpl(sw, bufferPool);
267 jw.write(this);
268 jw.close();
269 return sw.toString();
270 }
271 }
272
273}
Note: See TracBrowser for help on using the repository browser.