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.collections.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 * @since 1.0
28 * @version $Id: EnumerationIterator.java 1366797 2012-07-29 09:29:15Z tn $
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 /**
42 * Constructs a new <code>EnumerationIterator</code> 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</code> 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</code> 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 super();
68 this.enumeration = enumeration;
69 this.collection = collection;
70 this.last = null;
71 }
72
73 // Iterator interface
74 //-----------------------------------------------------------------------
75 /**
76 * Returns true if the underlying enumeration has more elements.
77 *
78 * @return true if the underlying enumeration has more elements
79 * @throws NullPointerException if the underlying enumeration is null
80 */
81 public boolean hasNext() {
82 return enumeration.hasMoreElements();
83 }
84
85 /**
86 * Returns the next object from the enumeration.
87 *
88 * @return the next object from the enumeration
89 * @throws NullPointerException if the enumeration is null
90 */
91 public E next() {
92 last = enumeration.nextElement();
93 return last;
94 }
95
96 /**
97 * Removes the last retrieved element if a collection is attached.
98 * <p>
99 * Functions if an associated <code>Collection</code> is known.
100 * If so, the first occurrence of the last returned object from this
101 * iterator will be removed from the collection.
102 *
103 * @exception IllegalStateException <code>next()</code> not called.
104 * @exception UnsupportedOperationException if no associated collection
105 */
106 public void remove() {
107 if (collection != null) {
108 if (last != null) {
109 collection.remove(last);
110 } else {
111 throw new IllegalStateException("next() must have been called for remove() to function");
112 }
113 } else {
114 throw new UnsupportedOperationException("No Collection associated with this Iterator");
115 }
116 }
117
118 // Properties
119 //-----------------------------------------------------------------------
120 /**
121 * Returns the underlying enumeration.
122 *
123 * @return the underlying enumeration
124 */
125 public Enumeration<? extends E> getEnumeration() {
126 return enumeration;
127 }
128
129 /**
130 * Sets the underlying enumeration.
131 *
132 * @param enumeration the new underlying enumeration
133 */
134 public void setEnumeration(final Enumeration<? extends E> enumeration) {
135 this.enumeration = enumeration;
136 }
137
138 }