001: /*
002: * Copyright 1999,2004 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 org.apache.catalina.util;
018:
019: import java.util.Collection;
020: import java.util.Enumeration;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.ArrayList;
024: import java.util.Map;
025: import java.util.NoSuchElementException;
026:
027: /**
028: * Adapter class that wraps an <code>Enumeration</code> around a Java2
029: * collection classes object <code>Iterator</code> so that existing APIs
030: * returning Enumerations can easily run on top of the new collections.
031: * Constructors are provided to easliy create such wrappers.
032: *
033: * @author Craig R. McClanahan
034: * @version $Revision: 1.4 $ $Date: 2004/02/27 14:58:50 $
035: */
036:
037: public final class Enumerator implements Enumeration {
038:
039: // ----------------------------------------------------------- Constructors
040:
041: /**
042: * Return an Enumeration over the values of the specified Collection.
043: *
044: * @param collection Collection whose values should be enumerated
045: */
046: public Enumerator(Collection collection) {
047:
048: this (collection.iterator());
049:
050: }
051:
052: /**
053: * Return an Enumeration over the values of the specified Collection.
054: *
055: * @param collection Collection whose values should be enumerated
056: * @param clone true to clone iterator
057: */
058: public Enumerator(Collection collection, boolean clone) {
059:
060: this (collection.iterator(), clone);
061:
062: }
063:
064: /**
065: * Return an Enumeration over the values returned by the
066: * specified Iterator.
067: *
068: * @param iterator Iterator to be wrapped
069: */
070: public Enumerator(Iterator iterator) {
071:
072: super ();
073: this .iterator = iterator;
074:
075: }
076:
077: /**
078: * Return an Enumeration over the values returned by the
079: * specified Iterator.
080: *
081: * @param iterator Iterator to be wrapped
082: * @param clone true to clone iterator
083: */
084: public Enumerator(Iterator iterator, boolean clone) {
085:
086: super ();
087: if (!clone) {
088: this .iterator = iterator;
089: } else {
090: List list = new ArrayList();
091: while (iterator.hasNext()) {
092: list.add(iterator.next());
093: }
094: this .iterator = list.iterator();
095: }
096:
097: }
098:
099: /**
100: * Return an Enumeration over the values of the specified Map.
101: *
102: * @param map Map whose values should be enumerated
103: */
104: public Enumerator(Map map) {
105:
106: this (map.values().iterator());
107:
108: }
109:
110: /**
111: * Return an Enumeration over the values of the specified Map.
112: *
113: * @param map Map whose values should be enumerated
114: * @param clone true to clone iterator
115: */
116: public Enumerator(Map map, boolean clone) {
117:
118: this (map.values().iterator(), clone);
119:
120: }
121:
122: // ----------------------------------------------------- Instance Variables
123:
124: /**
125: * The <code>Iterator</code> over which the <code>Enumeration</code>
126: * represented by this class actually operates.
127: */
128: private Iterator iterator = null;
129:
130: // --------------------------------------------------------- Public Methods
131:
132: /**
133: * Tests if this enumeration contains more elements.
134: *
135: * @return <code>true</code> if and only if this enumeration object
136: * contains at least one more element to provide, <code>false</code>
137: * otherwise
138: */
139: public boolean hasMoreElements() {
140:
141: return (iterator.hasNext());
142:
143: }
144:
145: /**
146: * Returns the next element of this enumeration if this enumeration
147: * has at least one more element to provide.
148: *
149: * @return the next element of this enumeration
150: *
151: * @exception NoSuchElementException if no more elements exist
152: */
153: public Object nextElement() throws NoSuchElementException {
154:
155: return (iterator.next());
156:
157: }
158:
159: }
|