This is an automated email from the ASF dual-hosted git repository.

benw pushed a commit to branch TAP5-2764
in repository https://gitbox.apache.org/repos/asf/tapestry-5.git

commit 0da3aaa27e404c7d3dc85898cfaa79180241b9aa
Author: Ben Weidig <b...@netzgut.net>
AuthorDate: Sun Oct 8 15:13:45 2023 +0200

    TAP5-2764: static JSONArray#empty() added
---
 .../java/org/apache/tapestry5/json/JSONArray.java  | 23 +++++++++++++++++++++-
 .../test/groovy/json/specs/JSONArraySpec.groovy    | 20 +++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git 
a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java 
b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java
index ee9e755b9..02cacfb17 100644
--- a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java
+++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java
@@ -21,6 +21,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Objects;
 
 import org.apache.tapestry5.json.exceptions.JSONArrayIndexOutOfBoundsException;
 import org.apache.tapestry5.json.exceptions.JSONSyntaxException;
@@ -53,11 +54,31 @@ public final class JSONArray extends JSONCollection 
implements Collection<Object
 
     private final List<Object> values;
 
+    private static final JSONArray EMPTY = new 
JSONArray(Collections.emptyList());
+
+    /**
+     * Returns an immutable {@code JSONArray}.
+     */
+    public static final JSONArray empty() {
+        return EMPTY;
+    }
+
     /**
      * Creates a {@code JSONArray} with no values.
      */
     public JSONArray() {
-        values = new ArrayList<Object>();
+        this(new ArrayList<>());
+    }
+
+    /**
+     * Creates a {@code JSONArray} with a specific underlying data structure.
+     *
+     * @param list the desired underlying {@code List} to be used.
+     * @throws NullPointerException if the list is null.
+     */
+    private JSONArray(List<Object> list) {
+        Objects.requireNonNull(list);
+        values = list;
     }
 
     /**
diff --git a/tapestry-json/src/test/groovy/json/specs/JSONArraySpec.groovy 
b/tapestry-json/src/test/groovy/json/specs/JSONArraySpec.groovy
index 35b76584a..df8e2db7a 100644
--- a/tapestry-json/src/test/groovy/json/specs/JSONArraySpec.groovy
+++ b/tapestry-json/src/test/groovy/json/specs/JSONArraySpec.groovy
@@ -617,4 +617,24 @@ class JSONArraySpec extends Specification {
             Double.NaN
         ]
     }
+
+    def "empty() returns an empty JSONArray"() {
+
+        when:
+        def array = JSONArray.empty()
+
+        then:
+
+        array.isEmpty()
+    }
+
+    def "empty() retuns an immutable JSONArray"() {
+        def array = JSONArray.empty()
+
+        when:
+        array.add("")
+
+        then:
+        UnsupportedOperationException e = thrown()
+    }
 }

Reply via email to