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