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.Collection;
  18. import java.util.Enumeration;
  19. import java.util.Iterator;
  20. /**
  21. * Adapter to make {@link Enumeration Enumeration} instances appear
  22. * to be {@link Iterator Iterator} instances.
  23. *
  24. * @since Commons Collections 1.0
  25. * @version $Revision: 1.8 $ $Date: 2004/04/21 20:34:12 $
  26. *
  27. * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  28. * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
  29. */
  30. public class EnumerationIterator implements Iterator {
  31. /** The collection to remove elements from */
  32. private Collection collection;
  33. /** The enumeration being converted */
  34. private Enumeration enumeration;
  35. /** The last object retrieved */
  36. private Object last;
  37. // Constructors
  38. //-----------------------------------------------------------------------
  39. /**
  40. * Constructs a new <code>EnumerationIterator</code> that will not
  41. * function until {@link #setEnumeration(Enumeration)} is called.
  42. */
  43. public EnumerationIterator() {
  44. this(null, null);
  45. }
  46. /**
  47. * Constructs a new <code>EnumerationIterator</code> that provides
  48. * an iterator view of the given enumeration.
  49. *
  50. * @param enumeration the enumeration to use
  51. */
  52. public EnumerationIterator(final Enumeration enumeration) {
  53. this(enumeration, null);
  54. }
  55. /**
  56. * Constructs a new <code>EnumerationIterator</code> that will remove
  57. * elements from the specified collection.
  58. *
  59. * @param enumeration the enumeration to use
  60. * @param collection the collection to remove elements form
  61. */
  62. public EnumerationIterator(final Enumeration enumeration, final Collection collection) {
  63. super();
  64. this.enumeration = enumeration;
  65. this.collection = collection;
  66. this.last = null;
  67. }
  68. // Iterator interface
  69. //-----------------------------------------------------------------------
  70. /**
  71. * Returns true if the underlying enumeration has more elements.
  72. *
  73. * @return true if the underlying enumeration has more elements
  74. * @throws NullPointerException if the underlying enumeration is null
  75. */
  76. public boolean hasNext() {
  77. return enumeration.hasMoreElements();
  78. }
  79. /**
  80. * Returns the next object from the enumeration.
  81. *
  82. * @return the next object from the enumeration
  83. * @throws NullPointerException if the enumeration is null
  84. */
  85. public Object next() {
  86. last = enumeration.nextElement();
  87. return last;
  88. }
  89. /**
  90. * Removes the last retrieved element if a collection is attached.
  91. * <p>
  92. * Functions if an associated <code>Collection</code> is known.
  93. * If so, the first occurrence of the last returned object from this
  94. * iterator will be removed from the collection.
  95. *
  96. * @exception IllegalStateException <code>next()</code> not called.
  97. * @exception UnsupportedOperationException if no associated collection
  98. */
  99. public void remove() {
  100. if (collection != null) {
  101. if (last != null) {
  102. collection.remove(last);
  103. } else {
  104. throw new IllegalStateException("next() must have been called for remove() to function");
  105. }
  106. } else {
  107. throw new UnsupportedOperationException("No Collection associated with this Iterator");
  108. }
  109. }
  110. // Properties
  111. //-----------------------------------------------------------------------
  112. /**
  113. * Returns the underlying enumeration.
  114. *
  115. * @return the underlying enumeration
  116. */
  117. public Enumeration getEnumeration() {
  118. return enumeration;
  119. }
  120. /**
  121. * Sets the underlying enumeration.
  122. *
  123. * @param enumeration the new underlying enumeration
  124. */
  125. public void setEnumeration(final Enumeration enumeration) {
  126. this.enumeration = enumeration;
  127. }
  128. }