net.sf.staccatocommons.iterators
Class UpdateCurrentThriterator<A>

java.lang.Object
  extended by net.sf.staccatocommons.iterators.AbstractUnmodifiableIterator<A>
      extended by net.sf.staccatocommons.iterators.thriter.AbstractThriterator<A>
          extended by net.sf.staccatocommons.iterators.thriter.AdvanceThriterator<A>
              extended by net.sf.staccatocommons.iterators.UpdateCurrentThriterator<A>
Type Parameters:
A -
All Implemented Interfaces:
Iterator<A>, EmptyAware, Thriter<A>, Thriterator<A>

public abstract class UpdateCurrentThriterator<A>
extends AdvanceThriterator<A>

A thriterator that is implemented through a single message updateCurrent(), which is both responsible for answering if there is a current element and setting it.

This is useful when implementor does not know if it has a next element until it effectively computes it. This implementation of Thriterator resolves it by caching the temporal element

Typical implementation of that method is the following:

 protected boolean updateCurrent() {
   A element = .. compute next element ..
   if ( .. element matches a criteria ..  ) {
     setCurrent(element);
   }
 }
 

Since:
2.2
Author:
flbulgarelli

Constructor Summary
UpdateCurrentThriterator()
           
 
Method Summary
 void advanceNext()
          Advances to the thriter to the position of the next element.
 A current()
          Answers element at the current position of this Thriter.
 boolean hasNext()
          Answers if the thriter has more elements, that is, if sending Thriter.advanceNext() would not result in a NoSuchElementException
protected  void setCurrent(A current)
          Sets the current value of iteration.
protected abstract  void updateCurrent()
          Sets the current element, if any.
 
Methods inherited from class net.sf.staccatocommons.iterators.thriter.AdvanceThriterator
next
 
Methods inherited from class net.sf.staccatocommons.iterators.thriter.AbstractThriterator
delayedCurrent, delayedNext, isEmpty
 
Methods inherited from class net.sf.staccatocommons.iterators.AbstractUnmodifiableIterator
remove
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Iterator
remove
 

Constructor Detail

UpdateCurrentThriterator

public UpdateCurrentThriterator()
Method Detail

hasNext

public final boolean hasNext()
Description copied from interface: Thriter
Answers if the thriter has more elements, that is, if sending Thriter.advanceNext() would not result in a NoSuchElementException

Returns:
if the Thriter has more elements

advanceNext

public final void advanceNext()
                       throws NoSuchElementException
Description copied from interface: Thriter
Advances to the thriter to the position of the next element.

Throws:
NoSuchElementException - if there are no more elements

setCurrent

protected final void setCurrent(A current)
Sets the current value of iteration.

Parameters:
current - the current value of iteration

current

public final A current()
Description copied from interface: Thriter
Answers element at the current position of this Thriter. Result of this method if Thriter.advanceNext() was never evaluated before is undefined.

Returns:
the current element

updateCurrent

protected abstract void updateCurrent()
Sets the current element, if any. Implementors need to send setCurrent(Object) in order to indicate which is the next element. If there is no current element - end of source has reached - implementors must do nothing.



Get Staccatocommons at SourceForge.net. Fast, secure and Free Open Source software downloads