01: /*
02: * Copyright 1999-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.commons.collections.iterators;
17:
18: import java.util.Enumeration;
19: import java.util.Iterator;
20:
21: /**
22: * Adapter to make an {@link Iterator Iterator} instance appear to be
23: * an {@link Enumeration Enumeration} instance.
24: *
25: * @since Commons Collections 1.0
26: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
27: *
28: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
29: */
30: public class IteratorEnumeration implements Enumeration {
31:
32: /** The iterator being decorated. */
33: private Iterator iterator;
34:
35: /**
36: * Constructs a new <code>IteratorEnumeration</code> that will not
37: * function until {@link #setIterator(Iterator) setIterator} is
38: * invoked.
39: */
40: public IteratorEnumeration() {
41: super ();
42: }
43:
44: /**
45: * Constructs a new <code>IteratorEnumeration</code> that will use
46: * the given iterator.
47: *
48: * @param iterator the iterator to use
49: */
50: public IteratorEnumeration(Iterator iterator) {
51: super ();
52: this .iterator = iterator;
53: }
54:
55: // Iterator interface
56: //-------------------------------------------------------------------------
57:
58: /**
59: * Returns true if the underlying iterator has more elements.
60: *
61: * @return true if the underlying iterator has more elements
62: */
63: public boolean hasMoreElements() {
64: return iterator.hasNext();
65: }
66:
67: /**
68: * Returns the next element from the underlying iterator.
69: *
70: * @return the next element from the underlying iterator.
71: * @throws java.util.NoSuchElementException if the underlying iterator has no
72: * more elements
73: */
74: public Object nextElement() {
75: return iterator.next();
76: }
77:
78: // Properties
79: //-------------------------------------------------------------------------
80:
81: /**
82: * Returns the underlying iterator.
83: *
84: * @return the underlying iterator
85: */
86: public Iterator getIterator() {
87: return iterator;
88: }
89:
90: /**
91: * Sets the underlying iterator.
92: *
93: * @param iterator the new underlying iterator
94: */
95: public void setIterator(Iterator iterator) {
96: this.iterator = iterator;
97: }
98:
99: }
|