EnumerationIterator.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.Collection;
  19. import java.util.Enumeration;
  20. import java.util.Iterator;

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

  29.     /** The collection to remove elements from */
  30.     private final Collection<? super E> collection;
  31.     /** The enumeration being converted */
  32.     private Enumeration<? extends E> enumeration;
  33.     /** The last object retrieved */
  34.     private E last;

  35.     // Constructors
  36.     /**
  37.      * Constructs a new {@code EnumerationIterator} that will not
  38.      * function until {@link #setEnumeration(Enumeration)} is called.
  39.      */
  40.     public EnumerationIterator() {
  41.         this(null, null);
  42.     }

  43.     /**
  44.      * Constructs a new {@code EnumerationIterator} that provides
  45.      * an iterator view of the given enumeration.
  46.      *
  47.      * @param enumeration  the enumeration to use
  48.      */
  49.     public EnumerationIterator(final Enumeration<? extends E> enumeration) {
  50.         this(enumeration, null);
  51.     }

  52.     /**
  53.      * Constructs a new {@code EnumerationIterator} that will remove
  54.      * elements from the specified collection.
  55.      *
  56.      * @param enumeration  the enumeration to use
  57.      * @param collection  the collection to remove elements from
  58.      */
  59.     public EnumerationIterator(final Enumeration<? extends E> enumeration, final Collection<? super E> collection) {
  60.         this.enumeration = enumeration;
  61.         this.collection = collection;
  62.         this.last = null;
  63.     }

  64.     /**
  65.      * Gets the underlying enumeration.
  66.      *
  67.      * @return the underlying enumeration
  68.      */
  69.     public Enumeration<? extends E> getEnumeration() {
  70.         return enumeration;
  71.     }

  72.     // Iterator interface
  73.     /**
  74.      * Returns true if the underlying enumeration has more elements.
  75.      *
  76.      * @return true if the underlying enumeration has more elements
  77.      * @throws NullPointerException  if the underlying enumeration is null
  78.      */
  79.     @Override
  80.     public boolean hasNext() {
  81.         return enumeration.hasMoreElements();
  82.     }

  83.     /**
  84.      * Returns the next object from the enumeration.
  85.      *
  86.      * @return the next object from the enumeration
  87.      * @throws NullPointerException if the enumeration is null
  88.      */
  89.     @Override
  90.     public E next() {
  91.         last = enumeration.nextElement();
  92.         return last;
  93.     }

  94.     /**
  95.      * Removes the last retrieved element if a collection is attached.
  96.      * <p>
  97.      * Functions if an associated {@code Collection} is known.
  98.      * If so, the first occurrence of the last returned object from this
  99.      * iterator will be removed from the collection.
  100.      *
  101.      * @throws IllegalStateException {@code next()} not called.
  102.      * @throws UnsupportedOperationException if no associated collection
  103.      */
  104.     @Override
  105.     public void remove() {
  106.         if (collection == null) {
  107.             throw new UnsupportedOperationException("No Collection associated with this Iterator");
  108.         }
  109.         if (last == null) {
  110.             throw new IllegalStateException("next() must have been called for remove() to function");
  111.         }
  112.         collection.remove(last);
  113.     }

  114.     /**
  115.      * Sets the underlying enumeration.
  116.      *
  117.      * @param enumeration  the new underlying enumeration
  118.      */
  119.     public void setEnumeration(final Enumeration<? extends E> enumeration) {
  120.         this.enumeration = enumeration;
  121.     }

  122. }