Top Description Fields Constructors Methods
org.apache.commons.collections4.iterators

public Class IteratorIterable<E>

extends Object
implements Iterable<E>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable
Imports
java.util.Iterator, org.apache.commons.collections4.ResettableIterator

Adapter to make an Iterator instance appear to be an 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 IteratorIterable using a Iterator that is NOT a ResettableIterator iterator:

  Iterator<Integer> iterator = // some non-resettable iterator
  Iterable<Integer> iterable = new IteratorIterable<Integer>(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 IteratorIterable using a ResettableIterator iterator:

  Integer[] array = {Integer.valueOf(1),Integer.valueOf(2),Integer.valueOf(3)};
  Iterator<Integer> iterator = IteratorUtils.arrayIterator(array); // a resettable iterator
  Iterable<Integer> iterable = new IteratorIterable<Integer>(iterator);

A multiple use iterable can also be explicitly constructed using any Iterator and specifying true for the multipleUse flag:

  Iterator<Integer> iterator = // some non-resettable iterator
  Iterable<Integer> iterable = new IteratorIterable<Integer>(iterator, true);
Since
4.0

Field Summary

Modifier and TypeField and Description
private final Iterator<? extends E>
iterator

the iterator being adapted into an iterable.

private final Iterator<E>
typeSafeIterator

the iterator parameterized as the iterator() return type.

Constructor Summary

AccessConstructor and Description
public
IteratorIterable(final Iterator<? extends E>
the iterator to use.
iterator
)

Constructs a new IteratorIterable that will use the given iterator.

public
IteratorIterable(final Iterator<? extends E>
the iterator to use.
iterator
,
final boolean
true if the new iterable can be used in multiple iterations
multipleUse
)

Constructs a new IteratorIterable that will use the given iterator.

Method Summary

Modifier and TypeMethod and Description
private static <E> Iterator<E>
createTypesafeIterator(final Iterator<? extends E> iterator)

Factory method to create an Iterator from another iterator over objects of a different subtype.

public Iterator<E>

Returns:

the iterator
iterator
()

Implements java.lang.Iterable.iterator.

Gets the iterator wrapped by this iterable.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

iteratorback to summary
private final Iterator<? extends E> iterator

the iterator being adapted into an iterable.

typeSafeIteratorback to summary
private final Iterator<E> typeSafeIterator

the iterator parameterized as the iterator() return type.

Constructor Detail

IteratorIterableback to summary
public IteratorIterable(final Iterator<? extends E> iterator)

Constructs a new IteratorIterable that will use the given iterator.

Parameters
iterator:Iterator<? extends E>

the iterator to use.

IteratorIterableback to summary
public IteratorIterable(final Iterator<? extends E> iterator, final boolean multipleUse)

Constructs a new IteratorIterable that will use the given iterator.

Parameters
iterator:Iterator<? extends E>

the iterator to use.

multipleUse:boolean

true if the new iterable can be used in multiple iterations

Method Detail

createTypesafeIteratorback to summary
private static <E> Iterator<E> createTypesafeIterator(final Iterator<? extends E> iterator)

Factory method to create an Iterator from another iterator over objects of a different subtype.

iteratorback to summary
public Iterator<E> iterator()

Implements java.lang.Iterable.iterator.

Gets the iterator wrapped by this iterable.

Returns:Iterator<E>

the iterator

Annotations
@Override