View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.iterators;
18  
19  import java.util.Collection;
20  import java.util.Enumeration;
21  import java.util.Iterator;
22  
23  /**
24   * Adapter to make {@link Enumeration Enumeration} instances appear
25   * to be {@link Iterator Iterator} instances.
26   *
27   * @param <E> The type of elements returned by this iterator.
28   * @since 1.0
29   */
30  public class EnumerationIterator<E> implements Iterator<E> {
31  
32      /** The collection to remove elements from */
33      private final Collection<? super E> collection;
34  
35      /** The enumeration being converted */
36      private Enumeration<? extends E> enumeration;
37  
38      /** The last object retrieved */
39      private E last;
40  
41      /**
42       * Constructs a new {@code EnumerationIterator} that will not
43       * function until {@link #setEnumeration(Enumeration)} is called.
44       */
45      public EnumerationIterator() {
46          this(null, null);
47      }
48  
49      /**
50       * Constructs a new {@code EnumerationIterator} that provides
51       * an iterator view of the given enumeration.
52       *
53       * @param enumeration  The enumeration to use
54       */
55      public EnumerationIterator(final Enumeration<? extends E> enumeration) {
56          this(enumeration, null);
57      }
58  
59      /**
60       * Constructs a new {@code EnumerationIterator} that will remove
61       * elements from the specified collection.
62       *
63       * @param enumeration  The enumeration to use
64       * @param collection  The collection to remove elements from
65       */
66      public EnumerationIterator(final Enumeration<? extends E> enumeration, final Collection<? super E> collection) {
67          this.enumeration = enumeration;
68          this.collection = collection;
69          this.last = null;
70      }
71  
72      /**
73       * Gets the underlying enumeration.
74       *
75       * @return The underlying enumeration
76       */
77      public Enumeration<? extends E> getEnumeration() {
78          return enumeration;
79      }
80  
81      /**
82       * Returns true if the underlying enumeration has more elements.
83       *
84       * @return true if the underlying enumeration has more elements
85       * @throws NullPointerException  if the underlying enumeration is null
86       */
87      @Override
88      public boolean hasNext() {
89          return enumeration.hasMoreElements();
90      }
91  
92      /**
93       * Returns the next object from the enumeration.
94       *
95       * @return The next object from the enumeration
96       * @throws NullPointerException if the enumeration is null
97       */
98      @Override
99      public E next() {
100         last = enumeration.nextElement();
101         return last;
102     }
103 
104     /**
105      * Removes the last retrieved element if a collection is attached.
106      * <p>
107      * Functions if an associated {@code Collection} is known.
108      * If so, the first occurrence of the last returned object from this
109      * iterator will be removed from the collection.
110      *
111      * @throws IllegalStateException {@code next()} not called.
112      * @throws UnsupportedOperationException if no associated collection
113      */
114     @Override
115     public void remove() {
116         if (collection == null) {
117             throw new UnsupportedOperationException("No Collection associated with this Iterator");
118         }
119         if (last == null) {
120             throw new IllegalStateException("next() must have been called for remove() to function");
121         }
122         collection.remove(last);
123     }
124 
125     /**
126      * Sets the underlying enumeration.
127      *
128      * @param enumeration  The new underlying enumeration
129      */
130     public void setEnumeration(final Enumeration<? extends E> enumeration) {
131         this.enumeration = enumeration;
132     }
133 
134 }