001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.iterators;
017:
018: import java.util.Iterator;
019:
020: import org.apache.commons.collections.Transformer;
021:
022: /**
023: * Decorates an iterator such that each element returned is transformed.
024: *
025: * @since Commons Collections 1.0
026: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
027: *
028: * @author James Strachan
029: * @author Stephen Colebourne
030: */
031: public class TransformIterator implements Iterator {
032:
033: /** The iterator being used */
034: private Iterator iterator;
035: /** The transformer being used */
036: private Transformer transformer;
037:
038: //-----------------------------------------------------------------------
039: /**
040: * Constructs a new <code>TransformIterator</code> that will not function
041: * until the {@link #setIterator(Iterator) setIterator} method is
042: * invoked.
043: */
044: public TransformIterator() {
045: super ();
046: }
047:
048: /**
049: * Constructs a new <code>TransformIterator</code> that won't transform
050: * elements from the given iterator.
051: *
052: * @param iterator the iterator to use
053: */
054: public TransformIterator(Iterator iterator) {
055: super ();
056: this .iterator = iterator;
057: }
058:
059: /**
060: * Constructs a new <code>TransformIterator</code> that will use the
061: * given iterator and transformer. If the given transformer is null,
062: * then objects will not be transformed.
063: *
064: * @param iterator the iterator to use
065: * @param transformer the transformer to use
066: */
067: public TransformIterator(Iterator iterator, Transformer transformer) {
068: super ();
069: this .iterator = iterator;
070: this .transformer = transformer;
071: }
072:
073: //-----------------------------------------------------------------------
074: public boolean hasNext() {
075: return iterator.hasNext();
076: }
077:
078: /**
079: * Gets the next object from the iteration, transforming it using the
080: * current transformer. If the transformer is null, no transformation
081: * occurs and the object from the iterator is returned directly.
082: *
083: * @return the next object
084: * @throws java.util.NoSuchElementException if there are no more elements
085: */
086: public Object next() {
087: return transform(iterator.next());
088: }
089:
090: public void remove() {
091: iterator.remove();
092: }
093:
094: //-----------------------------------------------------------------------
095: /**
096: * Gets the iterator this iterator is using.
097: *
098: * @return the iterator.
099: */
100: public Iterator 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 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 transform(Object source) {
143: if (transformer != null) {
144: return transformer.transform(source);
145: }
146: return source;
147: }
148: }
|