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