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