org.apache.commons.collections.iterators
public class IteratorChain extends java.lang.Object implements java.util.Iterator
An IteratorChain is an Iterator that wraps one or more Iterators. When any method from the Iterator interface is called, the IteratorChain will proxy to a single underlying Iterator. The IteratorChain will invoke the Iterators in sequence until all Iterators are exhausted completely.
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 Iteratorafter a method in the Iterator interface has been called will result in an UnsupportedOperationException. Subclasses should take care to not alter the underlying List of Iterators.
Modifier and Type | Field and Description |
---|---|
protected java.util.Iterator |
currentIterator |
protected int |
currentIteratorIndex |
protected boolean |
isLocked |
protected java.util.List |
iteratorChain |
protected java.util.Iterator |
lastUsedIterator |
Constructor and Description |
---|
IteratorChain()
Construct an IteratorChain with no Iterators.
|
IteratorChain(java.util.Collection iterators)
Constructs a new
IteratorChain over the collection
of iterators. |
IteratorChain(java.util.Iterator iterator)
Construct an IteratorChain with a single Iterator.
|
IteratorChain(java.util.Iterator[] iterators)
Constructs a new
IteratorChain over the array
of iterators. |
IteratorChain(java.util.Iterator a,
java.util.Iterator b)
Constructs a new
IteratorChain over the two
given iterators. |
Modifier and Type | Method and Description |
---|---|
void |
addIterator(java.util.Iterator iterator)
Add an Iterator to the end of the chain
|
java.util.List |
getIterators()
Get the list of Iterators (unmodifiable)
|
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.
|
java.lang.Object |
next()
Returns the next Object of the current Iterator
|
void |
remove()
Removes from the underlying collection the last element
returned by the Iterator.
|
void |
setIterator(int index,
java.util.Iterator iterator)
Set the Iterator at the given index
|
int |
size()
Number of Iterators in the current IteratorChain.
|
protected void |
updateCurrentIterator() |
protected java.util.Iterator currentIterator
protected int currentIteratorIndex
protected boolean isLocked
protected final java.util.List iteratorChain
protected java.util.Iterator lastUsedIterator
public IteratorChain()
public IteratorChain(java.util.Collection iterators)
IteratorChain
over the collection
of iterators.iterators
- the collection of iteratorsjava.lang.NullPointerException
- if iterators collection is or contains nulljava.lang.ClassCastException
- if iterators collection doesn't contain an iteratorpublic IteratorChain(java.util.Iterator iterator)
iterator
- first Iterator in the IteratorChainjava.lang.NullPointerException
- if the iterator is nullpublic IteratorChain(java.util.Iterator[] iterators)
IteratorChain
over the array
of iterators.iterators
- the array of iteratorsjava.lang.NullPointerException
- if iterators array is or contains nullpublic IteratorChain(java.util.Iterator a, java.util.Iterator b)
IteratorChain
over the two
given iterators.a
- the first child iteratorb
- the second child iteratorjava.lang.NullPointerException
- if either iterator is nullpublic void addIterator(java.util.Iterator iterator)
iterator
- Iterator to addjava.lang.IllegalStateException
- if I've already started iteratingjava.lang.NullPointerException
- if the iterator is nullpublic java.util.List getIterators()
public boolean hasNext() throws java.lang.UnsupportedOperationException
hasNext
in interface java.util.Iterator
java.lang.UnsupportedOperationException
- if the IteratorChain does not contain at least one
Iteratorpublic boolean isLocked()
public java.lang.Object next() throws java.util.NoSuchElementException, java.lang.UnsupportedOperationException
next
in interface java.util.Iterator
java.util.NoSuchElementException
- if all the Iterators are exhaustedjava.lang.UnsupportedOperationException
- if the IteratorChain does not contain at least one
Iteratorpublic void remove() throws java.lang.UnsupportedOperationException, java.lang.IllegalStateException
remove
in interface java.util.Iterator
java.lang.UnsupportedOperationException
- if the remove operator is not supported by the underlying
Iterator or if there are no Iterators in the IteratorChainjava.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.public void setIterator(int index, java.util.Iterator iterator) throws java.lang.IndexOutOfBoundsException
index
- index of the Iterator to replaceiterator
- Iterator to place at the given indexjava.lang.IndexOutOfBoundsException
- if index < 0 or index > size()java.lang.IllegalStateException
- if I've already started iteratingjava.lang.NullPointerException
- if the iterator is nullpublic int size()
protected void updateCurrentIterator()
Copyright © 2001-2004 Apache Software Foundation. Documenation generated February 26 2016.