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

java.lang.Object
  extended by org.neo4j.helpers.collection.CachingIterator<T>
Type Parameters:
T - the type of items in the iterator.
All Implemented Interfaces:
Iterator<T>, ListIterator<T>
Direct Known Subclasses:
PagingIterator

public class CachingIterator<T>
extends Object
implements ListIterator<T>

An Iterator which lazily fetches and caches items from the underlying iterator when items are requested. This enables positioning as well as going backwards through the iteration.

Author:
Mattias Persson

Constructor Summary
CachingIterator(Iterator<T> source)
          Creates a new caching iterator using source as its underlying Iterator to get items lazily from.
 
Method Summary
 void add(T e)
          Not supported by this implement.
 T current()
          Returns the last item returned by next()/previous().
 boolean hasNext()
          Returns whether a call to next() will be able to return an item or not.
 boolean hasPrevious()
          Returns whether or not a call to previous() will be able to return an item or not.
 T next()
          Returns the next item given the current position().
 int nextIndex()
          
 int position()
          Returns the current position of the iterator, initially 0.
 int position(int newPosition)
          Sets the position of the iterator.
 T previous()
          
 int previousIndex()
          
 void remove()
          Not supported by this implement.
 void set(T e)
          Not supported by this implement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CachingIterator

public CachingIterator(Iterator<T> source)
Creates a new caching iterator using source as its underlying Iterator to get items lazily from.

Parameters:
source - the underlying Iterator to lazily get items from.
Method Detail

hasNext

public boolean hasNext()
Returns whether a call to next() will be able to return an item or not. If the current position() is beyond the size of the cache (as will be the case if only calls to hasNext()/next() has been made up to this point) the underlying iterator is asked, else true since it can be returned from the cache.

Specified by:
hasNext in interface Iterator<T>
Specified by:
hasNext in interface ListIterator<T>
Returns:
whether or not there are more items in this iteration given the current position().

next

public T next()
Returns the next item given the current position(). If the current position() is beyond the size of the cache (as will be the case if only calls to hasNext()/next() has been made up to this point) the underlying iterator is asked for the next item (and cached if there was one), else the item is returned from the cache.

Specified by:
next in interface Iterator<T>
Specified by:
next in interface ListIterator<T>
Returns:
the next item given the current position().

position

public int position()
Returns the current position of the iterator, initially 0. The position represents the index of the item which will be returned by the next call to next() and also the index of the next item returned by previous() plus one. An example:

Returns:
the position of the iterator.

position

public int position(int newPosition)
Sets the position of the iterator. 0 means all the way back to the beginning. It is also possible to set the position to one higher than the last item, so that the next call to previous() would return the last item. Items will be cached along the way if necessary.

Parameters:
newPosition - the position to set for the iterator, must be non-negative.
Returns:
the position before changing to the new position.

hasPrevious

public boolean hasPrevious()
Returns whether or not a call to previous() will be able to return an item or not. So it will return true if position() is bigger than 0.

Specified by:
hasPrevious in interface ListIterator<T>

previous

public T previous()

Specified by:
previous in interface ListIterator<T>

current

public T current()
Returns the last item returned by next()/previous(). If no call has been made to next() or previous() since this iterator was created or since a call to position(int) has been made a NoSuchElementException will be thrown.

Returns:
the last item returned by next()/previous().
Throws:
NoSuchElementException - if no call has been made to next() or previous() since this iterator was created or since a call to position(int) has been made.

nextIndex

public int nextIndex()

Specified by:
nextIndex in interface ListIterator<T>

previousIndex

public int previousIndex()

Specified by:
previousIndex in interface ListIterator<T>

remove

public void remove()
Not supported by this implement.

Specified by:
remove in interface Iterator<T>
Specified by:
remove in interface ListIterator<T>

set

public void set(T e)
Not supported by this implement.

Specified by:
set in interface ListIterator<T>

add

public void add(T e)
Not supported by this implement.

Specified by:
add in interface ListIterator<T>


Copyright © 2002-2012 The Neo4j Graph Database Project. All Rights Reserved.