org.knime.core.data.util
Class SynchronizedIterator<T>

java.lang.Object
  extended by org.knime.core.data.util.SynchronizedIterator<T>
Type Parameters:
T - The elements contained in the Iterable.

public final class SynchronizedIterator<T>
extends Object

Synchronized (thread-safe) wrapper for an Iterator. This class does not implement the Iterator interface as it does not follow the usual hasNext(), next() procedure (which is not a atomic sequence). Instead, this class should be used as follows:

 final SynchronizedIterator<T> it = 
      new SynchronizedIterator<T>(wrappedIterator);
      
 // this is usually executed in different threads
 T next;
 while ((next = it.next()) != null) {
 }
 
Alternatively, this class provides a method nextWithIndex(), which wraps the iteration element and its index in the iteration loop in a SynchronizedIterator.ElementAndIndex.

Author:
Bernd Wiswedel, University of Konstanz

Nested Class Summary
static class SynchronizedIterator.ElementAndIndex<T>
          Class combining an iteration element with its index in the iteration loop.
 
Constructor Summary
SynchronizedIterator(Iterable<T> iterable)
          Creates new iterator from an Iterable.
SynchronizedIterator(Iterator<T> it)
          Creates new iterator from a given (unsynchronized) iterator.
 
Method Summary
 T next()
           
 SynchronizedIterator.ElementAndIndex<T> nextWithIndex()
          Get the next element along with its index in the iteration (starting with 0 for the first element) or null if no more elements are available.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SynchronizedIterator

public SynchronizedIterator(Iterator<T> it)
Creates new iterator from a given (unsynchronized) iterator.

Parameters:
it - The iterator instance to wrap.
Throws:
NullPointerException - If the argument is null.

SynchronizedIterator

public SynchronizedIterator(Iterable<T> iterable)
Creates new iterator from an Iterable.

Parameters:
iterable - Argument iterable.
Throws:
NullPointerException - If the argument is null.
Method Detail

next

public T next()
Returns:
the next element in the iteration or null if no more are available.

nextWithIndex

public SynchronizedIterator.ElementAndIndex<T> nextWithIndex()
Get the next element along with its index in the iteration (starting with 0 for the first element) or null if no more elements are available.

Returns:
the next element with index


Copyright, 2003 - 2010. All rights reserved.
University of Konstanz, Germany.
Chair for Bioinformatics and Information Mining, Prof. Dr. Michael R. Berthold.
You may not modify, publish, transmit, transfer or sell, reproduce, create derivative works from, distribute, perform, display, or in any way exploit any of the content, in whole or in part, except as otherwise expressly permitted in writing by the copyright owner or as specified in the license file distributed with this product.