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    *      http://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      /** The enumeration being converted */
35      private Enumeration<? extends E> enumeration;
36      /** The last object retrieved */
37      private E last;
38  
39      // Constructors
40      /**
41       * Constructs a new {@code EnumerationIterator} that will not
42       * function until {@link #setEnumeration(Enumeration)} is called.
43       */
44      public EnumerationIterator() {
45          this(null, null);
46      }
47  
48      /**
49       * Constructs a new {@code EnumerationIterator} that provides
50       * an iterator view of the given enumeration.
51       *
52       * @param enumeration  the enumeration to use
53       */
54      public EnumerationIterator(final Enumeration<? extends E> enumeration) {
55          this(enumeration, null);
56      }
57  
58      /**
59       * Constructs a new {@code EnumerationIterator} that will remove
60       * elements from the specified collection.
61       *
62       * @param enumeration  the enumeration to use
63       * @param collection  the collection to remove elements from
64       */
65      public EnumerationIterator(final Enumeration<? extends E> enumeration, final Collection<? super E> collection) {
66          this.enumeration = enumeration;
67          this.collection = collection;
68          this.last = null;
69      }
70  
71      /**
72       * Gets the underlying enumeration.
73       *
74       * @return the underlying enumeration
75       */
76      public Enumeration<? extends E> getEnumeration() {
77          return enumeration;
78      }
79  
80      // Iterator interface
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 }