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     */
017    package org.apache.commons.collections.iterators;
018    
019    import java.util.Enumeration;
020    import java.util.Iterator;
021    
022    /** 
023     * Adapter to make an {@link Iterator Iterator} instance appear to be
024     * an {@link Enumeration Enumeration} instance.
025     *
026     * @since Commons Collections 1.0
027     * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
028     * 
029     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
030     */
031    public class IteratorEnumeration implements Enumeration {
032        
033        /** The iterator being decorated. */
034        private Iterator iterator;
035        
036        /**
037         * Constructs a new <code>IteratorEnumeration</code> that will not 
038         * function until {@link #setIterator(Iterator) setIterator} is  
039         * invoked.
040         */
041        public IteratorEnumeration() {
042            super();
043        }
044    
045        /**
046         * Constructs a new <code>IteratorEnumeration</code> that will use
047         * the given iterator. 
048         * 
049         * @param iterator  the iterator to use
050         */
051        public IteratorEnumeration( Iterator iterator ) {
052            super();
053            this.iterator = iterator;
054        }
055    
056        // Iterator interface
057        //-------------------------------------------------------------------------
058    
059        /**
060         *  Returns true if the underlying iterator has more elements.
061         *
062         *  @return true if the underlying iterator has more elements
063         */
064        public boolean hasMoreElements() {
065            return iterator.hasNext();
066        }
067    
068        /**
069         *  Returns the next element from the underlying iterator.
070         *
071         *  @return the next element from the underlying iterator.
072         *  @throws java.util.NoSuchElementException  if the underlying iterator has no
073         *    more elements
074         */
075        public Object nextElement() {
076            return iterator.next();
077        }
078    
079        // Properties
080        //-------------------------------------------------------------------------
081    
082        /**
083         *  Returns the underlying iterator.
084         * 
085         *  @return the underlying iterator
086         */
087        public Iterator getIterator() {
088            return iterator;
089        }
090    
091        /**
092         *  Sets the underlying iterator.
093         *
094         *  @param iterator  the new underlying iterator
095         */
096        public void setIterator( Iterator iterator ) {
097            this.iterator = iterator;
098        }
099        
100    }