View Javadoc
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  
19  import java.util.Iterator;
20  
21  import org.apache.commons.collections4.Transformer;
22  
23  /**
24   * Decorates an iterator such that each element returned is transformed.
25   *
26   * @since 1.0
27   */
28  public class TransformIterator<I, O> implements Iterator<O> {
29  
30      /** The iterator being used */
31      private Iterator<? extends I> iterator;
32      /** The transformer being used */
33      private Transformer<? super I, ? extends O> transformer;
34  
35      /**
36       * Constructs a new {@code TransformIterator} that will not function
37       * until the {@link #setIterator(Iterator) setIterator} and
38       * {@link #setTransformer(Transformer)} methods are invoked.
39       */
40      public TransformIterator() {
41      }
42  
43      /**
44       * Constructs a new {@code TransformIterator} that won't transform
45       * elements from the given iterator.
46       *
47       * @param iterator  the iterator to use
48       */
49      public TransformIterator(final Iterator<? extends I> iterator) {
50          this.iterator = iterator;
51      }
52  
53      /**
54       * Constructs a new {@code TransformIterator} that will use the
55       * given iterator and transformer.  If the given transformer is null,
56       * then objects will not be transformed.
57       *
58       * @param iterator  the iterator to use
59       * @param transformer  the transformer to use
60       */
61      public TransformIterator(final Iterator<? extends I> iterator,
62                               final Transformer<? super I, ? extends O> transformer) {
63          this.iterator = iterator;
64          this.transformer = transformer;
65      }
66  
67      /**
68       * Gets the iterator this iterator is using.
69       *
70       * @return the iterator.
71       */
72      public Iterator<? extends I> getIterator() {
73          return iterator;
74      }
75  
76      /**
77       * Gets the transformer this iterator is using.
78       *
79       * @return the transformer.
80       */
81      public Transformer<? super I, ? extends O> getTransformer() {
82          return transformer;
83      }
84  
85      @Override
86      public boolean hasNext() {
87          return iterator.hasNext();
88      }
89  
90      /**
91       * Gets the next object from the iteration, transforming it using the
92       * current transformer. If the transformer is null, no transformation
93       * occurs and the object from the iterator is returned directly.
94       *
95       * @return the next object
96       * @throws java.util.NoSuchElementException if there are no more elements
97       */
98      @Override
99      public O next() {
100         return transform(iterator.next());
101     }
102 
103     @Override
104     public void remove() {
105         iterator.remove();
106     }
107 
108     /**
109      * Sets the iterator for this iterator to use.
110      * If iteration has started, this effectively resets the iterator.
111      *
112      * @param iterator  the iterator to use
113      */
114     public void setIterator(final Iterator<? extends I> iterator) {
115         this.iterator = iterator;
116     }
117 
118     /**
119      * Sets the transformer this the iterator to use.
120      * A null transformer is a no-op transformer.
121      *
122      * @param transformer  the transformer to use
123      */
124     public void setTransformer(final Transformer<? super I, ? extends O> transformer) {
125         this.transformer = transformer;
126     }
127 
128     /**
129      * Transforms the given object using the transformer.
130      * If the transformer is null, the original object is returned as-is.
131      *
132      * @param source  the object to transform
133      * @return the transformed object
134      */
135     protected O transform(final I source) {
136         return transformer.transform(source);
137     }
138 }