1 /*
2 * $Id: IteratorEnumeration.java 358692 2005-12-23 03:37:26Z niallp $
3 * $Revision: 358692 $
4 * $Date: 2005-12-23 03:37:26 +0000 (Fri, 23 Dec 2005) $
5 *
6 * ====================================================================
7 *
8 * Copyright 2005 The Apache Software Foundation
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 */
23
24 package org.apache.commons.resources.util;
25
26 import java.util.Enumeration;
27 import java.util.Iterator;
28
29 /**
30 * Two way Adaptor class for java.util.Iterator and java.util.Enumeration.
31 */
32 public class IteratorEnumeration implements Iterator, Enumeration {
33
34 private Iterator iterator;
35 private Enumeration enumeration;
36
37 // -------------------------- Constructors
38
39 /**
40 * Construct an instance which wraps an Iterator.
41 *
42 * @param iterator The <code>Iterator</code> to wrap.
43 */
44 public IteratorEnumeration(Iterator iterator) {
45 this.iterator = iterator;
46 }
47
48 /**
49 * Construct an instance which wraps an Enumeration.
50 *
51 * @param enumeration The <code>Enumeration</code> to wrap.
52 */
53 public IteratorEnumeration(Enumeration enumeration) {
54 this.enumeration = enumeration;
55 }
56
57 // -------------------------- Iterator Methods
58
59 /**
60 * <code>remove()<code> is not supported.
61 * @throws UnsupportedOperationException always.
62 */
63 public void remove() {
64 throw new UnsupportedOperationException();
65 }
66
67 /**
68 * Indicates whether the wrapped
69 * <code>Iterator/Enumeration</code> has more elements.
70 *
71 * @return <code>true</code> if the wrapped
72 * <code>Iterator/Enumeration</code> has more elements.
73 */
74 public boolean hasNext() {
75 if (iterator == null) {
76 return enumeration.hasMoreElements();
77 } else {
78 return iterator.hasNext();
79 }
80 }
81
82 /**
83 * Returns the next element in the wrapped
84 * <code>Iterator/Enumeration</code>.
85 *
86 * @return the next element.
87 */
88 public Object next() {
89 if (iterator == null) {
90 return enumeration.nextElement();
91 } else {
92 return iterator.next();
93 }
94 }
95
96 // -------------------------- Enumeration Methods
97
98 /**
99 * Indicates whether the wrapped
100 * <code>Iterator/Enumeration</code> has more elements.
101 *
102 * @return <code>true</code> if the wrapped
103 * <code>Iterator/Enumeration</code> has more elements.
104 */
105 public boolean hasMoreElements() {
106 return hasNext();
107 }
108
109 /**
110 * Returns the next element in the wrapped
111 * <code>Iterator/Enumeration</code>.
112 *
113 * @return the next element.
114 */
115 public Object nextElement() {
116 return next();
117 }
118
119 }