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

struberg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git


The following commit(s) were added to refs/heads/master by this push:
     new 3bc1fe5  JOHNZON-226 iterator for JsonArrayImpl
3bc1fe5 is described below

commit 3bc1fe5fe0da2a4fac16ddf998e38b11104a57ef
Author: Mark Struberg <[email protected]>
AuthorDate: Wed Jul 31 23:29:08 2019 +0200

    JOHNZON-226 iterator for JsonArrayImpl
---
 .../org/apache/johnzon/core/JsonArrayImpl.java     | 32 ++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git 
a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayImpl.java 
b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayImpl.java
index 96be451..e247d33 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayImpl.java
@@ -28,7 +28,9 @@ import java.io.ObjectStreamException;
 import java.io.Serializable;
 import java.io.StringWriter;
 import java.util.AbstractList;
+import java.util.Iterator;
 import java.util.List;
+import java.util.NoSuchElementException;
 
 class JsonArrayImpl extends AbstractList<JsonValue> implements JsonArray, 
Serializable {
     private final BufferStrategy.BufferProvider<char[]> provider;
@@ -198,4 +200,34 @@ class JsonArrayImpl extends AbstractList<JsonValue> 
implements JsonArray, Serial
     private Object writeReplace() throws ObjectStreamException {
         return new SerializableValue(toString());
     }
+
+    @Override
+    public Iterator<JsonValue> iterator() {
+        return new JsonArrayIterator();
+    }
+
+    /**
+     * We don't need any range check, so we can simplify the Iterator logic.
+     * get() on an ArrayList works even faster than ArrayList#iterator!
+     */
+    private class JsonArrayIterator implements Iterator<JsonValue> {
+        int cursor = 0;
+
+        @Override
+        public boolean hasNext() {
+            return cursor != unmodifieableBackingList.size();
+        }
+
+        @Override
+        public JsonValue next() {
+            try {
+                int i = cursor;
+                JsonValue next = unmodifieableBackingList.get(i);
+                cursor = i + 1;
+                return next;
+            } catch (IndexOutOfBoundsException e) {
+                throw new NoSuchElementException();
+            }
+        }
+    }
 }

Reply via email to