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 * @since 1.0
027 * @version $Id: IteratorEnumeration.html 972421 2015-11-14 20:00:04Z tn $
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</code> 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</code> 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    // Iterator interface
052    //-------------------------------------------------------------------------
053
054    /**
055     * Returns true if the underlying iterator has more elements.
056     *
057     * @return true if the underlying iterator has more elements
058     */
059    public boolean hasMoreElements() {
060        return iterator.hasNext();
061    }
062
063    /**
064     * Returns the next element from the underlying iterator.
065     *
066     * @return the next element from the underlying iterator.
067     * @throws java.util.NoSuchElementException if the underlying iterator has
068     * no more elements
069     */
070    public E nextElement() {
071        return iterator.next();
072    }
073
074    // Properties
075    //-------------------------------------------------------------------------
076
077    /**
078     * Returns the underlying iterator.
079     *
080     * @return the underlying iterator
081     */
082    public Iterator<? extends E> getIterator() {
083        return iterator;
084    }
085
086    /**
087     * Sets the underlying iterator.
088     *
089     * @param iterator the new underlying iterator
090     */
091    public void setIterator(final Iterator<? extends E> iterator) {
092        this.iterator = iterator;
093    }
094
095}