Neo4j Community

org.neo4j.helpers.collection
Class PrefetchingIterator<T>

java.lang.Object
  extended by org.neo4j.helpers.collection.PrefetchingIterator<T>
All Implemented Interfaces:
Iterator<T>
Direct Known Subclasses:
CatchingIteratorWrapper, CombiningIterator, FilteringIterator, LinesOfFileIterator, NestingIterator, RangeIterator

public abstract class PrefetchingIterator<T>
extends Object
implements Iterator<T>

Abstract class for how you usually implement iterators when you don't know how many objects there are (which is pretty much every time) Basically the hasNext() method will look up the next object and cache it with setPrefetchedNext(Object). The cached object is then set to null in next(). So you only have to implement one method, fetchNextOrNull which returns null when the iteration has reached the end, and you're done.


Constructor Summary
PrefetchingIterator()
           
 
Method Summary
protected abstract  T fetchNextOrNull()
           
protected  T getPrefetchedNextOrNull()
           
 boolean hasNext()
          Tries to fetch the next item and caches it so that consecutive calls (w/o an intermediate call to next() will remember it and won't try to fetch it again.
 T next()
          Uses hasNext() to try to fetch the next item and returns it if found, otherwise it throws a NoSuchElementException.
 void remove()
           
protected  void setPrefetchedNext(T nextOrNull)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrefetchingIterator

public PrefetchingIterator()
Method Detail

hasNext

public boolean hasNext()
Tries to fetch the next item and caches it so that consecutive calls (w/o an intermediate call to next() will remember it and won't try to fetch it again.

Specified by:
hasNext in interface Iterator<T>
Returns:
true if there was a next item to return in the next call to next().

next

public T next()
Uses hasNext() to try to fetch the next item and returns it if found, otherwise it throws a NoSuchElementException.

Specified by:
next in interface Iterator<T>
Returns:
the next item in the iteration, or throws NoSuchElementException if there's no more items to return.

fetchNextOrNull

protected abstract T fetchNextOrNull()

setPrefetchedNext

protected void setPrefetchedNext(T nextOrNull)

getPrefetchedNextOrNull

protected T getPrefetchedNextOrNull()

remove

public void remove()
Specified by:
remove in interface Iterator<T>

Neo4j Community

Copyright © 2011 The Neo4j Graph Database Project. All Rights Reserved.