View Javadoc

1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.ui.savedrequest;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Enumeration;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.NoSuchElementException;
25  
26  
27  /**
28   * <p>Adapter that wraps an <code>Enumeration</code> around a Java 2 collection <code>Iterator</code>.</p>
29   * <p>Constructors are provided to easily create such wrappers.</p>
30   * <p>This class is based on code in Apache Tomcat.</p>
31   *
32   * @author Craig McClanahan
33   * @author Andrey Grebnev
34   * @version $Id: Enumerator.java 1784 2007-02-24 21:00:24Z luke_t $
35   */
36  public class Enumerator implements Enumeration {
37      //~ Instance fields ================================================================================================
38  
39      /**
40       * The <code>Iterator</code> over which the <code>Enumeration</code> represented by this class actually operates.
41       */
42      private Iterator iterator = null;
43  
44      //~ Constructors ===================================================================================================
45  
46  /**
47       * Return an Enumeration over the values of the specified Collection.
48       *
49       * @param collection Collection whose values should be enumerated
50       */
51      public Enumerator(Collection collection) {
52          this(collection.iterator());
53      }
54  
55  /**
56       * Return an Enumeration over the values of the specified Collection.
57       *
58       * @param collection Collection whose values should be enumerated
59       * @param clone true to clone iterator
60       */
61      public Enumerator(Collection collection, boolean clone) {
62          this(collection.iterator(), clone);
63      }
64  
65  /**
66       * Return an Enumeration over the values returned by the specified
67       * Iterator.
68       *
69       * @param iterator Iterator to be wrapped
70       */
71      public Enumerator(Iterator iterator) {
72          super();
73          this.iterator = iterator;
74      }
75  
76  /**
77       * Return an Enumeration over the values returned by the specified
78       * Iterator.
79       *
80       * @param iterator Iterator to be wrapped
81       * @param clone true to clone iterator
82       */
83      public Enumerator(Iterator iterator, boolean clone) {
84          super();
85  
86          if (!clone) {
87              this.iterator = iterator;
88          } else {
89              List list = new ArrayList();
90  
91              while (iterator.hasNext()) {
92                  list.add(iterator.next());
93              }
94  
95              this.iterator = list.iterator();
96          }
97      }
98  
99  /**
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         this(map.values().iterator());
106     }
107 
108 /**
109      * Return an Enumeration over the values of the specified Map.
110      *
111      * @param map Map whose values should be enumerated
112      * @param clone true to clone iterator
113      */
114     public Enumerator(Map map, boolean clone) {
115         this(map.values().iterator(), clone);
116     }
117 
118     //~ Methods ========================================================================================================
119 
120     /**
121      * Tests if this enumeration contains more elements.
122      *
123      * @return <code>true</code> if and only if this enumeration object contains at least one more element to provide,
124      *         <code>false</code> otherwise
125      */
126     public boolean hasMoreElements() {
127         return (iterator.hasNext());
128     }
129 
130     /**
131      * Returns the next element of this enumeration if this enumeration has at least one more element to
132      * provide.
133      *
134      * @return the next element of this enumeration
135      *
136      * @exception NoSuchElementException if no more elements exist
137      */
138     public Object nextElement() throws NoSuchElementException {
139         return (iterator.next());
140     }
141 }