001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.collections.iterators; 018 019 import java.util.Iterator; 020 021 import org.apache.commons.collections.Transformer; 022 023 /** 024 * Decorates an iterator such that each element returned is transformed. 025 * 026 * @since Commons Collections 1.0 027 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 028 * 029 * @author James Strachan 030 * @author Stephen Colebourne 031 */ 032 public class TransformIterator implements Iterator { 033 034 /** The iterator being used */ 035 private Iterator iterator; 036 /** The transformer being used */ 037 private Transformer transformer; 038 039 //----------------------------------------------------------------------- 040 /** 041 * Constructs a new <code>TransformIterator</code> that will not function 042 * until the {@link #setIterator(Iterator) setIterator} method is 043 * invoked. 044 */ 045 public TransformIterator() { 046 super(); 047 } 048 049 /** 050 * Constructs a new <code>TransformIterator</code> that won't transform 051 * elements from the given iterator. 052 * 053 * @param iterator the iterator to use 054 */ 055 public TransformIterator(Iterator iterator) { 056 super(); 057 this.iterator = iterator; 058 } 059 060 /** 061 * Constructs a new <code>TransformIterator</code> that will use the 062 * given iterator and transformer. If the given transformer is null, 063 * then objects will not be transformed. 064 * 065 * @param iterator the iterator to use 066 * @param transformer the transformer to use 067 */ 068 public TransformIterator(Iterator iterator, Transformer transformer) { 069 super(); 070 this.iterator = iterator; 071 this.transformer = transformer; 072 } 073 074 //----------------------------------------------------------------------- 075 public boolean hasNext() { 076 return iterator.hasNext(); 077 } 078 079 /** 080 * Gets the next object from the iteration, transforming it using the 081 * current transformer. If the transformer is null, no transformation 082 * occurs and the object from the iterator is returned directly. 083 * 084 * @return the next object 085 * @throws java.util.NoSuchElementException if there are no more elements 086 */ 087 public Object next() { 088 return transform(iterator.next()); 089 } 090 091 public void remove() { 092 iterator.remove(); 093 } 094 095 //----------------------------------------------------------------------- 096 /** 097 * Gets the iterator this iterator is using. 098 * 099 * @return the iterator. 100 */ 101 public Iterator getIterator() { 102 return iterator; 103 } 104 105 /** 106 * Sets the iterator for this iterator to use. 107 * If iteration has started, this effectively resets the iterator. 108 * 109 * @param iterator the iterator to use 110 */ 111 public void setIterator(Iterator iterator) { 112 this.iterator = iterator; 113 } 114 115 //----------------------------------------------------------------------- 116 /** 117 * Gets the transformer this iterator is using. 118 * 119 * @return the transformer. 120 */ 121 public Transformer getTransformer() { 122 return transformer; 123 } 124 125 /** 126 * Sets the transformer this the iterator to use. 127 * A null transformer is a no-op transformer. 128 * 129 * @param transformer the transformer to use 130 */ 131 public void setTransformer(Transformer transformer) { 132 this.transformer = transformer; 133 } 134 135 //----------------------------------------------------------------------- 136 /** 137 * Transforms the given object using the transformer. 138 * If the transformer is null, the original object is returned as-is. 139 * 140 * @param source the object to transform 141 * @return the transformed object 142 */ 143 protected Object transform(Object source) { 144 if (transformer != null) { 145 return transformer.transform(source); 146 } 147 return source; 148 } 149 }