Changeset 13231 in josm for trunk/src/org/glassfish/json/JsonArrayBuilderImpl.java
- Timestamp:
- 2017-12-23T02:40:43+01:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/glassfish/json/JsonArrayBuilderImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2012-201 3Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 49 49 import java.util.AbstractList; 50 50 import java.util.ArrayList; 51 import java.util.Collection; 51 52 import java.util.Collections; 52 53 import java.util.List; 54 import java.util.Optional; 53 55 54 56 /** 55 * JsonArrayBuilder impl 57 * JsonArrayBuilder implementation 56 58 * 57 59 * @author Jitendra Kotamraju 60 * @author Kin-man Chung 58 61 */ 62 59 63 class JsonArrayBuilderImpl implements JsonArrayBuilder { 60 64 private ArrayList<JsonValue> valueList; … … 65 69 } 66 70 71 JsonArrayBuilderImpl(JsonArray array, BufferPool bufferPool) { 72 this.bufferPool = bufferPool; 73 valueList = new ArrayList<>(); 74 valueList.addAll(array); 75 } 76 77 JsonArrayBuilderImpl(Collection<?> collection, BufferPool bufferPool) { 78 this.bufferPool = bufferPool; 79 valueList = new ArrayList<>(); 80 populate(collection); 81 } 82 83 @Override 67 84 public JsonArrayBuilder add(JsonValue value) { 68 85 validateValue(value); … … 71 88 } 72 89 90 @Override 73 91 public JsonArrayBuilder add(String value) { 74 92 validateValue(value); … … 77 95 } 78 96 97 @Override 79 98 public JsonArrayBuilder add(BigDecimal value) { 80 99 validateValue(value); … … 83 102 } 84 103 104 @Override 85 105 public JsonArrayBuilder add(BigInteger value) { 86 106 validateValue(value); … … 89 109 } 90 110 111 @Override 91 112 public JsonArrayBuilder add(int value) { 92 113 addValueList(JsonNumberImpl.getJsonNumber(value)); … … 94 115 } 95 116 117 @Override 96 118 public JsonArrayBuilder add(long value) { 97 119 addValueList(JsonNumberImpl.getJsonNumber(value)); … … 99 121 } 100 122 123 @Override 101 124 public JsonArrayBuilder add(double value) { 102 125 addValueList(JsonNumberImpl.getJsonNumber(value)); … … 104 127 } 105 128 129 @Override 106 130 public JsonArrayBuilder add(boolean value) { 107 131 addValueList(value ? JsonValue.TRUE : JsonValue.FALSE); … … 109 133 } 110 134 135 @Override 111 136 public JsonArrayBuilder addNull() { 112 137 addValueList(JsonValue.NULL); … … 114 139 } 115 140 141 @Override 116 142 public JsonArrayBuilder add(JsonObjectBuilder builder) { 117 143 if (builder == null) { … … 122 148 } 123 149 150 @Override 124 151 public JsonArrayBuilder add(JsonArrayBuilder builder) { 125 152 if (builder == null) { … … 130 157 } 131 158 159 @Override 160 public JsonArrayBuilder addAll(JsonArrayBuilder builder) { 161 if (builder == null) { 162 throw new NullPointerException(JsonMessages.ARRBUILDER_ARRAY_BUILDER_NULL()); 163 } 164 if (valueList == null) { 165 valueList = new ArrayList<>(); 166 } 167 valueList.addAll(builder.build()); 168 return this; 169 } 170 171 @Override 172 public JsonArrayBuilder add(int index, JsonValue value) { 173 validateValue(value); 174 addValueList(index, value); 175 return this; 176 } 177 178 @Override 179 public JsonArrayBuilder add(int index, String value) { 180 validateValue(value); 181 addValueList(index, new JsonStringImpl(value)); 182 return this; 183 } 184 185 @Override 186 public JsonArrayBuilder add(int index, BigDecimal value) { 187 validateValue(value); 188 addValueList(index, JsonNumberImpl.getJsonNumber(value)); 189 return this; 190 } 191 192 @Override 193 public JsonArrayBuilder add(int index, BigInteger value) { 194 validateValue(value); 195 addValueList(index, JsonNumberImpl.getJsonNumber(value)); 196 return this; 197 } 198 199 @Override 200 public JsonArrayBuilder add(int index, int value) { 201 addValueList(index, JsonNumberImpl.getJsonNumber(value)); 202 return this; 203 } 204 205 @Override 206 public JsonArrayBuilder add(int index, long value) { 207 addValueList(index, JsonNumberImpl.getJsonNumber(value)); 208 return this; 209 } 210 211 @Override 212 public JsonArrayBuilder add(int index, double value) { 213 addValueList(index, JsonNumberImpl.getJsonNumber(value)); 214 return this; 215 } 216 217 @Override 218 public JsonArrayBuilder add(int index, boolean value) { 219 addValueList(index, value ? JsonValue.TRUE : JsonValue.FALSE); 220 return this; 221 } 222 223 @Override 224 public JsonArrayBuilder addNull(int index) { 225 addValueList(index, JsonValue.NULL); 226 return this; 227 } 228 229 @Override 230 public JsonArrayBuilder add(int index, JsonObjectBuilder builder) { 231 if (builder == null) { 232 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL()); 233 } 234 addValueList(index, builder.build()); 235 return this; 236 } 237 238 @Override 239 public JsonArrayBuilder add(int index, JsonArrayBuilder builder) { 240 if (builder == null) { 241 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL()); 242 } 243 addValueList(index, builder.build()); 244 return this; 245 } 246 247 @Override 248 public JsonArrayBuilder set(int index, JsonValue value) { 249 validateValue(value); 250 setValueList(index, value); 251 return this; 252 } 253 254 @Override 255 public JsonArrayBuilder set(int index, String value) { 256 validateValue(value); 257 setValueList(index, new JsonStringImpl(value)); 258 return this; 259 } 260 261 @Override 262 public JsonArrayBuilder set(int index, BigDecimal value) { 263 validateValue(value); 264 setValueList(index, JsonNumberImpl.getJsonNumber(value)); 265 return this; 266 } 267 268 @Override 269 public JsonArrayBuilder set(int index, BigInteger value) { 270 validateValue(value); 271 setValueList(index, JsonNumberImpl.getJsonNumber(value)); 272 return this; 273 } 274 275 @Override 276 public JsonArrayBuilder set(int index, int value) { 277 setValueList(index, JsonNumberImpl.getJsonNumber(value)); 278 return this; 279 } 280 281 @Override 282 public JsonArrayBuilder set(int index, long value) { 283 setValueList(index, JsonNumberImpl.getJsonNumber(value)); 284 return this; 285 } 286 287 @Override 288 public JsonArrayBuilder set(int index, double value) { 289 setValueList(index, JsonNumberImpl.getJsonNumber(value)); 290 return this; 291 } 292 293 @Override 294 public JsonArrayBuilder set(int index, boolean value) { 295 setValueList(index, value ? JsonValue.TRUE : JsonValue.FALSE); 296 return this; 297 } 298 299 @Override 300 public JsonArrayBuilder setNull(int index) { 301 setValueList(index, JsonValue.NULL); 302 return this; 303 } 304 305 @Override 306 public JsonArrayBuilder set(int index, JsonObjectBuilder builder) { 307 if (builder == null) { 308 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL()); 309 } 310 setValueList(index, builder.build()); 311 return this; 312 } 313 314 @Override 315 public JsonArrayBuilder set(int index, JsonArrayBuilder builder) { 316 if (builder == null) { 317 throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL()); 318 } 319 setValueList(index, builder.build()); 320 return this; 321 } 322 323 @Override 324 public JsonArrayBuilder remove(int index) { 325 if (valueList == null) { 326 throw new IndexOutOfBoundsException(JsonMessages.ARRBUILDER_VALUELIST_NULL(index, 0)); 327 } 328 valueList.remove(index); 329 return this; 330 } 331 332 @Override 132 333 public JsonArray build() { 133 334 List<JsonValue> snapshot; … … 143 344 } 144 345 346 private void populate(Collection<?> collection) { 347 for (Object value : collection) { 348 if (value != null && value instanceof Optional) { 349 ((Optional<?>) value).ifPresent(v -> 350 this.valueList.add(MapUtil.handle(v, bufferPool))); 351 } else { 352 this.valueList.add(MapUtil.handle(value, bufferPool)); 353 } 354 } 355 } 356 145 357 private void addValueList(JsonValue value) { 146 358 if (valueList == null) { 147 valueList = new ArrayList< JsonValue>();359 valueList = new ArrayList<>(); 148 360 } 149 361 valueList.add(value); 362 } 363 364 private void addValueList(int index, JsonValue value) { 365 if (valueList == null) { 366 valueList = new ArrayList<>(); 367 } 368 valueList.add(index, value); 369 } 370 371 private void setValueList(int index, JsonValue value) { 372 if (valueList == null) { 373 throw new IndexOutOfBoundsException(JsonMessages.ARRBUILDER_VALUELIST_NULL(index, 0)); 374 } 375 valueList.set(index, value); 150 376 } 151 377 … … 263 489 public String toString() { 264 490 StringWriter sw = new StringWriter(); 265 JsonWriter jw = new JsonWriterImpl(sw, bufferPool) ;266 jw.write(this); 267 jw.close();491 try (JsonWriter jw = new JsonWriterImpl(sw, bufferPool)) { 492 jw.write(this); 493 } 268 494 return sw.toString(); 269 495 } 270 } 271 496 497 @Override 498 public JsonArray asJsonArray() { 499 return this; 500 } 501 } 272 502 } 273 503 274 275
Note:
See TracChangeset
for help on using the changeset viewer.