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.collections.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 * @since 1.0
27 * @version $Id: IteratorEnumeration.java 1429905 2013-01-07 17:15:14Z ggregory $
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</code> that will not function
36 * until {@link #setIterator(Iterator) setIterator} is invoked.
37 */
38 public IteratorEnumeration() {
39 super();
40 }
41
42 /**
43 * Constructs a new <code>IteratorEnumeration</code> that will use the given
44 * iterator.
45 *
46 * @param iterator the iterator to use
47 */
48 public IteratorEnumeration(final Iterator<? extends E> iterator) {
49 super();
50 this.iterator = iterator;
51 }
52
53 // Iterator interface
54 //-------------------------------------------------------------------------
55
56 /**
57 * Returns true if the underlying iterator has more elements.
58 *
59 * @return true if the underlying iterator has more elements
60 */
61 public boolean hasMoreElements() {
62 return iterator.hasNext();
63 }
64
65 /**
66 * Returns the next element from the underlying iterator.
67 *
68 * @return the next element from the underlying iterator.
69 * @throws java.util.NoSuchElementException if the underlying iterator has
70 * no more elements
71 */
72 public E nextElement() {
73 return iterator.next();
74 }
75
76 // Properties
77 //-------------------------------------------------------------------------
78
79 /**
80 * Returns the underlying iterator.
81 *
82 * @return the underlying iterator
83 */
84 public Iterator<? extends E> getIterator() {
85 return iterator;
86 }
87
88 /**
89 * Sets the underlying iterator.
90 *
91 * @param iterator the new underlying iterator
92 */
93 public void setIterator(final Iterator<? extends E> iterator) {
94 this.iterator = iterator;
95 }
96
97 }