IteratorEnumeration.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.iterators;

  18. import java.util.Enumeration;
  19. import java.util.Iterator;

  20. /**
  21.  * Adapter to make an {@link Iterator Iterator} instance appear to be an
  22.  * {@link Enumeration Enumeration} instance.
  23.  *
  24.  * @param <E> the type of elements returned by this iterator.
  25.  * @since 1.0
  26.  */
  27. public class IteratorEnumeration<E> implements Enumeration<E> {

  28.     /** The iterator being decorated. */
  29.     private Iterator<? extends E> iterator;

  30.     /**
  31.      * Constructs a new {@code IteratorEnumeration} that will not function
  32.      * until {@link #setIterator(Iterator) setIterator} is invoked.
  33.      */
  34.     public IteratorEnumeration() {
  35.     }

  36.     /**
  37.      * Constructs a new {@code IteratorEnumeration} that will use the given
  38.      * iterator.
  39.      *
  40.      * @param iterator the iterator to use
  41.      */
  42.     public IteratorEnumeration(final Iterator<? extends E> iterator) {
  43.         this.iterator = iterator;
  44.     }

  45.     /**
  46.      * Gets the underlying iterator.
  47.      *
  48.      * @return the underlying iterator
  49.      */
  50.     public Iterator<? extends E> getIterator() {
  51.         return iterator;
  52.     }

  53.     /**
  54.      * Returns true if the underlying iterator has more elements.
  55.      *
  56.      * @return true if the underlying iterator has more elements
  57.      */
  58.     @Override
  59.     public boolean hasMoreElements() {
  60.         return iterator.hasNext();
  61.     }

  62.     /**
  63.      * Returns the next element from the underlying iterator.
  64.      *
  65.      * @return the next element from the underlying iterator.
  66.      * @throws java.util.NoSuchElementException if the underlying iterator has
  67.      * no more elements
  68.      */
  69.     @Override
  70.     public E nextElement() {
  71.         return iterator.next();
  72.     }

  73.     /**
  74.      * Sets the underlying iterator.
  75.      *
  76.      * @param iterator the new underlying iterator
  77.      */
  78.     public void setIterator(final Iterator<? extends E> iterator) {
  79.         this.iterator = iterator;
  80.     }

  81. }