net.sf.jga.util
Class CachingIterator<T>

java.lang.Object
  extended by net.sf.jga.util.CachingIterator<T>
All Implemented Interfaces:
java.lang.Iterable<T>, java.util.Iterator<T>

public class CachingIterator<T>
extends java.lang.Object
implements java.util.Iterator<T>, java.lang.Iterable<T>

Iterator that allows the program to retain the last few elements returned.

Copyright © 2003-2005 David A. Hall

Author:
David A. Hall

Constructor Summary
CachingIterator(java.util.Iterator<? extends T> base)
          Builds a CachingIterator that can retain 1 element.
CachingIterator(java.util.Iterator<? extends T> base, int max)
          Builds a CachingIterator that can retain the given number of elements.
 
Method Summary
 T cached(int n)
          Returns the Nth previous element consumed from the underlying iterator.
 int getCacheCount()
          Returns the number of elements in the cache
 int getCacheSize()
          Returns the cache size
 boolean hasCached(int n)
          Returns true if there have been at least N elements consumed from the underlying iterator.
 boolean hasNext()
           
 java.util.Iterator<T> iterator()
           
 T next()
           
 void remove()
          Removes the last item returned from the base iterator, if the base iterator supports the remove operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CachingIterator

public CachingIterator(java.util.Iterator<? extends T> base)
Builds a CachingIterator that can retain 1 element.


CachingIterator

public CachingIterator(java.util.Iterator<? extends T> base,
                       int max)
Builds a CachingIterator that can retain the given number of elements.

Throws:
java.lang.IllegalArgumentException - if max <= 0.
Method Detail

hasCached

public boolean hasCached(int n)
Returns true if there have been at least N elements consumed from the underlying iterator.


cached

public T cached(int n)
Returns the Nth previous element consumed from the underlying iterator.


getCacheSize

public int getCacheSize()
Returns the cache size


getCacheCount

public int getCacheCount()
Returns the number of elements in the cache


iterator

public java.util.Iterator<T> iterator()
Specified by:
iterator in interface java.lang.Iterable<T>

hasNext

public boolean hasNext()
Specified by:
hasNext in interface java.util.Iterator<T>

next

public T next()
Specified by:
next in interface java.util.Iterator<T>

remove

public void remove()
Removes the last item returned from the base iterator, if the base iterator supports the remove operation. The item is not removed from the cache.

Specified by:
remove in interface java.util.Iterator<T>
Throws:
java.lang.UnsupportedOperationException - if the base iterator does not support the remove() operation


Copyright © 2002-2006 David A. Hall. All Rights Reserved.