From 4499cf08620c412556a9721f98451d5e882d2c80 Mon Sep 17 00:00:00 2001
From: Brent Worden
Date: Wed, 25 May 2011 18:04:29 +0000
Subject: [PATCH] COLLECTIONS-213. Added multiple use support to
IteratorIterable.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1127604 13f79535-47bb-0310-9956-ffa450edef68
---
.../commons/collections/IteratorUtils.java | 20 ++++-
.../iterators/IteratorIterable.java | 74 +++++++++++++++++--
.../collections/TestIteratorUtils.java | 37 ++++++++++
.../iterators/TestIteratorIterable.java | 30 +++++++-
4 files changed, 150 insertions(+), 11 deletions(-)
diff --git a/src/java/org/apache/commons/collections/IteratorUtils.java b/src/java/org/apache/commons/collections/IteratorUtils.java
index c1b5f1cd8..81da617a5 100644
--- a/src/java/org/apache/commons/collections/IteratorUtils.java
+++ b/src/java/org/apache/commons/collections/IteratorUtils.java
@@ -759,7 +759,8 @@ public class IteratorUtils {
}
/**
- * Gets an iterable that wraps an iterator.
+ * Gets an iterable that wraps an iterator. The returned iterable can be
+ * used for a single iteration.
*
* @param iterator the iterator to use, not null
* @return a new, single use iterable
@@ -769,7 +770,22 @@ public class IteratorUtils {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
- return new IteratorIterable(iterator);
+ return new IteratorIterable(iterator, false);
+ }
+
+ /**
+ * Gets an iterable that wraps an iterator. The returned iterable can be
+ * used for multiple iterations.
+ *
+ * @param iterator the iterator to use, not null
+ * @return a new, multiple use iterable
+ * @throws NullPointerException if iterator is null
+ */
+ public static Iterable asMultipleUseIterable(Iterator extends E> iterator) {
+ if (iterator == null) {
+ throw new NullPointerException("Iterator must not be null");
+ }
+ return new IteratorIterable(iterator, true);
}
/**
diff --git a/src/java/org/apache/commons/collections/iterators/IteratorIterable.java b/src/java/org/apache/commons/collections/iterators/IteratorIterable.java
index 23e78b35c..1c28e5fc6 100644
--- a/src/java/org/apache/commons/collections/iterators/IteratorIterable.java
+++ b/src/java/org/apache/commons/collections/iterators/IteratorIterable.java
@@ -15,11 +15,47 @@ package org.apache.commons.collections.iterators;
import java.util.Iterator;
+import org.apache.commons.collections.ResettableIterator;
+
/**
* Adapter to make an {@link Iterator Iterator} instance appear to be an
- * {@link Iterable Iterable} instance. Unlike normal iterable instance, the
- * {@link #iterator()} method always returns the same iterator instance. This
- * prohibits this iterator to be only usable for one iterative operation.
+ * {@link Iterable Iterable} instance. The iterable can be constructed in one
+ * of two variants: single use, multiple use.
+ *
+ *
+ * In the single use iterable case, the iterable is only usable for one
+ * iterative operation over the source iterator. Subsequent iterative
+ * operations use the same, exhausted source iterator. To create a single use
+ * iterable, construct a new {@link IteratorIterable} using a {@link Iterator}
+ * that is NOT a {@link ResettableIterator} iterator:
+ *
+ * Iterator iterator = // some non-resettable iterator
+ * Iterable iterable = new IteratorIterable(iterator);
+ *
+ *
+ *
+ *
+ * In the multiple use iterable case, the iterable is usable for any number of
+ * iterative operations over the source iterator. Of special note, even though
+ * the iterable supports multiple iterations, it does not support concurrent
+ * iterations. To implicitly create a multiple use iterable, construct a new
+ * {@link IteratorIterable} using a {@link ResettableIterator} iterator:
+ *