Top Description Inners Fields Constructors Methods
com.google.common.collect

public abstract Class AbstractIterator<T extends @Nullable Object>

extends UnmodifiableIterator<T extends @Nullable Object>
Class Inheritance
Known Direct Subclasses
com.google.common.collect.Collections2.OrderedPermutationIterator, com.google.common.collect.Collections2.PermutationIterator, com.google.common.collect.ConsumingQueueIterator, com.google.common.collect.StandardTable.Column.EntrySetIterator, com.google.common.collect.StandardTable.ColumnKeyIterator, com.google.common.collect.TreeTraverser.PostOrderIterator, com.google.common.graph.EndpointPairIterator
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkState, com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT

This class provides a skeletal implementation of the 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.io.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.

Author
Kevin Bourrillion
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum

Field Summary

Modifier and TypeField and Description
private T
private AbstractIterator.State

Constructor Summary

AccessConstructor and Description
protected
AbstractIterator()

Constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
protected abstract T

Returns:

the next element if there was one. If endOfData was called during execution, the return value will be ignored.
computeNext
()

Returns the next element

Note

the implementation must call endOfData() when there are no elements left in the iteration.

protected final T

Returns:

null; a convenience so your computeNext implementation can use the simple statement return endOfData();
endOfData
()

Implementations of computeNext must invoke this method when there are no elements left in the iteration.

public final boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.
public final T
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.
public final T
peek()

Returns the next element in the iteration without advancing the iteration, according to the contract of PeekingIterator#peek().

private boolean
Inherited from com.google.common.collect.UnmodifiableIterator:
remove

Field Detail

nextback to summary
private T next
Annotations
@CheckForNull
stateback to summary
private AbstractIterator.State state

Constructor Detail

AbstractIteratorback to summary
protected AbstractIterator()

Constructor for use by subclasses.

Method Detail

computeNextback to summary
protected abstract T computeNext()

Returns the next element

Note

the implementation must call 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.

Returns:T

the next element if there was one. If endOfData was called during execution, the return value will be ignored.

Annotations
@CheckForNull
Exceptions
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.
endOfDataback to summary
protected final T endOfData()

Implementations of computeNext must invoke this method when there are no elements left in the iteration.

Returns:T

null; a convenience so your computeNext implementation can use the simple statement return endOfData();

Annotations
@CanIgnoreReturnValue
@CheckForNull
hasNextback to summary
public final boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

Annotations
@Override
nextback to summary
public final T next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:T

the next element in the iteration

Annotations
@CanIgnoreReturnValue
@Override
@ParametricNullness
peekback to summary
public final T peek()

Returns the next element in the iteration without advancing the iteration, according to the contract of PeekingIterator#peek().

Implementations of AbstractIterator that wish to expose this functionality should implement PeekingIterator.

Annotations
@ParametricNullness
tryToComputeNextback to summary
private boolean tryToComputeNext()
com.google.common.collect back to summary

private final Enum AbstractIterator.State

extends Enum<AbstractIterator.State>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final AbstractIterator.State
DONE

We have reached the end of the data and are finished.

public static final AbstractIterator.State
FAILED

We've suffered an exception and are kaput.

public static final AbstractIterator.State
NOT_READY

We haven't yet computed or have already returned the element.

public static final AbstractIterator.State
READY

We have computed the next element and haven't returned it yet.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static AbstractIterator.State
public static AbstractIterator.State[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

DONEback to summary
public static final AbstractIterator.State DONE

We have reached the end of the data and are finished.

FAILEDback to summary
public static final AbstractIterator.State FAILED

We've suffered an exception and are kaput.

NOT_READYback to summary
public static final AbstractIterator.State NOT_READY

We haven't yet computed or have already returned the element.

READYback to summary
public static final AbstractIterator.State READY

We have computed the next element and haven't returned it yet.

Constructor Detail

Stateback to summary
private State()

Method Detail

valueOfback to summary
public static AbstractIterator.State valueOf(String name)
valuesback to summary
public static AbstractIterator.State[] values()