Package com.google.common.collect
Class AbstractSequentialIterator<T>
- java.lang.Object
-
- com.google.common.collect.UnmodifiableIterator<T>
-
- com.google.common.collect.AbstractSequentialIterator<T>
-
- All Implemented Interfaces:
Iterator<T>
@GwtCompatible public abstract class AbstractSequentialIterator<T> extends UnmodifiableIterator<T>
This class provides a skeletal implementation of theIteratorinterface for sequences whose next element can always be derived from the previous element. Null elements are not supported, nor is theUnmodifiableIterator.remove()method.Example:
Iterator<Integer> powersOfTwo = new AbstractSequentialIterator<Integer>(1) { protected Integer computeNext(Integer previous) { return (previous == 1 << 30) ? null : previous * 2; } };- Since:
- 12.0 (in Guava as
AbstractLinkedIteratorsince 8.0) - Author:
- Chris Povirk
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractSequentialIterator(T firstOrNull)Creates a new iterator with the given first element, or, iffirstOrNullis null, creates a new empty iterator.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract TcomputeNext(T previous)Returns the element that followsprevious, or returnsnullif no elements remain.booleanhasNext()Returnstrueif the iteration has more elements.Tnext()Returns the next element in the iteration.-
Methods inherited from class com.google.common.collect.UnmodifiableIterator
remove
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Iterator
forEachRemaining
-
-
-
-
Constructor Detail
-
AbstractSequentialIterator
protected AbstractSequentialIterator(@CheckForNull T firstOrNull)
Creates a new iterator with the given first element, or, iffirstOrNullis null, creates a new empty iterator.
-
-
Method Detail
-
computeNext
@CheckForNull protected abstract T computeNext(T previous)
Returns the element that followsprevious, or returnsnullif no elements remain. This method is invoked during each call tonext()in order to compute the result of a future call tonext().
-
hasNext
public final boolean hasNext()
Description copied from interface:java.util.IteratorReturnstrueif the iteration has more elements. (In other words, returnstrueifIterator.next()would return an element rather than throwing an exception.)- Returns:
trueif the iteration has more elements
-
next
public final T next()
Description copied from interface:java.util.IteratorReturns the next element in the iteration.- Returns:
- the next element in the iteration
-
-