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.
| Modifier and Type | Field and Description |
|---|---|
| private Iterator | currentIterator
The current iterator |
| private boolean | isLocked
ComparatorChain is "locked" after the first time compare(Object,Object) is called |
| private final Queue | iteratorChain
The chain of iterators |
| private Iterator | lastUsedIterator
The "last used" Iterator is the Iterator upon which next() or hasNext() was most recently called used for the remove() operation only |
| Access | Constructor and Description |
|---|---|
| public | |
| public | IteratorChain(final Iterator<? extends E>
the first child iterator in the IteratorChain, not null iterator)Construct an IteratorChain with a single Iterator. |
| public | IteratorChain(final Iterator<? extends E>
the first child iterator in the IteratorChain, not null first, final Iterator<? extends E> the second child iterator in the IteratorChain, not null second)Constructs a new |
| public | IteratorChain(final Iterator<? extends E>...
the array of iterators, not null iteratorChain)Constructs a new |
| public | IteratorChain(final Collection<Iterator<? extends E>>
the collection of iterators, not null iteratorChain)Constructs a new |
| Modifier and Type | Method and Description |
|---|---|
| public void | addIterator(final Iterator<? extends E>
Iterator to add iterator)Add an Iterator to the end of the chain |
| private void | |
| public boolean | Returns: true if elements remainImplements java. |
| public boolean | Returns: true if IteratorChain cannot be modified, false if it canDetermine if modifications can still be made to the IteratorChain. |
| private void | |
| public E | Returns: Object from the current IteratorImplements java. |
| public void | remove()
Overrides default java. |
| public int | Returns: Iterator countReturns the remaining number of Iterators in the current IteratorChain. |
| protected void | updateCurrentIterator()
Updates the current iterator field to ensure that the current Iterator is not exhausted |
| currentIterator | back to summary |
|---|---|
| private Iterator<? extends E> currentIterator The current iterator | |
| isLocked | back to summary |
|---|---|
| private boolean isLocked ComparatorChain is "locked" after the first time compare(Object,Object) is called | |
| iteratorChain | back to summary |
|---|---|
| private final Queue<Iterator<? extends E>> iteratorChain The chain of iterators | |
| lastUsedIterator | back to summary |
|---|---|
| private Iterator<? extends E> lastUsedIterator The "last used" Iterator is the Iterator upon which next() or hasNext() was most recently called used for the remove() operation only | |
| IteratorChain | back to summary |
|---|---|
| public IteratorChain() Construct an IteratorChain with no Iterators.
You will normally use | |
| IteratorChain | back to summary |
|---|---|
| public IteratorChain(final Iterator<? extends E> iterator) Construct an IteratorChain with a single 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
| |
| IteratorChain | back to summary |
|---|---|
| public IteratorChain(final Iterator<? extends E> first, final Iterator<? extends E> second) Constructs a new This method takes two iterators. The newly constructed iterator will iterate through each one of the input iterators in turn.
| |
| IteratorChain | back to summary |
|---|---|
| public IteratorChain(final Iterator<? extends E>... iteratorChain) Constructs a new This method takes an array of iterators. The newly constructed iterator will iterate through each one of the input iterators in turn.
| |
| IteratorChain | back to summary |
|---|---|
| public IteratorChain(final Collection<Iterator<? extends E>> iteratorChain) Constructs a new This method takes a collection of iterators. The newly constructed iterator will iterate through each one of the input iterators in turn.
| |
| addIterator | back to summary |
|---|---|
| public void addIterator(final Iterator<? extends E> iterator) Add an Iterator to the end of the chain
| |
| checkLocked | back to summary |
|---|---|
| private void checkLocked() Checks whether the iterator chain is now locked and in use. | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements java. Return true if any Iterator in the IteratorChain has a remaining element.
| |
| isLocked | back to summary |
|---|---|
| public boolean isLocked() Determine if modifications can still be made to the IteratorChain. IteratorChains cannot be modified once they have executed a method from the Iterator interface.
| |
| lockChain | back to summary |
|---|---|
| private void lockChain() Lock the chain so no more iterators can be added. This must be called from all Iterator interface methods. | |
| next | back to summary |
|---|---|
| public E next() Implements java. Returns the next Object of the current Iterator
| |
| remove | back to summary |
|---|---|
| public void remove() Overrides default java. Removes from the underlying collection the last element returned by the Iterator. As with next() and hasNext(), this method calls remove() on the underlying Iterator. Therefore, this method may throw an UnsupportedOperationException if the underlying Iterator does not support this method.
| |
| size | back to summary |
|---|---|
| public int size() Returns the remaining number of Iterators in the current IteratorChain.
| |
| updateCurrentIterator | back to summary |
|---|---|
| protected void updateCurrentIterator() Updates the current iterator field to ensure that the current Iterator is not exhausted | |