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/JsonObjectBuilderImpl.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:
     
    5151
    5252/**
    53  * JsonObjectBuilder impl
     53 * JsonObjectBuilder implementation
    5454 *
    5555 * @author Jitendra Kotamraju
     56 * @author Kin-man Chung
    5657 */
    5758class JsonObjectBuilderImpl implements JsonObjectBuilder {
     59
    5860    private Map<String, JsonValue> valueMap;
    5961    private final BufferPool bufferPool;
     
    6365    }
    6466
     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
    6580    public JsonObjectBuilder add(String name, JsonValue value) {
    6681        validateName(name);
     
    7085    }
    7186
     87    @Override
    7288    public JsonObjectBuilder add(String name, String value) {
    7389        validateName(name);
     
    7793    }
    7894
     95    @Override
    7996    public JsonObjectBuilder add(String name, BigInteger value) {
    8097        validateName(name);
     
    84101    }
    85102
     103    @Override
    86104    public JsonObjectBuilder add(String name, BigDecimal value) {
    87105        validateName(name);
     
    91109    }
    92110
     111    @Override
    93112    public JsonObjectBuilder add(String name, int value) {
    94113        validateName(name);
     
    97116    }
    98117
     118    @Override
    99119    public JsonObjectBuilder add(String name, long value) {
    100120        validateName(name);
     
    103123    }
    104124
     125    @Override
    105126    public JsonObjectBuilder add(String name, double value) {
    106127        validateName(name);
     
    109130    }
    110131
     132    @Override
    111133    public JsonObjectBuilder add(String name, boolean value) {
    112134        validateName(name);
     
    115137    }
    116138
     139    @Override
    117140    public JsonObjectBuilder addNull(String name) {
    118141        validateName(name);
     
    121144    }
    122145
     146    @Override
    123147    public JsonObjectBuilder add(String name, JsonObjectBuilder builder) {
    124148        validateName(name);
     
    130154    }
    131155
     156    @Override
    132157    public JsonObjectBuilder add(String name, JsonArrayBuilder builder) {
    133158        validateName(name);
     
    139164    }
    140165
     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
    141186    public JsonObject build() {
    142187        Map<String, JsonValue> snapshot = (valueMap == null)
     
    147192    }
    148193
     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
    149207    private void putValueMap(String name, JsonValue value) {
    150208        if (valueMap == null) {
    151             this.valueMap = new LinkedHashMap<String, JsonValue>();
     209            this.valueMap = new LinkedHashMap<>();
    152210        }
    153211        valueMap.put(name, value);
     
    264322        public String toString() {
    265323            StringWriter sw = new StringWriter();
    266             JsonWriter jw = new JsonWriterImpl(sw, bufferPool);
    267             jw.write(this);
    268             jw.close();
     324            try (JsonWriter jw = new JsonWriterImpl(sw, bufferPool)) {
     325                jw.write(this);
     326            }
    269327            return sw.toString();
    270328        }
     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        }
    271349    }
    272350
Note: See TracChangeset for help on using the changeset viewer.