001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf.util;
018:
019: import java.util.*;
020:
021: /**
022: * <p>Adapter class that wraps an <code>Enumeration</code> around a Java2
023: * collection classes object <code>Iterator</code> so that existing APIs
024: * returning Enumerations can easily run on top of the new collections.
025: * Constructors are provided to easliy create such wrappers.</p>
026: * <p>The source code is taken from Apache Tomcat</p>
027: *
028: * @author Craig R. McClanahan
029: * @author Andrey Grebnev <a href="mailto:andrey.grebnev@blandware.com"><andrey.grebnev@blandware.com></a>
030: */
031: public class Enumerator implements Enumeration {
032:
033: // ----------------------------------------------------------- Constructors
034:
035: /**
036: * Return an Enumeration over the values of the specified Collection.
037: *
038: * @param collection Collection whose values should be enumerated
039: */
040: public Enumerator(Collection collection) {
041:
042: this (collection.iterator());
043:
044: }
045:
046: /**
047: * Return an Enumeration over the values of the specified Collection.
048: *
049: * @param collection Collection whose values should be enumerated
050: * @param clone true to clone iterator
051: */
052: public Enumerator(Collection collection, boolean clone) {
053:
054: this (collection.iterator(), clone);
055:
056: }
057:
058: /**
059: * Return an Enumeration over the values returned by the
060: * specified Iterator.
061: *
062: * @param iterator Iterator to be wrapped
063: */
064: public Enumerator(Iterator iterator) {
065:
066: super ();
067: this .iterator = iterator;
068:
069: }
070:
071: /**
072: * Return an Enumeration over the values returned by the
073: * specified Iterator.
074: *
075: * @param iterator Iterator to be wrapped
076: * @param clone true to clone iterator
077: */
078: public Enumerator(Iterator iterator, boolean clone) {
079:
080: super ();
081: if (!clone) {
082: this .iterator = iterator;
083: } else {
084: List list = new ArrayList();
085: while (iterator.hasNext()) {
086: list.add(iterator.next());
087: }
088: this .iterator = list.iterator();
089: }
090:
091: }
092:
093: /**
094: * Return an Enumeration over the values of the specified Map.
095: *
096: * @param map Map whose values should be enumerated
097: */
098: public Enumerator(Map map) {
099:
100: this (map.values().iterator());
101:
102: }
103:
104: /**
105: * Return an Enumeration over the values of the specified Map.
106: *
107: * @param map Map whose values should be enumerated
108: * @param clone true to clone iterator
109: */
110: public Enumerator(Map map, boolean clone) {
111:
112: this (map.values().iterator(), clone);
113:
114: }
115:
116: // ----------------------------------------------------- Instance Variables
117:
118: /**
119: * The <code>Iterator</code> over which the <code>Enumeration</code>
120: * represented by this class actually operates.
121: */
122: private Iterator iterator = null;
123:
124: // --------------------------------------------------------- Public Methods
125:
126: /**
127: * Tests if this enumeration contains more elements.
128: *
129: * @return <code>true</code> if and only if this enumeration object
130: * contains at least one more element to provide, <code>false</code>
131: * otherwise
132: */
133: public boolean hasMoreElements() {
134:
135: return (iterator.hasNext());
136:
137: }
138:
139: /**
140: * Returns the next element of this enumeration if this enumeration
141: * has at least one more element to provide.
142: *
143: * @return the next element of this enumeration
144: *
145: * @exception NoSuchElementException if no more elements exist
146: */
147: public Object nextElement() throws NoSuchElementException {
148:
149: return (iterator.next());
150:
151: }
152:
153: }
|