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.functor.core.collection;
18
19 import java.util.Iterator;
20
21 import org.apache.commons.functor.UnaryFunction;
22
23 /**
24 * Iterator that transforms another Iterator by applying a UnaryFunction to each returned element.
25 * @version $Revision: 1156793 $ $Date: 2011-08-11 22:06:24 +0200 (Thu, 11 Aug 2011) $
26 * @author Rodney Waldhoff
27 */
28 public final class TransformedIterator<E, T> implements Iterator<T> {
29
30 // attributes
31 // ------------------------------------------------------------------------
32
33 private final UnaryFunction<? super E, ? extends T> function;
34 private final Iterator<? extends E> iterator;
35
36 // constructor
37 // ------------------------------------------------------------------------
38 /**
39 * Create a new TransformedIterator.
40 * @param iterator Iterator to decorate
41 * @param function to apply
42 */
43 public TransformedIterator(Iterator<? extends E> iterator, UnaryFunction<? super E, ? extends T> function) {
44 if (null == iterator) {
45 throw new IllegalArgumentException("Iterator argument was null");
46 }
47 if (null == function) {
48 throw new IllegalArgumentException("filtering UnaryFunction argument was null");
49 }
50 this.function = function;
51 this.iterator = iterator;
52 }
53
54 // iterator methods
55 // ------------------------------------------------------------------------
56
57 /**
58 * {@inheritDoc}
59 * @see java.util.Iterator#hasNext()
60 */
61 public boolean hasNext() {
62 return iterator.hasNext();
63 }
64
65 /**
66 * {@inheritDoc}
67 * @see java.util.Iterator#next()
68 */
69 public T next() {
70 return function.evaluate(iterator.next());
71 }
72
73 /**
74 * {@inheritDoc}
75 * @see java.util.Iterator#remove()
76 */
77 public void remove() {
78 iterator.remove();
79 }
80
81 /**
82 * {@inheritDoc}
83 */
84 public boolean equals(Object obj) {
85 if (obj == this) {
86 return true;
87 }
88 if (!(obj instanceof TransformedIterator<?, ?>)) {
89 return false;
90 }
91 TransformedIterator<?, ?> that = (TransformedIterator<?, ?>) obj;
92 return function.equals(that.function) && iterator.equals(that.iterator);
93 }
94
95 /**
96 * {@inheritDoc}
97 */
98 public int hashCode() {
99 int hash = "TransformedIterator".hashCode();
100 hash <<= 2;
101 hash ^= function.hashCode();
102 hash <<= 2;
103 hash ^= iterator.hashCode();
104 return hash;
105 }
106
107 /**
108 * {@inheritDoc}
109 */
110 public String toString() {
111 return "TransformedIterator<" + iterator + "," + function + ">";
112 }
113
114 // class methods
115 // ------------------------------------------------------------------------
116 /**
117 * Get a Transformed Iterator instance.
118 * @param iter to decorate, if null result is null
119 * @param func transforming function, cannot be null
120 * @return Iterator<T>
121 */
122 public static <E, T> Iterator<T> transform(Iterator<? extends E> iter, UnaryFunction<? super E, ? extends T> func) {
123 if (null == iter) {
124 return null;
125 }
126 return new TransformedIterator<E, T>(iter, func);
127 }
128
129 /**
130 * Get an Iterator instance that may be transformed.
131 * @param iter to decorate, if null result is null
132 * @param func transforming function, if null result is iter
133 * @return Iterator<?>
134 */
135 public static <E> Iterator<?> maybeTransform(Iterator<? extends E> iter, UnaryFunction<? super E, ?> func) {
136 return null == func ? (null == iter ? null : iter) : new TransformedIterator<E, Object>(iter, func);
137 }
138
139 }