    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.Enumeration;
    020import java.util.Iterator;
    021
    022/**
    023 * Adapter to make an {@link Iterator Iterator} instance appear to be an
    024 * {@link Enumeration Enumeration} instance.
    025 *
    026 * @param <E> the type of elements returned by this iterator.
    027 * @since 1.0
    028 */
    029public class IteratorEnumeration<E> implements Enumeration<E> {
    030
    031    /** The iterator being decorated. */
    032    private Iterator<? extends E> iterator;
    033
    034    /**
    035     * Constructs a new {@code IteratorEnumeration} that will not function
    036     * until {@link #setIterator(Iterator) setIterator} is invoked.
    037     */
    038    public IteratorEnumeration() {
    039    }
    040
    041    /**
    042     * Constructs a new {@code IteratorEnumeration} that will use the given
    043     * iterator.
    044     *
    045     * @param iterator the iterator to use
    046     */
    047    public IteratorEnumeration(final Iterator<? extends E> iterator) {
    048        this.iterator = iterator;
    049    }
    050
    051    /**
    052     * Gets the underlying iterator.
    053     *
    054     * @return the underlying iterator
    055     */
    056    public Iterator<? extends E> getIterator() {
    057        return iterator;
    058    }
    059
    060    /**
    061     * Returns true if the underlying iterator has more elements.
    062     *
    063     * @return true if the underlying iterator has more elements
    064     */
    065    @Override
    066    public boolean hasMoreElements() {
    067        return iterator.hasNext();
    068    }
    069
    070    /**
    071     * Returns the next element from the underlying iterator.
    072     *
    073     * @return the next element from the underlying iterator.
    074     * @throws java.util.NoSuchElementException if the underlying iterator has
    075     * no more elements
    076     */
    077    @Override
    078    public E nextElement() {
    079        return iterator.next();
    080    }
    081
    082    /**
    083     * Sets the underlying iterator.
    084     *
    085     * @param iterator the new underlying iterator
    086     */
    087    public void setIterator(final Iterator<? extends E> iterator) {
    088        this.iterator = iterator;
    089    }
    090
    091}