summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2018-10-29 20:09:28 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2018-10-29 20:09:28 -0400
commite6ff84b22248c3c66b7d3fd619132e48a89db5ef (patch)
tree4961a4fa853998b67dc2d815bab81731be54c019 /base
parent074393ed8abda6003f31b97496fa8195c4627753 (diff)
QueuedIterators
QueuedIterators are iterators that can have other iterators interleaved into their iteration sequences. This was implemented as a decent idea, and because it may help get the iterative topDownTransform working again (if it ever worked in the first place...)
Diffstat (limited to 'base')
-rw-r--r--base/src/main/java/bjc/utils/data/QueuedIterator.java171
-rw-r--r--base/src/test/java/bjc/utils/data/QueuedIteratorTest.java79
2 files changed, 250 insertions, 0 deletions
diff --git a/base/src/main/java/bjc/utils/data/QueuedIterator.java b/base/src/main/java/bjc/utils/data/QueuedIterator.java
new file mode 100644
index 0000000..d75a9a3
--- /dev/null
+++ b/base/src/main/java/bjc/utils/data/QueuedIterator.java
@@ -0,0 +1,171 @@
+package bjc.utils.data;
+
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.Iterator;
+
+/**
+ * An iterator that supports queuing elements after/before the current iterator;
+ *
+ * @author bjculkin
+ *
+ * @param <E>
+ */
+public class QueuedIterator<E> implements Iterator<E> {
+ private Iterator<E> cur;
+
+ private Deque<Iterator<E>> pending;
+
+ /**
+ * Static method for constructing iterators.
+ *
+ * @return A queued iterator.
+ */
+ public static <E> QueuedIterator<E> queued() {
+ return new QueuedIterator<>();
+ }
+
+ /**
+ * Static method for constructing iterators.
+ *
+ * @param itrs
+ * The iterators to use.
+ * @return A queued iterator over the provided iterators.
+ */
+ @SafeVarargs
+ public static <E> QueuedIterator<E> queued(Iterator<E>... itrs) {
+ return new QueuedIterator<>(itrs);
+ }
+
+ /**
+ * Static method for constructing iterators.
+ *
+ * @param itrs
+ * The iterables to use.
+ * @return A queued iterator over the provided iterables.
+ */
+ @SafeVarargs
+ public static <E> QueuedIterator<E> queued(Iterable<E>... itrs) {
+ return new QueuedIterator<>(itrs);
+ }
+
+ /**
+ * Create a new queued iterator that starts blank.
+ */
+ public QueuedIterator() {
+ pending = new ArrayDeque<>();
+ }
+
+ /**
+ * Create a new queued iterator with a set of initial sources.
+ *
+ * @param inits
+ * The set of initial iterators to use.
+ */
+ @SafeVarargs
+ public QueuedIterator(Iterator<E>... inits) {
+ this();
+
+ for (Iterator<E> init : inits) {
+ pending.add(init);
+ }
+ }
+
+ /**
+ * Create a new queued iterator with a set of initial sources.
+ *
+ * @param inits
+ * The set of initial iterables to use.
+ */
+ @SafeVarargs
+ public QueuedIterator(Iterable<E>... inits) {
+ this();
+
+ for (Iterable<E> init : inits) {
+ pending.add(init.iterator());
+ }
+ }
+
+ /**
+ * Add a new iterator who we will iterate through first.
+ *
+ * @param itr
+ * The iterator to go through first.
+ */
+ public void before(Iterator<E> itr) {
+ pending.push(cur);
+
+ cur = itr;
+ }
+
+ /**
+ * Add a new iterable who we will iterate through first.
+ *
+ * @param itr
+ * The iterable to go through first.
+ */
+ public void before(Iterable<E> itr) {
+ before(itr.iterator());
+ }
+
+ /**
+ * Add a new iterator who we will iterate through next.
+ *
+ * @param itr
+ * The iterator to go through next.
+ */
+ public void after(Iterator<E> itr) {
+ pending.push(itr);
+ }
+ /**
+ * Add a new iterable who we will iterate through next.
+ *
+ * @param itr
+ * The iterable to go through next.
+ */
+ public void after(Iterable<E> itr) {
+ after(itr.iterator());
+ }
+
+ /**
+ * Add a new iterator who we will iterate through last.
+ *
+ * @param itr
+ * The iterator to go through last.
+ */
+ public void last(Iterator<E> itr) {
+ pending.add(itr);
+ }
+
+ /**
+ * Add a new iterable who we will iterate through last.
+ *
+ * @param itr
+ * The iterable to go through last.
+ */
+ public void last(Iterable<E> itr) {
+ last(itr.iterator());
+ }
+ @Override
+ public boolean hasNext() {
+ while (cur == null || !cur.hasNext()) {
+ if (pending.isEmpty()) return false;
+
+ cur = pending.pop();
+ }
+
+ return cur.hasNext();
+ }
+
+ @Override
+ public E next() {
+ while (cur == null || !cur.hasNext()) {
+ if (pending.isEmpty()) return null;
+
+ cur = pending.pop();
+ }
+
+ return cur.next();
+ }
+
+}
diff --git a/base/src/test/java/bjc/utils/data/QueuedIteratorTest.java b/base/src/test/java/bjc/utils/data/QueuedIteratorTest.java
new file mode 100644
index 0000000..573d218
--- /dev/null
+++ b/base/src/test/java/bjc/utils/data/QueuedIteratorTest.java
@@ -0,0 +1,79 @@
+package bjc.utils.data;
+
+import static org.junit.Assert.*;
+
+import java.util.Iterator;
+
+import static java.util.Arrays.asList;
+
+import org.junit.Test;
+
+import bjc.utils.funcutils.TestUtils;
+
+import static bjc.utils.data.QueuedIterator.*;
+import static bjc.utils.funcutils.TestUtils.*;
+import static bjc.utils.funcutils.IteratorUtils.*;
+
+/**
+ * Test of QueuedIterator.
+ *
+ * @author bjculkin
+ *
+ */
+public class QueuedIteratorTest {
+
+ /**
+ * Test of functionality.
+ */
+ @Test
+ public void test() {
+ assertIteratorEquals(false, queued());
+
+ assertIteratorEquals(false, queued(asList(1, 2, 3)), 1, 2, 3);
+ assertIteratorEquals(false, queued(asList(1, 2, 3), asList(3, 2, 1)), 1, 2, 3, 3, 2, 1);
+
+ }
+
+ /**
+ * Test of before() method.
+ */
+ @Test
+ public void testBefore() {
+ QueuedIterator<Integer> itr = queued(asList(1, 2, 3));
+
+ assertIteratorEquals(true, itr, 1, 2);
+
+ itr.before(asList(1, 2, 3));
+
+ assertIteratorEquals(false, itr, 1, 2, 3, 3);
+ }
+
+ /**
+ * Test of after() method.
+ */
+ @Test
+ public void testAfter() {
+ QueuedIterator<Integer> itr = queued(asList(1, 2, 3));
+
+ assertIteratorEquals(true, itr, 1, 2);
+
+ itr.after(asList(1, 2, 3));
+
+ assertIteratorEquals(false, itr, 3, 1, 2, 3);
+ }
+
+ /**
+ * Test of last() method.
+ */
+ @Test
+ public void testLast() {
+ QueuedIterator<Integer> itr = queued(asList(1, 2, 3));
+
+ assertIteratorEquals(true, itr, 1, 2);
+
+ itr.after(asList(4));
+ itr.last(asList(1, 2, 3));
+
+ assertIteratorEquals(false, itr, 3, 4, 1, 2, 3);
+ }
+}