001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.iterators;
018
019import java.util.Collection;
020import java.util.Enumeration;
021import java.util.Iterator;
022
023/**
024 * Adapter to make {@link Enumeration Enumeration} instances appear
025 * to be {@link Iterator Iterator} instances.
026 *
027 * @param <E> the type of elements returned by this iterator.
028 * @since 1.0
029 */
030public class EnumerationIterator<E> implements Iterator<E> {
031
032    /** The collection to remove elements from */
033    private final Collection<? super E> collection;
034    /** The enumeration being converted */
035    private Enumeration<? extends E> enumeration;
036    /** The last object retrieved */
037    private E last;
038
039    // Constructors
040    /**
041     * Constructs a new {@code EnumerationIterator} that will not
042     * function until {@link #setEnumeration(Enumeration)} is called.
043     */
044    public EnumerationIterator() {
045        this(null, null);
046    }
047
048    /**
049     * Constructs a new {@code EnumerationIterator} that provides
050     * an iterator view of the given enumeration.
051     *
052     * @param enumeration  the enumeration to use
053     */
054    public EnumerationIterator(final Enumeration<? extends E> enumeration) {
055        this(enumeration, null);
056    }
057
058    /**
059     * Constructs a new {@code EnumerationIterator} that will remove
060     * elements from the specified collection.
061     *
062     * @param enumeration  the enumeration to use
063     * @param collection  the collection to remove elements from
064     */
065    public EnumerationIterator(final Enumeration<? extends E> enumeration, final Collection<? super E> collection) {
066        this.enumeration = enumeration;
067        this.collection = collection;
068        this.last = null;
069    }
070
071    /**
072     * Gets the underlying enumeration.
073     *
074     * @return the underlying enumeration
075     */
076    public Enumeration<? extends E> getEnumeration() {
077        return enumeration;
078    }
079
080    // Iterator interface
081    /**
082     * Returns true if the underlying enumeration has more elements.
083     *
084     * @return true if the underlying enumeration has more elements
085     * @throws NullPointerException  if the underlying enumeration is null
086     */
087    @Override
088    public boolean hasNext() {
089        return enumeration.hasMoreElements();
090    }
091
092    /**
093     * Returns the next object from the enumeration.
094     *
095     * @return the next object from the enumeration
096     * @throws NullPointerException if the enumeration is null
097     */
098    @Override
099    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}