Class AbstractIterator<T>
- java.lang.Object
-
- cn.cloudself.query.util.iterator.AbstractIterator<T>
-
- All Implemented Interfaces:
Iterator<T>
public abstract class AbstractIterator<T> extends Object implements Iterator<T>
This class provides a skeletal implementation of theIteratorinterface, to make this interface easier to implement for certain types of data sources.Iteratorrequires its implementations to support querying the end-of-data status without changing the iterator's state, using thehasNext()method. But many data sources, such asReader.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 thecomputeNext()method, and invoke theendOfData()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.
- Since:
- 2.0
- Author:
- Kevin Bourrillion
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanhasNext()Tnext()Tpeek()Returns the next element in the iteration without advancing the iteration, according to the-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Iterator
forEachRemaining, remove
-
-
-
-
Method Detail
-
peek
public final T peek()
Returns the next element in the iteration without advancing the iteration, according to theImplementations of
AbstractIteratorthat wish to expose this functionality should implementPeekingIterator.
-
-