    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    // Properties
    072    /**
    073     * Returns the underlying enumeration.
    074     *
    075     * @return the underlying enumeration
    076     */
    077    public Enumeration<? extends E> getEnumeration() {
    078        return enumeration;
    079    }
    080
    081    // Iterator interface
    082    /**
    083     * Returns true if the underlying enumeration has more elements.
    084     *
    085     * @return true if the underlying enumeration has more elements
    086     * @throws NullPointerException  if the underlying enumeration is null
    087     */
    088    @Override
    089    public boolean hasNext() {
    090        return enumeration.hasMoreElements();
    091    }
    092
    093    /**
    094     * Returns the next object from the enumeration.
    095     *
    096     * @return the next object from the enumeration
    097     * @throws NullPointerException if the enumeration is null
    098     */
    099    @Override
    100    public E next() {
    101        last = enumeration.nextElement();
    102        return last;
    103    }
    104
    105    /**
    106     * Removes the last retrieved element if a collection is attached.
    107     * <p>
    108     * Functions if an associated {@code Collection} is known.
    109     * If so, the first occurrence of the last returned object from this
    110     * iterator will be removed from the collection.
    111     *
    112     * @throws IllegalStateException {@code next()} not called.
    113     * @throws UnsupportedOperationException if no associated collection
    114     */
    115    @Override
    116    public void remove() {
    117        if (collection == null) {
    118            throw new UnsupportedOperationException("No Collection associated with this Iterator");
    119        }
    120        if (last == null) {
    121            throw new IllegalStateException("next() must have been called for remove() to function");
    122        }
    123        collection.remove(last);
    124    }
    125
    126    /**
    127     * Sets the underlying enumeration.
    128     *
    129     * @param enumeration  the new underlying enumeration
    130     */
    131    public void setEnumeration(final Enumeration<? extends E> enumeration) {
    132        this.enumeration = enumeration;
    133    }
    134
    135}