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 19 import java.util.Collection; 20 import java.util.Enumeration; 21 import java.util.Iterator; 22 23 /** 24 * Adapter to make {@link Enumeration Enumeration} instances appear 25 * to be {@link Iterator Iterator} instances. 26 * 27 * @param <E> the type of elements returned by this iterator. 28 * @since 1.0 29 */ 30 public class EnumerationIterator<E> implements Iterator<E> { 31 32 /** The collection to remove elements from */ 33 private final Collection<? super E> collection; 34 /** The enumeration being converted */ 35 private Enumeration<? extends E> enumeration; 36 /** The last object retrieved */ 37 private E last; 38 39 // Constructors 40 /** 41 * Constructs a new {@code EnumerationIterator} that will not 42 * function until {@link #setEnumeration(Enumeration)} is called. 43 */ 44 public EnumerationIterator() { 45 this(null, null); 46 } 47 48 /** 49 * Constructs a new {@code EnumerationIterator} that provides 50 * an iterator view of the given enumeration. 51 * 52 * @param enumeration the enumeration to use 53 */ 54 public EnumerationIterator(final Enumeration<? extends E> enumeration) { 55 this(enumeration, null); 56 } 57 58 /** 59 * Constructs a new {@code EnumerationIterator} that will remove 60 * elements from the specified collection. 61 * 62 * @param enumeration the enumeration to use 63 * @param collection the collection to remove elements from 64 */ 65 public EnumerationIterator(final Enumeration<? extends E> enumeration, final Collection<? super E> collection) { 66 this.enumeration = enumeration; 67 this.collection = collection; 68 this.last = null; 69 } 70 71 // Properties 72 /** 73 * Returns the underlying enumeration. 74 * 75 * @return the underlying enumeration 76 */ 77 public Enumeration<? extends E> getEnumeration() { 78 return enumeration; 79 } 80 81 // Iterator interface 82 /** 83 * Returns true if the underlying enumeration has more elements. 84 * 85 * @return true if the underlying enumeration has more elements 86 * @throws NullPointerException if the underlying enumeration is null 87 */ 88 @Override 89 public boolean hasNext() { 90 return enumeration.hasMoreElements(); 91 } 92 93 /** 94 * Returns the next object from the enumeration. 95 * 96 * @return the next object from the enumeration 97 * @throws NullPointerException if the enumeration is null 98 */ 99 @Override 100 public E next() { 101 last = enumeration.nextElement(); 102 return last; 103 } 104 105 /** 106 * Removes the last retrieved element if a collection is attached. 107 * <p> 108 * Functions if an associated {@code Collection} is known. 109 * If so, the first occurrence of the last returned object from this 110 * iterator will be removed from the collection. 111 * 112 * @throws IllegalStateException {@code next()} not called. 113 * @throws UnsupportedOperationException if no associated collection 114 */ 115 @Override 116 public void remove() { 117 if (collection == null) { 118 throw new UnsupportedOperationException("No Collection associated with this Iterator"); 119 } 120 if (last == null) { 121 throw new IllegalStateException("next() must have been called for remove() to function"); 122 } 123 collection.remove(last); 124 } 125 126 /** 127 * Sets the underlying enumeration. 128 * 129 * @param enumeration the new underlying enumeration 130 */ 131 public void setEnumeration(final Enumeration<? extends E> enumeration) { 132 this.enumeration = enumeration; 133 } 134 135 }