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 java., 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 and Type | Class and Description |
|---|---|
| private static enum |
| Modifier and Type | Field and Description |
|---|---|
| private T | |
| private AbstractIterator. |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| protected abstract T | Returns: the next element if there was one. IfendOfData was called during execution,
the return value will be ignored.Returns the next element Note the implementation must call |
| protected final T | Returns: null; a convenience so your computeNext implementation can use the
simple statement return endOfData();Implementations of |
| public final boolean | |
| public final T | |
| public final T | peek()
Returns the next element in the iteration without advancing the iteration, according to the
contract of |
| private boolean |
| next | back to summary |
|---|---|
| private T next
| |
| state | back to summary |
|---|---|
| private AbstractIterator. | |
| AbstractIterator | back to summary |
|---|---|
| protected AbstractIterator() Constructor for use by subclasses. | |
| computeNext | back to summary |
|---|---|
| protected abstract T computeNext() Returns the next element Note the implementation must call The initial invocation of If this method throws an exception, it will propagate outward to the The implementation of this method may not invoke the
| |
| endOfData | back to summary |
|---|---|
| protected final T endOfData() Implementations of
| |
| hasNext | back to summary |
|---|---|
| public final boolean hasNext() Implements java. Doc from java. Returns
| |
| next | back to summary |
|---|---|
| public final T next() Implements java. Doc from java. Returns the next element in the iteration.
| |
| peek | back to summary |
|---|---|
| public final T peek() Returns the next element in the iteration without advancing the iteration, according to the
contract of Implementations of
| |
| tryToComputeNext | back to summary |
|---|---|
| private boolean tryToComputeNext() | |
| Modifier and Type | Field and Description |
|---|---|
| public static final AbstractIterator. | DONE
We have reached the end of the data and are finished. |
| public static final AbstractIterator. | FAILED
We've suffered an exception and are kaput. |
| public static final AbstractIterator. | NOT_READY
We haven't yet computed or have already returned the element. |
| public static final AbstractIterator. | READY
We have computed the next element and haven't returned it yet. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static AbstractIterator. | |
| public static AbstractIterator. |
| DONE | back to summary |
|---|---|
| public static final AbstractIterator. We have reached the end of the data and are finished. | |
| FAILED | back to summary |
|---|---|
| public static final AbstractIterator. We've suffered an exception and are kaput. | |
| NOT_READY | back to summary |
|---|---|
| public static final AbstractIterator. We haven't yet computed or have already returned the element. | |
| READY | back to summary |
|---|---|
| public static final AbstractIterator. We have computed the next element and haven't returned it yet. | |
| State | back to summary |
|---|---|
| private State() | |
| valueOf | back to summary |
|---|---|
| public static AbstractIterator. | |
| values | back to summary |
|---|---|
| public static AbstractIterator. | |