TransformIterator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.iterators;

  18. import java.util.Iterator;

  19. import org.apache.commons.collections4.Transformer;

  20. /**
  21.  * Decorates an iterator such that each element returned is transformed.
  22.  *
  23.  * @param <I> the type of the input to the function.
  24.  * @param <O> the type of the result of the function.
  25.  * @since 1.0
  26.  */
  27. public class TransformIterator<I, O> implements Iterator<O> {

  28.     /** The iterator being used */
  29.     private Iterator<? extends I> iterator;
  30.     /** The transformer being used */
  31.     private Transformer<? super I, ? extends O> transformer;

  32.     /**
  33.      * Constructs a new {@code TransformIterator} that will not function
  34.      * until the {@link #setIterator(Iterator) setIterator} and
  35.      * {@link #setTransformer(Transformer)} methods are invoked.
  36.      */
  37.     public TransformIterator() {
  38.     }

  39.     /**
  40.      * Constructs a new {@code TransformIterator} that won't transform
  41.      * elements from the given iterator.
  42.      *
  43.      * @param iterator  the iterator to use
  44.      */
  45.     public TransformIterator(final Iterator<? extends I> iterator) {
  46.         this.iterator = iterator;
  47.     }

  48.     /**
  49.      * Constructs a new {@code TransformIterator} that will use the
  50.      * given iterator and transformer.  If the given transformer is null,
  51.      * then objects will not be transformed.
  52.      *
  53.      * @param iterator  the iterator to use
  54.      * @param transformer  the transformer to use
  55.      */
  56.     public TransformIterator(final Iterator<? extends I> iterator,
  57.                              final Transformer<? super I, ? extends O> transformer) {
  58.         this.iterator = iterator;
  59.         this.transformer = transformer;
  60.     }

  61.     /**
  62.      * Gets the iterator this iterator is using.
  63.      *
  64.      * @return the iterator.
  65.      */
  66.     public Iterator<? extends I> getIterator() {
  67.         return iterator;
  68.     }

  69.     /**
  70.      * Gets the transformer this iterator is using.
  71.      *
  72.      * @return the transformer.
  73.      */
  74.     public Transformer<? super I, ? extends O> getTransformer() {
  75.         return transformer;
  76.     }

  77.     @Override
  78.     public boolean hasNext() {
  79.         return iterator.hasNext();
  80.     }

  81.     /**
  82.      * Gets the next object from the iteration, transforming it using the
  83.      * current transformer. If the transformer is null, no transformation
  84.      * occurs and the object from the iterator is returned directly.
  85.      *
  86.      * @return the next object
  87.      * @throws java.util.NoSuchElementException if there are no more elements
  88.      */
  89.     @Override
  90.     public O next() {
  91.         return transform(iterator.next());
  92.     }

  93.     @Override
  94.     public void remove() {
  95.         iterator.remove();
  96.     }

  97.     /**
  98.      * Sets the iterator for this iterator to use.
  99.      * If iteration has started, this effectively resets the iterator.
  100.      *
  101.      * @param iterator  the iterator to use
  102.      */
  103.     public void setIterator(final Iterator<? extends I> iterator) {
  104.         this.iterator = iterator;
  105.     }

  106.     /**
  107.      * Sets the transformer this the iterator to use.
  108.      * A null transformer is a no-op transformer.
  109.      *
  110.      * @param transformer  the transformer to use
  111.      */
  112.     public void setTransformer(final Transformer<? super I, ? extends O> transformer) {
  113.         this.transformer = transformer;
  114.     }

  115.     /**
  116.      * Transforms the given object using the transformer.
  117.      * If the transformer is null, the original object is returned as-is.
  118.      *
  119.      * @param source  the object to transform
  120.      * @return the transformed object
  121.      */
  122.     protected O transform(final I source) {
  123.         return transformer.apply(source);
  124.     }
  125. }