KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > util > core > Enumerator


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.util.core;
17
18 import java.util.*;
19
20 /**
21  * <p>Adapter class that wraps an <code>Enumeration</code> around a Java2
22  * collection classes object <code>Iterator</code> so that existing APIs
23  * returning Enumerations can easily run on top of the new collections.
24  * Constructors are provided to easliy create such wrappers.</p>
25  * <p>The source code is taken from Apache Tomcat</p>
26  *
27  * <p><a HREF="Enumerator.java.htm"><i>View Source</i></a></p>
28  *
29  * @author Craig R. McClanahan
30  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
31  * @version $Revision: 1.1 $ $Date: 2005/07/04 12:23:59 $
32  */

33 public class Enumerator implements Enumeration {
34
35
36     // ----------------------------------------------------------- Constructors
37

38
39     /**
40      * Return an Enumeration over the values of the specified Collection.
41      *
42      * @param collection Collection whose values should be enumerated
43      */

44     public Enumerator(Collection collection) {
45
46         this(collection.iterator());
47
48     }
49
50
51     /**
52      * Return an Enumeration over the values of the specified Collection.
53      *
54      * @param collection Collection whose values should be enumerated
55      * @param clone true to clone iterator
56      */

57     public Enumerator(Collection collection, boolean clone) {
58
59         this(collection.iterator(), clone);
60
61     }
62
63
64     /**
65      * Return an Enumeration over the values returned by the
66      * specified Iterator.
67      *
68      * @param iterator Iterator to be wrapped
69      */

70     public Enumerator(Iterator iterator) {
71
72         super();
73         this.iterator = iterator;
74
75     }
76
77
78     /**
79      * Return an Enumeration over the values returned by the
80      * specified Iterator.
81      *
82      * @param iterator Iterator to be wrapped
83      * @param clone true to clone iterator
84      */

85     public Enumerator(Iterator iterator, boolean clone) {
86
87         super();
88         if (!clone) {
89             this.iterator = iterator;
90         } else {
91             List list = new ArrayList();
92             while (iterator.hasNext()) {
93                 list.add(iterator.next());
94             }
95             this.iterator = list.iterator();
96         }
97
98     }
99
100
101     /**
102      * Return an Enumeration over the values of the specified Map.
103      *
104      * @param map Map whose values should be enumerated
105      */

106     public Enumerator(Map map) {
107
108         this(map.values().iterator());
109
110     }
111
112
113     /**
114      * Return an Enumeration over the values of the specified Map.
115      *
116      * @param map Map whose values should be enumerated
117      * @param clone true to clone iterator
118      */

119     public Enumerator(Map map, boolean clone) {
120
121         this(map.values().iterator(), clone);
122
123     }
124
125
126     // ----------------------------------------------------- Instance Variables
127

128
129     /**
130      * The <code>Iterator</code> over which the <code>Enumeration</code>
131      * represented by this class actually operates.
132      */

133     private Iterator iterator = null;
134
135
136     // --------------------------------------------------------- Public Methods
137

138
139     /**
140      * Tests if this enumeration contains more elements.
141      *
142      * @return <code>true</code> if and only if this enumeration object
143      * contains at least one more element to provide, <code>false</code>
144      * otherwise
145      */

146     public boolean hasMoreElements() {
147
148         return (iterator.hasNext());
149
150     }
151
152
153     /**
154      * Returns the next element of this enumeration if this enumeration
155      * has at least one more element to provide.
156      *
157      * @return the next element of this enumeration
158      *
159      * @exception NoSuchElementException if no more elements exist
160      */

161     public Object JavaDoc nextElement() throws NoSuchElementException {
162
163         return (iterator.next());
164
165     }
166
167 }
168
Popular Tags