@GwtCompatible public abstract class AbstractIterator<T> extends UnmodifiableIterator<T>
Iterator interface, to make this
interface easier to implement for certain types of data sources.
Iterator requires its implementations to support querying the end-of-data status
without changing the iterator's state, using the hasNext() method. But many data sources,
such as Reader.read(), do not expose this information; the only way to discover
whether there is any data left is by trying to retrieve it. These types of data sources are
ordinarily difficult to write iterators for. But using this class, one must implement only the
computeNext() method, and invoke the endOfData() method when appropriate.
Another example is an iterator that skips over null elements in a backing iterator. This could be implemented as:
public static Iterator<String> skipNulls(final Iterator<String> in) {
return new AbstractIterator<String>() {
protected String computeNext() {
while (in.hasNext()) {
String s = in.next();
if (s != null) {
return s;
}
}
return endOfData();
}
};
}
This class supports iterators that include null elements.
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractIterator()
Constructor for use by subclasses.
|
| Modifier and Type | Method and Description |
|---|---|
protected abstract T |
computeNext()
Returns the next element.
|
protected T |
endOfData()
Implementations of
computeNext() must invoke this method when there are no
elements left in the iteration. |
boolean |
hasNext()
Returns
true if the iteration has more elements. |
T |
next()
Returns the next element in the iteration.
|
T |
peek()
Returns the next element in the iteration without advancing the iteration, according to the
contract of
PeekingIterator.peek(). |
removeclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitforEachRemainingprotected AbstractIterator()
protected abstract T computeNext()
endOfData() when
there are no elements left in the iteration. Failure to do so could result in an infinite loop.
The initial invocation of hasNext() or next() calls this method, as does
the first invocation of hasNext or next following each successful call to
next. Once the implementation either invokes endOfData or throws an exception,
computeNext is guaranteed to never be called again.
If this method throws an exception, it will propagate outward to the hasNext or
next invocation that invoked this method. Any further attempts to use the iterator will
result in an IllegalStateException.
The implementation of this method may not invoke the hasNext, next, or
peek() methods on this instance; if it does, an IllegalStateException will
result.
endOfData was called during execution,
the return value will be ignored.RuntimeException - if any unrecoverable error happens. This exception will propagate
outward to the hasNext(), next(), or peek() invocation that invoked
this method. Any further attempts to use the iterator will result in an IllegalStateException.@CanIgnoreReturnValue protected final T endOfData()
computeNext() must invoke this method when there are no
elements left in the iteration.null; a convenience so your computeNext implementation can use the
simple statement return endOfData();@CanIgnoreReturnValue public final boolean hasNext()
java.util.Iteratortrue if the iteration has more elements.
(In other words, returns true if Iterator.next() would
return an element rather than throwing an exception.)true if the iteration has more elements@CanIgnoreReturnValue public final T next()
java.util.Iteratorpublic final T peek()
PeekingIterator.peek().
Implementations of AbstractIterator that wish to expose this functionality should
implement PeekingIterator.
Copyright © 2010–2019. All rights reserved.