Ignore:
Timestamp:
2017-12-23T02:40:43+01:00 (8 years ago)
Author:
Don-vip
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/glassfish/json/JsonArrayBuilderImpl.java

    r6756 r13231  
    22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    33 *
    4  * Copyright (c) 2012-2013 Oracle and/or its affiliates. All rights reserved.
     4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved.
    55 *
    66 * The contents of this file are subject to the terms of either the GNU
     
    99 * may not use this file except in compliance with the License.  You can
    1010 * 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
     11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1
     12 * or LICENSE.txt.  See the License for the specific
    1313 * language governing permissions and limitations under the License.
    1414 *
    1515 * 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.
    1717 *
    1818 * GPL Classpath Exception:
     
    4949import java.util.AbstractList;
    5050import java.util.ArrayList;
     51import java.util.Collection;
    5152import java.util.Collections;
    5253import java.util.List;
     54import java.util.Optional;
    5355
    5456/**
    55  * JsonArrayBuilder impl
     57 * JsonArrayBuilder implementation
    5658 *
    5759 * @author Jitendra Kotamraju
     60 * @author Kin-man Chung
    5861 */
     62
    5963class JsonArrayBuilderImpl implements JsonArrayBuilder {
    6064    private ArrayList<JsonValue> valueList;
     
    6569    }
    6670
     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
    6784    public JsonArrayBuilder add(JsonValue value) {
    6885        validateValue(value);
     
    7188    }
    7289
     90    @Override
    7391    public JsonArrayBuilder add(String value) {
    7492        validateValue(value);
     
    7795    }
    7896
     97    @Override
    7998    public JsonArrayBuilder add(BigDecimal value) {
    8099        validateValue(value);
     
    83102    }
    84103
     104    @Override
    85105    public JsonArrayBuilder add(BigInteger value) {
    86106        validateValue(value);
     
    89109    }
    90110
     111    @Override
    91112    public JsonArrayBuilder add(int value) {
    92113        addValueList(JsonNumberImpl.getJsonNumber(value));
     
    94115    }
    95116
     117    @Override
    96118    public JsonArrayBuilder add(long value) {
    97119        addValueList(JsonNumberImpl.getJsonNumber(value));
     
    99121    }
    100122
     123    @Override
    101124    public JsonArrayBuilder add(double value) {
    102125        addValueList(JsonNumberImpl.getJsonNumber(value));
     
    104127    }
    105128
     129    @Override
    106130    public JsonArrayBuilder add(boolean value) {
    107131        addValueList(value ? JsonValue.TRUE : JsonValue.FALSE);
     
    109133    }
    110134
     135    @Override
    111136    public JsonArrayBuilder addNull() {
    112137        addValueList(JsonValue.NULL);
     
    114139    }
    115140
     141    @Override
    116142    public JsonArrayBuilder add(JsonObjectBuilder builder) {
    117143        if (builder == null) {
     
    122148    }
    123149
     150    @Override
    124151    public JsonArrayBuilder add(JsonArrayBuilder builder) {
    125152        if (builder == null) {
     
    130157    }
    131158
     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
    132333    public JsonArray build() {
    133334        List<JsonValue> snapshot;
     
    143344    }
    144345
     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
    145357    private void addValueList(JsonValue value) {
    146358        if (valueList == null) {
    147             valueList = new ArrayList<JsonValue>();
     359            valueList = new ArrayList<>();
    148360        }
    149361        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);
    150376    }
    151377
     
    263489        public String toString() {
    264490            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            }
    268494            return sw.toString();
    269495        }
    270     }
    271 
     496
     497        @Override
     498        public JsonArray asJsonArray() {
     499            return this;
     500        }
     501    }
    272502}
    273503
    274 
    275 
Note: See TracChangeset for help on using the changeset viewer.