KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > iterators > TransformIterator


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

16 package org.apache.commons.collections.iterators;
17
18 import java.util.Iterator JavaDoc;
19
20 import org.apache.commons.collections.Transformer;
21
22 /**
23  * Decorates an iterator such that each element returned is transformed.
24  *
25  * @since Commons Collections 1.0
26  * @version $Revision: 1.9 $ $Date: 2004/02/18 00:59:50 $
27  *
28  * @author James Strachan
29  * @author Stephen Colebourne
30  */

31 public class TransformIterator implements Iterator JavaDoc {
32
33     /** The iterator being used */
34     private Iterator JavaDoc iterator;
35     /** The transformer being used */
36     private Transformer transformer;
37
38     //-----------------------------------------------------------------------
39
/**
40      * Constructs a new <code>TransformIterator</code> that will not function
41      * until the {@link #setIterator(Iterator) setIterator} method is
42      * invoked.
43      */

44     public TransformIterator() {
45         super();
46     }
47
48     /**
49      * Constructs a new <code>TransformIterator</code> that won't transform
50      * elements from the given iterator.
51      *
52      * @param iterator the iterator to use
53      */

54     public TransformIterator(Iterator JavaDoc iterator) {
55         super();
56         this.iterator = iterator;
57     }
58
59     /**
60      * Constructs a new <code>TransformIterator</code> that will use the
61      * given iterator and transformer. If the given transformer is null,
62      * then objects will not be transformed.
63      *
64      * @param iterator the iterator to use
65      * @param transformer the transformer to use
66      */

67     public TransformIterator(Iterator JavaDoc iterator, Transformer transformer) {
68         super();
69         this.iterator = iterator;
70         this.transformer = transformer;
71     }
72
73     //-----------------------------------------------------------------------
74
public boolean hasNext() {
75         return iterator.hasNext();
76     }
77
78     /**
79      * Gets the next object from the iteration, transforming it using the
80      * current transformer. If the transformer is null, no transformation
81      * occurs and the object from the iterator is returned directly.
82      *
83      * @return the next object
84      * @throws java.util.NoSuchElementException if there are no more elements
85      */

86     public Object JavaDoc next() {
87         return transform(iterator.next());
88     }
89
90     public void remove() {
91         iterator.remove();
92     }
93
94     //-----------------------------------------------------------------------
95
/**
96      * Gets the iterator this iterator is using.
97      *
98      * @return the iterator.
99      */

100     public Iterator JavaDoc getIterator() {
101         return iterator;
102     }
103
104     /**
105      * Sets the iterator for this iterator to use.
106      * If iteration has started, this effectively resets the iterator.
107      *
108      * @param iterator the iterator to use
109      */

110     public void setIterator(Iterator JavaDoc iterator) {
111         this.iterator = iterator;
112     }
113
114     //-----------------------------------------------------------------------
115
/**
116      * Gets the transformer this iterator is using.
117      *
118      * @return the transformer.
119      */

120     public Transformer getTransformer() {
121         return transformer;
122     }
123
124     /**
125      * Sets the transformer this the iterator to use.
126      * A null transformer is a no-op transformer.
127      *
128      * @param transformer the transformer to use
129      */

130     public void setTransformer(Transformer transformer) {
131         this.transformer = transformer;
132     }
133
134     //-----------------------------------------------------------------------
135
/**
136      * Transforms the given object using the transformer.
137      * If the transformer is null, the original object is returned as-is.
138      *
139      * @param source the object to transform
140      * @return the transformed object
141      */

142     protected Object JavaDoc transform(Object JavaDoc source) {
143         if (transformer != null) {
144             return transformer.transform(source);
145         }
146         return source;
147     }
148 }
149
Popular Tags