KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > util > Enumerator


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

17
18
19 package org.apache.jasper.util;
20
21
22 import java.util.Collection JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29
30
31 /**
32  * Adapter class that wraps an <code>Enumeration</code> around a Java2
33  * collection classes object <code>Iterator</code> so that existing APIs
34  * returning Enumerations can easily run on top of the new collections.
35  * Constructors are provided to easliy create such wrappers.
36  *
37  * @author Craig R. McClanahan
38  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
39  */

40
41 public final class Enumerator implements Enumeration JavaDoc {
42
43
44     // ----------------------------------------------------------- Constructors
45

46
47     /**
48      * Return an Enumeration over the values of the specified Collection.
49      *
50      * @param collection Collection whose values should be enumerated
51      */

52     public Enumerator(Collection JavaDoc collection) {
53
54         this(collection.iterator());
55
56     }
57
58
59     /**
60      * Return an Enumeration over the values of the specified Collection.
61      *
62      * @param collection Collection whose values should be enumerated
63      * @param clone true to clone iterator
64      */

65     public Enumerator(Collection JavaDoc collection, boolean clone) {
66
67         this(collection.iterator(), clone);
68
69     }
70
71
72     /**
73      * Return an Enumeration over the values returned by the
74      * specified Iterator.
75      *
76      * @param iterator Iterator to be wrapped
77      */

78     public Enumerator(Iterator JavaDoc iterator) {
79
80         super();
81         this.iterator = iterator;
82
83     }
84
85
86     /**
87      * Return an Enumeration over the values returned by the
88      * specified Iterator.
89      *
90      * @param iterator Iterator to be wrapped
91      * @param clone true to clone iterator
92      */

93     public Enumerator(Iterator JavaDoc iterator, boolean clone) {
94
95         super();
96         if (!clone) {
97             this.iterator = iterator;
98         } else {
99             List JavaDoc list = new ArrayList JavaDoc();
100             while (iterator.hasNext()) {
101                 list.add(iterator.next());
102             }
103             this.iterator = list.iterator();
104         }
105
106     }
107
108
109     /**
110      * Return an Enumeration over the values of the specified Map.
111      *
112      * @param map Map whose values should be enumerated
113      */

114     public Enumerator(Map JavaDoc map) {
115
116         this(map.values().iterator());
117
118     }
119
120
121     /**
122      * Return an Enumeration over the values of the specified Map.
123      *
124      * @param map Map whose values should be enumerated
125      * @param clone true to clone iterator
126      */

127     public Enumerator(Map JavaDoc map, boolean clone) {
128
129         this(map.values().iterator(), clone);
130
131     }
132
133
134     // ----------------------------------------------------- Instance Variables
135

136
137     /**
138      * The <code>Iterator</code> over which the <code>Enumeration</code>
139      * represented by this class actually operates.
140      */

141     private Iterator JavaDoc iterator = null;
142
143
144     // --------------------------------------------------------- Public Methods
145

146
147     /**
148      * Tests if this enumeration contains more elements.
149      *
150      * @return <code>true</code> if and only if this enumeration object
151      * contains at least one more element to provide, <code>false</code>
152      * otherwise
153      */

154     public boolean hasMoreElements() {
155
156         return (iterator.hasNext());
157
158     }
159
160
161     /**
162      * Returns the next element of this enumeration if this enumeration
163      * has at least one more element to provide.
164      *
165      * @return the next element of this enumeration
166      *
167      * @exception NoSuchElementException if no more elements exist
168      */

169     public Object JavaDoc nextElement() throws NoSuchElementException JavaDoc {
170
171         return (iterator.next());
172
173     }
174
175
176 }
177
Popular Tags