Index: trunk/src/org/glassfish/json/JsonArrayBuilderImpl.java
===================================================================
--- trunk/src/org/glassfish/json/JsonArrayBuilderImpl.java	(revision 6756)
+++ trunk/src/org/glassfish/json/JsonArrayBuilderImpl.java	(revision 13231)
@@ -2,5 +2,5 @@
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2012-2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
@@ -9,10 +9,10 @@
  * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
- * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
- * or packager/legal/LICENSE.txt.  See the License for the specific
+ * https://oss.oracle.com/licenses/CDDL+GPL-1.1
+ * or LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
- * file and include the License file at packager/legal/LICENSE.txt.
+ * file and include the License file at LICENSE.txt.
  *
  * GPL Classpath Exception:
@@ -49,12 +49,16 @@
 import java.util.AbstractList;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 
 /**
- * JsonArrayBuilder impl
+ * JsonArrayBuilder implementation
  *
  * @author Jitendra Kotamraju
+ * @author Kin-man Chung
  */
+
 class JsonArrayBuilderImpl implements JsonArrayBuilder {
     private ArrayList<JsonValue> valueList;
@@ -65,4 +69,17 @@
     }
 
+    JsonArrayBuilderImpl(JsonArray array, BufferPool bufferPool) {
+        this.bufferPool = bufferPool;
+        valueList = new ArrayList<>();
+        valueList.addAll(array);
+    }
+
+    JsonArrayBuilderImpl(Collection<?> collection, BufferPool bufferPool) {
+        this.bufferPool = bufferPool;
+        valueList = new ArrayList<>();
+        populate(collection);
+    }
+
+    @Override
     public JsonArrayBuilder add(JsonValue value) {
         validateValue(value);
@@ -71,4 +88,5 @@
     }
 
+    @Override
     public JsonArrayBuilder add(String value) {
         validateValue(value);
@@ -77,4 +95,5 @@
     }
 
+    @Override
     public JsonArrayBuilder add(BigDecimal value) {
         validateValue(value);
@@ -83,4 +102,5 @@
     }
 
+    @Override
     public JsonArrayBuilder add(BigInteger value) {
         validateValue(value);
@@ -89,4 +109,5 @@
     }
 
+    @Override
     public JsonArrayBuilder add(int value) {
         addValueList(JsonNumberImpl.getJsonNumber(value));
@@ -94,4 +115,5 @@
     }
 
+    @Override
     public JsonArrayBuilder add(long value) {
         addValueList(JsonNumberImpl.getJsonNumber(value));
@@ -99,4 +121,5 @@
     }
 
+    @Override
     public JsonArrayBuilder add(double value) {
         addValueList(JsonNumberImpl.getJsonNumber(value));
@@ -104,4 +127,5 @@
     }
 
+    @Override
     public JsonArrayBuilder add(boolean value) {
         addValueList(value ? JsonValue.TRUE : JsonValue.FALSE);
@@ -109,4 +133,5 @@
     }
 
+    @Override
     public JsonArrayBuilder addNull() {
         addValueList(JsonValue.NULL);
@@ -114,4 +139,5 @@
     }
 
+    @Override
     public JsonArrayBuilder add(JsonObjectBuilder builder) {
         if (builder == null) {
@@ -122,4 +148,5 @@
     }
 
+    @Override
     public JsonArrayBuilder add(JsonArrayBuilder builder) {
         if (builder == null) {
@@ -130,4 +157,178 @@
     }
 
+    @Override
+    public JsonArrayBuilder addAll(JsonArrayBuilder builder) {
+        if (builder == null) {
+            throw new NullPointerException(JsonMessages.ARRBUILDER_ARRAY_BUILDER_NULL());
+        }
+        if (valueList == null) {
+            valueList = new ArrayList<>();
+        }
+        valueList.addAll(builder.build());
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder add(int index, JsonValue value) {
+        validateValue(value);
+        addValueList(index, value);
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder add(int index, String value) {
+        validateValue(value);
+        addValueList(index, new JsonStringImpl(value));
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder add(int index, BigDecimal value) {
+        validateValue(value);
+        addValueList(index, JsonNumberImpl.getJsonNumber(value));
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder add(int index, BigInteger value) {
+        validateValue(value);
+        addValueList(index, JsonNumberImpl.getJsonNumber(value));
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder add(int index, int value) {
+        addValueList(index, JsonNumberImpl.getJsonNumber(value));
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder add(int index, long value) {
+        addValueList(index, JsonNumberImpl.getJsonNumber(value));
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder add(int index, double value) {
+        addValueList(index, JsonNumberImpl.getJsonNumber(value));
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder add(int index, boolean value) {
+        addValueList(index, value ? JsonValue.TRUE : JsonValue.FALSE);
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder addNull(int index) {
+        addValueList(index, JsonValue.NULL);
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder add(int index, JsonObjectBuilder builder) {
+        if (builder == null) {
+            throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
+        }
+        addValueList(index, builder.build());
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder add(int index, JsonArrayBuilder builder) {
+        if (builder == null) {
+            throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
+        }
+        addValueList(index, builder.build());
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder set(int index, JsonValue value) {
+        validateValue(value);
+        setValueList(index, value);
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder set(int index, String value) {
+        validateValue(value);
+        setValueList(index, new JsonStringImpl(value));
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder set(int index, BigDecimal value) {
+        validateValue(value);
+        setValueList(index, JsonNumberImpl.getJsonNumber(value));
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder set(int index, BigInteger value) {
+        validateValue(value);
+        setValueList(index, JsonNumberImpl.getJsonNumber(value));
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder set(int index, int value) {
+        setValueList(index, JsonNumberImpl.getJsonNumber(value));
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder set(int index, long value) {
+        setValueList(index, JsonNumberImpl.getJsonNumber(value));
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder set(int index, double value) {
+        setValueList(index, JsonNumberImpl.getJsonNumber(value));
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder set(int index, boolean value) {
+        setValueList(index, value ? JsonValue.TRUE : JsonValue.FALSE);
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder setNull(int index) {
+        setValueList(index, JsonValue.NULL);
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder set(int index, JsonObjectBuilder builder) {
+        if (builder == null) {
+            throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
+        }
+        setValueList(index, builder.build());
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder set(int index, JsonArrayBuilder builder) {
+        if (builder == null) {
+            throw new NullPointerException(JsonMessages.ARRBUILDER_OBJECT_BUILDER_NULL());
+        }
+        setValueList(index, builder.build());
+        return this;
+    }
+
+    @Override
+    public JsonArrayBuilder remove(int index) {
+        if (valueList == null) {
+            throw new IndexOutOfBoundsException(JsonMessages.ARRBUILDER_VALUELIST_NULL(index, 0));
+        }
+        valueList.remove(index);
+        return this;
+    }
+
+    @Override
     public JsonArray build() {
         List<JsonValue> snapshot;
@@ -143,9 +344,34 @@
     }
 
+    private void populate(Collection<?> collection) {
+        for (Object value : collection) {
+            if (value != null && value instanceof Optional) {
+                ((Optional<?>) value).ifPresent(v ->
+                        this.valueList.add(MapUtil.handle(v, bufferPool)));
+            } else {
+                this.valueList.add(MapUtil.handle(value, bufferPool));
+            }
+        }
+    }
+
     private void addValueList(JsonValue value) {
         if (valueList == null) {
-            valueList = new ArrayList<JsonValue>();
+            valueList = new ArrayList<>();
         }
         valueList.add(value);
+    }
+
+    private void addValueList(int index, JsonValue value) {
+        if (valueList == null) {
+            valueList = new ArrayList<>();
+        }
+        valueList.add(index, value);
+    }
+
+    private void setValueList(int index, JsonValue value) {
+        if (valueList == null) {
+            throw new IndexOutOfBoundsException(JsonMessages.ARRBUILDER_VALUELIST_NULL(index, 0));
+        }
+        valueList.set(index, value);
     }
 
@@ -263,13 +489,15 @@
         public String toString() {
             StringWriter sw = new StringWriter();
-            JsonWriter jw = new JsonWriterImpl(sw, bufferPool);
-            jw.write(this);
-            jw.close();
+            try (JsonWriter jw = new JsonWriterImpl(sw, bufferPool)) {
+                jw.write(this);
+            }
             return sw.toString();
         }
-    }
-
+
+        @Override
+        public JsonArray asJsonArray() {
+            return this;
+        }
+    }
 }
 
-
-
