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