You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wayang.apache.org by be...@apache.org on 2021/04/06 16:18:10 UTC

[incubator-wayang] 07/07: [WAYANG-28] add OneElementIterator.java

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

bertty pushed a commit to branch debugger
in repository https://gitbox.apache.org/repos/asf/incubator-wayang.git

commit a3342a09368aea8d2878f11c88e78db6a38115d7
Author: Bertty Contreras-Rojas <be...@scalytics.io>
AuthorDate: Tue Apr 6 12:07:23 2021 -0400

    [WAYANG-28] add OneElementIterator.java
---
 .../hackit/core/iterator/OneElementIterator.java   | 58 ++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/wayang-plugins/wayang-hackit/wayang-hackit-core/src/main/java/org/apache/wayang/plugin/hackit/core/iterator/OneElementIterator.java b/wayang-plugins/wayang-hackit/wayang-hackit-core/src/main/java/org/apache/wayang/plugin/hackit/core/iterator/OneElementIterator.java
new file mode 100644
index 0000000..5f5c0dd
--- /dev/null
+++ b/wayang-plugins/wayang-hackit/wayang-hackit-core/src/main/java/org/apache/wayang/plugin/hackit/core/iterator/OneElementIterator.java
@@ -0,0 +1,58 @@
+package org.apache.wayang.plugin.hackit.core.iterator;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+public class OneElementIterator<T> implements Iterator<T>, Iterable<T>{
+
+    private final boolean removeAllowed;
+    private boolean beforeFirst;
+    private boolean removed;
+    private T object;
+
+    public OneElementIterator(T object) {
+        this(object, true);
+    }
+
+    public OneElementIterator(T object, boolean removeAllowed) {
+        this.beforeFirst = true;
+        this.removed = false;
+        this.object = object;
+        this.removeAllowed = removeAllowed;
+    }
+
+    public boolean hasNext() {
+        return this.beforeFirst && !this.removed;
+    }
+
+    public T next() {
+        if (this.beforeFirst && !this.removed) {
+            this.beforeFirst = false;
+            return this.object;
+        } else {
+            throw new NoSuchElementException();
+        }
+    }
+
+    public void remove() {
+        if (this.removeAllowed) {
+            if (!this.removed && !this.beforeFirst) {
+                this.object = null;
+                this.removed = true;
+            } else {
+                throw new IllegalStateException();
+            }
+        } else {
+            throw new UnsupportedOperationException();
+        }
+    }
+
+    public void reset() {
+        this.beforeFirst = true;
+    }
+
+    @Override
+    public Iterator<T> iterator() {
+        return this;
+    }
+}