jut.lang
Class AbstractOneTimeIterable<T>

java.lang.Object
  extended by jut.lang.AbstractOneTimeIterable<T>
All Implemented Interfaces:
Iterable<T>, OneTimeIterable<T>
Direct Known Subclasses:
BlockingOpenIterable, OneTimeIterableWrapper

public abstract class AbstractOneTimeIterable<T>
extends Object
implements OneTimeIterable<T>

template implementation of OneTimeIterable. the iterator of this template does not support the Iterator.remove() method. as convenience, a user method can be called before handing over the result of Iterator.next(), see onNext(jut.lang.ParamRunnable).

the "one Iterator only" check is thread safe.

this template class is part of the problem solution that in the standard JDK, Iterables are supposed to produce any number of Iterators. see also OneTimeIterable and OneTimeIterableWrapper.

Author:
Georg Dietrich

Constructor Summary
AbstractOneTimeIterable()
           
 
Method Summary
 Iterator<T> iterator()
          the first time called, returns an iterator over a set of elements of type T. subsequent calls should result in an IllegalStateException.
protected abstract  boolean iteratorHasNext()
          returns true if the iterator has more elements.
protected abstract  T iteratorNext()
          returns the next element in the iterator.
 void onNext(ParamRunnable<T> onNextObject)
          set the convenience user object to call before handing over the result of Iterator.next().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractOneTimeIterable

public AbstractOneTimeIterable()
Method Detail

onNext

public final void onNext(ParamRunnable<T> onNextObject)
set the convenience user object to call before handing over the result of Iterator.next().

Parameters:
onNextObject - - the convenience user object to call before handing over the result of Iterator.next().

iterator

public final Iterator<T> iterator()
the first time called, returns an iterator over a set of elements of type T. subsequent calls should result in an IllegalStateException. the iterator does not support the Iterator.remove() method. the "one Iterator only" check is thread safe.

Specified by:
iterator in interface Iterable<T>
Specified by:
iterator in interface OneTimeIterable<T>
Returns:
an iterator over a set of elements of type T.

iteratorNext

protected abstract T iteratorNext()
                           throws Exception
returns the next element in the iterator.

Returns:
the next element in the iterator.
Throws:
Exception - to indicate something went wrong. the exception is re-thrown, either directly (for RuntimeExceptions, or as NestedException.

iteratorHasNext

protected abstract boolean iteratorHasNext()
                                    throws Exception
returns true if the iterator has more elements. (in other words, returns true if iteratorNext() would return an element rather than throwing an exception.)

Returns:
true if the iterator has more elements, false if not.
Throws:
Exception - to indicate something went wrong. the exception is re-thrown, either directly (for RuntimeExceptions, or as NestedException.