public class IteratorChain<E>
extends java.lang.Object
implements java.util.Iterator<E>
This class makes multiple iterators look like one to the caller. When any method from the Iterator interface is called, the IteratorChain will delegate to a single underlying Iterator. The IteratorChain will invoke the Iterators in sequence until all Iterators are exhausted.
Under many circumstances, linking Iterators together in this manner is more efficient (and convenient) than reading out the contents of each Iterator into a List and creating a new Iterator.
Calling a method that adds new Iterator after a method in the Iterator interface has been called will result in an UnsupportedOperationException.
NOTE: As from version 3.0, the IteratorChain may contain no iterators. In this case the class will function as an empty iterator.
NOTE: As from version 4.0, the IteratorChain stores the iterators in a queue
and removes any reference to them as soon as they are not used anymore. Thus
the methods setIterator(Iterator)
and getIterators()
have been
removed and size()
will return the number of remaining iterators in
the queue.
Constructor and Description |
---|
IteratorChain()
Construct an IteratorChain with no Iterators.
|
IteratorChain(java.util.Collection<java.util.Iterator<? extends E>> iteratorChain)
Constructs a new
IteratorChain over the collection of
iterators. |
IteratorChain(java.util.Iterator<? extends E>... iteratorChain)
Constructs a new
IteratorChain over the array of iterators. |
IteratorChain(java.util.Iterator<? extends E> iterator)
Construct an IteratorChain with a single Iterator.
|
IteratorChain(java.util.Iterator<? extends E> first,
java.util.Iterator<? extends E> second)
Constructs a new
IteratorChain over the two given iterators. |
Modifier and Type | Method and Description |
---|---|
void |
addIterator(java.util.Iterator<? extends E> iterator)
Add an Iterator to the end of the chain
|
boolean |
hasNext()
Return true if any Iterator in the IteratorChain has a remaining element.
|
boolean |
isLocked()
Determine if modifications can still be made to the IteratorChain.
|
E |
next()
Returns the next Object of the current Iterator
|
void |
remove()
Removes from the underlying collection the last element returned by the
Iterator.
|
int |
size()
Returns the remaining number of Iterators in the current IteratorChain.
|
public IteratorChain()
You will normally use addIterator(Iterator)
to add some
iterators after using this constructor.
public IteratorChain(java.util.Iterator<? extends E> iterator)
This method takes one iterator. The newly constructed iterator will iterate through that iterator. Thus calling this constructor on its own will have no effect other than decorating the input iterator.
You will normally use addIterator(Iterator)
to add some more
iterators after using this constructor.
iterator
- the first child iterator in the IteratorChain, not nulljava.lang.NullPointerException
- if the iterator is nullpublic IteratorChain(java.util.Iterator<? extends E> first, java.util.Iterator<? extends E> second)
IteratorChain
over the two given iterators.
This method takes two iterators. The newly constructed iterator will iterate through each one of the input iterators in turn.
first
- the first child iterator in the IteratorChain, not nullsecond
- the second child iterator in the IteratorChain, not nulljava.lang.NullPointerException
- if either iterator is nullpublic IteratorChain(java.util.Iterator<? extends E>... iteratorChain)
IteratorChain
over the array of iterators.
This method takes an array of iterators. The newly constructed iterator will iterate through each one of the input iterators in turn.
iteratorChain
- the array of iterators, not nulljava.lang.NullPointerException
- if iterators array is or contains nullpublic IteratorChain(java.util.Collection<java.util.Iterator<? extends E>> iteratorChain)
IteratorChain
over the collection of
iterators.
This method takes a collection of iterators. The newly constructed iterator will iterate through each one of the input iterators in turn.
iteratorChain
- the collection of iterators, not nulljava.lang.NullPointerException
- if iterators collection is or contains nulljava.lang.ClassCastException
- if iterators collection doesn't contain an
iteratorpublic void addIterator(java.util.Iterator<? extends E> iterator)
iterator
- Iterator to addjava.lang.IllegalStateException
- if I've already started iteratingjava.lang.NullPointerException
- if the iterator is nullpublic int size()
public boolean isLocked()
public boolean hasNext()
hasNext
in interface java.util.Iterator<E>
public E next()
next
in interface java.util.Iterator<E>
java.util.NoSuchElementException
- if all the Iterators are
exhaustedpublic void remove()
remove
in interface java.util.Iterator<E>
java.lang.UnsupportedOperationException
- if the remove operator is not
supported by the underlying Iteratorjava.lang.IllegalStateException
- if the next method has not yet been called,
or the remove method has already been called after the last call to the
next method.Copyright © 2010 - 2020 Adobe. All Rights Reserved