|
|
@@ -0,0 +1,41 @@
|
|
|
+/*
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ * @copyright Ranides Atterwim
|
|
|
+ * @license WTFPL
|
|
|
+ * @url http://ranides.net/projects/assira
|
|
|
+ */
|
|
|
+package net.ranides.assira.collection.iterators;
|
|
|
+
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.NoSuchElementException;
|
|
|
+import java.util.function.Consumer;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ */
|
|
|
+public abstract class ForwardIterator<T> implements Iterator<T> {
|
|
|
+
|
|
|
+ private boolean ready = false;
|
|
|
+ private T last = null;
|
|
|
+
|
|
|
+ protected abstract boolean next(Consumer<? super T> action);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean hasNext() {
|
|
|
+ return ready || (ready = next(v -> last = v));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public T next() {
|
|
|
+ if(ready) {
|
|
|
+ ready = false;
|
|
|
+ return last;
|
|
|
+ }
|
|
|
+ if(!next(v -> last = v)) {
|
|
|
+ throw new NoSuchElementException();
|
|
|
+ }
|
|
|
+ return last;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|