Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.collect;
 
 import static com.google.common.base.Preconditions.checkState;
 
 
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(s):
Kevin Bourrillion
Since:
2.0 (imported from Google Collections Library)
 
 // When making changes to this class, please also update the copy at
 // com.google.common.base.AbstractIterator
 public abstract class AbstractIterator<T> extends UnmodifiableIterator<T> {
   private State state = .;

  
Constructor for use by subclasses.
 
   protected AbstractIterator() {}
 
   private enum State {
    
We have computed the next element and haven't returned it yet.
 
     READY,

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

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

    
We've suffered an exception and are kaput.
 
     FAILED,
   }
 
   private T next;

  
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 java.lang.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:
the next element if there was one. If endOfData was called during execution, the return value will be ignored.
Throws:
java.lang.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 java.lang.IllegalStateException.
  protected abstract T computeNext();

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

Returns:
null; a convenience so your computeNext implementation can use the simple statement return endOfData();
  protected final T endOfData() {
     = .;
    return null;
  }
  public final boolean hasNext() {
    switch () {
      case :
        return false;
      case :
        return true;
      default:
    }
    return tryToComputeNext();
  }
  private boolean tryToComputeNext() {
     = .// temporary pessimism
     = computeNext();
    if ( != .) {
       = .;
      return true;
    }
    return false;
  }
  public final T next() {
    if (!hasNext()) {
      throw new NoSuchElementException();
    }
     = .;
    return ;
  }

  
Returns the next element in the iteration without advancing the iteration, according to the contract of com.google.common.collect.PeekingIterator.peek().

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

  public final T peek() {
    if (!hasNext()) {
      throw new NoSuchElementException();
    }
    return ;
  }
New to GrepCode? Check out our FAQ X