View Javadoc
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.Enumeration;
20  import java.util.Iterator;
21  
22  /**
23   * Adapter to make an {@link Iterator Iterator} instance appear to be an
24   * {@link Enumeration Enumeration} instance.
25   *
26   * @param <E> the type of elements returned by this iterator.
27   * @since 1.0
28   */
29  public class IteratorEnumeration<E> implements Enumeration<E> {
30  
31      /** The iterator being decorated. */
32      private Iterator<? extends E> iterator;
33  
34      /**
35       * Constructs a new {@code IteratorEnumeration} that will not function
36       * until {@link #setIterator(Iterator) setIterator} is invoked.
37       */
38      public IteratorEnumeration() {
39      }
40  
41      /**
42       * Constructs a new {@code IteratorEnumeration} that will use the given
43       * iterator.
44       *
45       * @param iterator the iterator to use
46       */
47      public IteratorEnumeration(final Iterator<? extends E> iterator) {
48          this.iterator = iterator;
49      }
50  
51      /**
52       * Returns the underlying iterator.
53       *
54       * @return the underlying iterator
55       */
56      public Iterator<? extends E> getIterator() {
57          return iterator;
58      }
59  
60      /**
61       * Returns true if the underlying iterator has more elements.
62       *
63       * @return true if the underlying iterator has more elements
64       */
65      @Override
66      public boolean hasMoreElements() {
67          return iterator.hasNext();
68      }
69  
70      /**
71       * Returns the next element from the underlying iterator.
72       *
73       * @return the next element from the underlying iterator.
74       * @throws java.util.NoSuchElementException if the underlying iterator has
75       * no more elements
76       */
77      @Override
78      public E nextElement() {
79          return iterator.next();
80      }
81  
82      /**
83       * Sets the underlying iterator.
84       *
85       * @param iterator the new underlying iterator
86       */
87      public void setIterator(final Iterator<? extends E> iterator) {
88          this.iterator = iterator;
89      }
90  
91  }