Class TransformIterator<I,O>

java.lang.Object
org.apache.commons.collections4.iterators.TransformIterator<I,O>
Type Parameters:
I - The type of the input to the function.
O - The type of the result of the function.
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:
1.0
  • Constructor Details

    • TransformIterator

      Constructs a new TransformIterator that will not function until the setIterator and setTransformer(Transformer) methods are invoked.
    • TransformIterator

      public TransformIterator(Iterator<? extends 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<? extends I> iterator, Transformer<? super I,? extends 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, may not be null.
      transformer - The transformer to use, may be null to pass elements through unchanged
  • Method Details

    • getIterator

      public Iterator<? extends I> getIterator()
      Gets the iterator this iterator is using.
      Returns:
      The iterator.
    • getTransformer

      public Transformer<? super I,? extends O> getTransformer()
      Gets the transformer this iterator is using.
      Returns:
      The transformer, may be null.
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<I>
    • 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<I>
      Returns:
      The next object.
      Throws:
      NoSuchElementException - if there are no more elements.
    • remove

      public void remove()
      Specified by:
      remove in interface Iterator<I>
    • setIterator

      public void setIterator(Iterator<? extends 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.
    • setTransformer

      public void setTransformer(Transformer<? super I,? extends O> transformer)
      Sets the transformer this the iterator to use. A null transformer is a no-op transformer.
      Parameters:
      transformer - The transformer to use, may be null to pass elements through unchanged.
    • 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, may be null.
      Returns:
      The transformed object, the original object the transformer is null.