org.apache.commons.collections.iterators
Class TransformIterator<I,O>

java.lang.Object
  extended by org.apache.commons.collections.iterators.TransformIterator<I,O>
All Implemented Interfaces:
Iterator<O>

public class TransformIterator<I,O>
extends Object
implements Iterator<O>

Decorates an iterator such that each element returned is transformed.

Since:
Commons Collections 1.0
Version:
$Revision: 1.1.1.1 $ $Date: 2005/05/23 04:35:02 $
Author:
James Strachan, Matt Hall, John Watkinson, Stephen Colebourne

Constructor Summary
TransformIterator()
          Constructs a new TransformIterator that will not function until the setIterator method is invoked.
TransformIterator(Iterator<I> iterator)
          Constructs a new TransformIterator that won't transform elements from the given iterator.
TransformIterator(Iterator<I> iterator, Transformer<I,O> transformer)
          Constructs a new TransformIterator that will use the given iterator and transformer.
 
Method Summary
 Iterator<I> getIterator()
          Gets the iterator this iterator is using.
 Transformer<I,O> getTransformer()
          Gets the transformer this iterator is using.
 boolean hasNext()
           
 O next()
          Gets the next object from the iteration, transforming it using the current transformer.
 void remove()
           
 void setIterator(Iterator<I> iterator)
          Sets the iterator for this iterator to use.
 void setTransformer(Transformer<I,O> transformer)
          Sets the transformer this the iterator to use.
protected  O transform(I source)
          Transforms the given object using the transformer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TransformIterator

public TransformIterator()
Constructs a new TransformIterator that will not function until the setIterator method is invoked.


TransformIterator

public TransformIterator(Iterator<I> iterator)
Constructs a new TransformIterator that won't transform elements from the given iterator.

Parameters:
iterator - the iterator to use

TransformIterator

public TransformIterator(Iterator<I> iterator,
                         Transformer<I,O> transformer)
Constructs a new TransformIterator that will use the given iterator and transformer. If the given transformer is null, then objects will not be transformed.

Parameters:
iterator - the iterator to use
transformer - the transformer to use
Method Detail

hasNext

public boolean hasNext()
Specified by:
hasNext in interface Iterator<O>

next

public O next()
Gets the next object from the iteration, transforming it using the current transformer. If the transformer is null, no transformation occurs and the object from the iterator is returned directly.

Specified by:
next in interface Iterator<O>
Returns:
the next object
Throws:
NoSuchElementException - if there are no more elements

remove

public void remove()
Specified by:
remove in interface Iterator<O>

getIterator

public Iterator<I> getIterator()
Gets the iterator this iterator is using.

Returns:
the iterator.

setIterator

public void setIterator(Iterator<I> iterator)
Sets the iterator for this iterator to use. If iteration has started, this effectively resets the iterator.

Parameters:
iterator - the iterator to use

getTransformer

public Transformer<I,O> getTransformer()
Gets the transformer this iterator is using.

Returns:
the transformer.

setTransformer

public void setTransformer(Transformer<I,O> transformer)
Sets the transformer this the iterator to use. A null transformer is a no-op transformer.

Parameters:
transformer - the transformer to use

transform

protected O transform(I source)
Transforms the given object using the transformer. If the transformer is null, the original object is returned as-is.

Parameters:
source - the object to transform
Returns:
the transformed object


Copyright © 2005-2005 Apache Software Foundation, Matt Hall, John Watkinson. All Rights Reserved.