    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 *      https://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
    035    /** The enumeration being converted */
    036    private Enumeration<? extends E> enumeration;
    037
    038    /** The last object retrieved */
    039    private E last;
    040
    041    /**
    042     * Constructs a new {@code EnumerationIterator} that will not
    043     * function until {@link #setEnumeration(Enumeration)} is called.
    044     */
    045    public EnumerationIterator() {
    046        this(null, null);
    047    }
    048
    049    /**
    050     * Constructs a new {@code EnumerationIterator} that provides
    051     * an iterator view of the given enumeration.
    052     *
    053     * @param enumeration  The enumeration to use
    054     */
    055    public EnumerationIterator(final Enumeration<? extends E> enumeration) {
    056        this(enumeration, null);
    057    }
    058
    059    /**
    060     * Constructs a new {@code EnumerationIterator} that will remove
    061     * elements from the specified collection.
    062     *
    063     * @param enumeration  The enumeration to use
    064     * @param collection  The collection to remove elements from
    065     */
    066    public EnumerationIterator(final Enumeration<? extends E> enumeration, final Collection<? super E> collection) {
    067        this.enumeration = enumeration;
    068        this.collection = collection;
    069        this.last = null;
    070    }
    071
    072    /**
    073     * Gets the underlying enumeration.
    074     *
    075     * @return The underlying enumeration
    076     */
    077    public Enumeration<? extends E> getEnumeration() {
    078        return enumeration;
    079    }
    080
    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}