diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2019-04-11 18:59:02 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2019-04-11 18:59:02 -0300 |
| commit | 16e1e23250063d7885b790d082d2118f83693028 (patch) | |
| tree | da1a801d582c304f8e1fb6bec21c949452ebaadc /base | |
| parent | 4ba17336e900a5c1fcf9d9b0a9d87fa6f62d688f (diff) | |
QueuedIterator changes
Diffstat (limited to 'base')
3 files changed, 68 insertions, 10 deletions
diff --git a/base/src/main/java/bjc/utils/data/QueuedIterator.java b/base/src/main/java/bjc/utils/data/QueuedIterator.java index d75a9a3..0eeac4d 100644 --- a/base/src/main/java/bjc/utils/data/QueuedIterator.java +++ b/base/src/main/java/bjc/utils/data/QueuedIterator.java @@ -28,8 +28,21 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Static method for constructing iterators. * + * @param vals + * The values to iterate over. + * + * @return A queued iterator. + */ + public static <E> QueuedIterator<E> queued(E... vals) { + return new QueuedIterator<>(new ArrayIterator<>(vals)); + } + + /** + * Static method for constructing iterators. + * * @param itrs - * The iterators to use. + * The iterators to use. + * * @return A queued iterator over the provided iterators. */ @SafeVarargs @@ -41,7 +54,8 @@ public class QueuedIterator<E> implements Iterator<E> { * Static method for constructing iterators. * * @param itrs - * The iterables to use. + * The iterables to use. + * * @return A queued iterator over the provided iterables. */ @SafeVarargs @@ -87,6 +101,17 @@ public class QueuedIterator<E> implements Iterator<E> { } /** + * Create a new queued iterator with a set of initial values. + * + * @param vals + * The set of initial values to use. + */ + @SafeVarargs + public QueuedIterator(E... vals) { + this(new ArrayIterator(vals)); + } + + /** * Add a new iterator who we will iterate through first. * * @param itr @@ -109,6 +134,16 @@ public class QueuedIterator<E> implements Iterator<E> { } /** + * Add a new set of values who we will iterate through first. + * + * @param vals + * Values to iterate over first. + */ + public void before(E... vals) { + before(new ArrayIterator<>(vals)); + } + + /** * Add a new iterator who we will iterate through next. * * @param itr @@ -117,6 +152,7 @@ public class QueuedIterator<E> implements Iterator<E> { public void after(Iterator<E> itr) { pending.push(itr); } + /** * Add a new iterable who we will iterate through next. * @@ -126,6 +162,16 @@ public class QueuedIterator<E> implements Iterator<E> { public void after(Iterable<E> itr) { after(itr.iterator()); } + + /** + * Add a new set of values who we will iterate through next. + * + * @param vals + * The values to iterate over next. + */ + public void after(E... vals) { + after(new ArrayIterator<>(vals)); + } /** * Add a new iterator who we will iterate through last. @@ -146,6 +192,17 @@ public class QueuedIterator<E> implements Iterator<E> { public void last(Iterable<E> itr) { last(itr.iterator()); } + + /** + * Add a new set of values who we will iterate through last. + * + * @param itr + * The iterable to go through last. + */ + public void last(E... vals) { + last(new ArrayIterator<>(vals)); + } + @Override public boolean hasNext() { while (cur == null || !cur.hasNext()) { diff --git a/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java b/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java index 55e5985..c639da6 100644 --- a/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java @@ -1,6 +1,7 @@ package bjc.utils.funcutils; import java.util.Iterator; +import java.util.function.Function; /** * Utility methods for dealing with iterators. diff --git a/base/src/test/java/bjc/utils/data/QueuedIteratorTest.java b/base/src/test/java/bjc/utils/data/QueuedIteratorTest.java index ac8a174..bbbca56 100644 --- a/base/src/test/java/bjc/utils/data/QueuedIteratorTest.java +++ b/base/src/test/java/bjc/utils/data/QueuedIteratorTest.java @@ -21,7 +21,7 @@ public class QueuedIteratorTest { public void test() { assertIteratorEquals(false, queued()); - assertIteratorEquals(false, queued(asList(1, 2, 3)), 1, 2, 3); + assertIteratorEquals(false, queued(1, 2, 3), 1, 2, 3); assertIteratorEquals(false, queued(asList(1, 2, 3), asList(3, 2, 1)), 1, 2, 3, 3, 2, 1); } @@ -31,11 +31,11 @@ public class QueuedIteratorTest { */ @Test public void testBefore() { - QueuedIterator<Integer> itr = queued(asList(1, 2, 3)); + QueuedIterator<Integer> itr = queued(1, 2, 3); assertIteratorEquals(true, itr, 1, 2); - itr.before(asList(1, 2, 3)); + itr.before(1, 2, 3); assertIteratorEquals(false, itr, 1, 2, 3, 3); } @@ -45,11 +45,11 @@ public class QueuedIteratorTest { */ @Test public void testAfter() { - QueuedIterator<Integer> itr = queued(asList(1, 2, 3)); + QueuedIterator<Integer> itr = queued(1, 2, 3); assertIteratorEquals(true, itr, 1, 2); - itr.after(asList(1, 2, 3)); + itr.after(1, 2, 3); assertIteratorEquals(false, itr, 3, 1, 2, 3); } @@ -59,12 +59,12 @@ public class QueuedIteratorTest { */ @Test public void testLast() { - QueuedIterator<Integer> itr = queued(asList(1, 2, 3)); + QueuedIterator<Integer> itr = queued(1, 2, 3); assertIteratorEquals(true, itr, 1, 2); - itr.after(asList(4)); - itr.last(asList(1, 2, 3)); + itr.after(4); + itr.last(1, 2, 3); assertIteratorEquals(false, itr, 3, 4, 1, 2, 3); } |
