2
* Copyright 1999-2004 The Apache Software Foundation
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
package org.apache.commons.collections.iterators;
18
import java.util.Collection;
19
import java.util.Enumeration;
20
import java.util.Iterator;
22
/** Adapter to make {@link Enumeration Enumeration} instances appear
23
* to be {@link Iterator Iterator} instances.
26
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
27
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
29
public class EnumerationIterator implements Iterator {
31
private Collection collection;
33
private Enumeration enumeration;
38
* Constructs a new <Code>EnumerationIterator</Code> that will not
39
* function until {@link #setEnumeration(Enumeration)} is called.
41
public EnumerationIterator() {
46
* Constructs a new <Code>EnumerationIterator</Code> that provides
47
* an iterator view of the given enumeration.
49
* @param enumeration the enumeration to use
51
public EnumerationIterator( Enumeration enumeration ) {
52
this(enumeration, null);
56
* Constructs a new <Code>EnumerationIterator</Code> that will remove
57
* elements from the specified collection.
59
* @param enumeration the enumeration to use
60
* @param collection the collection to remove elements from
62
public EnumerationIterator( Enumeration enumeration, Collection collection ) {
63
this.enumeration = enumeration;
64
this.collection = collection;
69
//-------------------------------------------------------------------------
72
* Returns true if the underlying enumeration has more elements.
74
* @return true if the underlying enumeration has more elements
75
* @throws NullPointerException if the underlying enumeration is null
77
public boolean hasNext() {
78
return enumeration.hasMoreElements();
82
* Returns the next object from the enumeration.
84
* @return the next object from the enumeration
85
* @throws NullPointerException if the enumeration is null
87
public Object next() {
88
last = enumeration.nextElement();
93
* Functions if an associated <code>Collection</code> is known.
94
* If so, the first occurrence of the last returned object from this
95
* iterator will be removed from the collection.
97
* @exception IllegalStateException <code>next()</code> not called.
98
* @exception UnsupportedOperationException No associated
99
* <code>Collection</code>.
101
public void remove() {
102
if (collection != null) {
104
collection.remove(last);
107
throw new IllegalStateException
108
("next() must have been called for remove() to function");
112
throw new UnsupportedOperationException
113
("No Collection associated with this Iterator");
118
//-------------------------------------------------------------------------
121
* Returns the underlying enumeration.
123
* @return the underlying enumeration
125
public Enumeration getEnumeration() {
130
* Sets the underlying enumeration.
132
* @param enumeration the new underlying enumeration
134
public void setEnumeration( Enumeration enumeration ) {
135
this.enumeration = enumeration;