KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > collection > Iterators


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.collection;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.NoSuchElementException JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29
30 import org.jboss.util.Null;
31
32 /**
33  * A collection of <code>Iterator</code> and <code>Enumeration</code>
34  * utilities.
35  *
36  * @version <tt>$Revision: 1958 $</tt>
37  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
38  */

39 public final class Iterators
40 {
41    /////////////////////////////////////////////////////////////////////////
42
// Enumeration/Iterator Conversion //
43
/////////////////////////////////////////////////////////////////////////
44

45    /**
46     * An Enumeration to Iterator wrapper.
47     */

48    private static final class Enum2Iterator
49       implements Iterator JavaDoc
50    {
51       private final Enumeration JavaDoc enumeration;
52     
53       public Enum2Iterator(final Enumeration JavaDoc enumeration) {
54          this.enumeration = enumeration;
55       }
56    
57       public boolean hasNext() {
58          return enumeration.hasMoreElements();
59       }
60     
61       public Object JavaDoc next() {
62          return enumeration.nextElement();
63       }
64       
65       public void remove() {
66          throw new UnsupportedOperationException JavaDoc("Enumerations are immutable");
67       }
68    }
69
70    /**
71     * Return an Iterator wrapper for the given Enumeration
72     *
73     * @param enum Enumeration to wrap
74     * @return Enumeration wrapped as an Iterator
75     */

76    public static Iterator JavaDoc forEnumeration(final Enumeration JavaDoc enumeration) {
77       return new Enum2Iterator(enumeration);
78    }
79
80    /**
81     * An Iterator to Enumeration wrapper class.
82     */

83    private static final class Iter2Enumeration
84       implements Enumeration JavaDoc
85    {
86       private final Iterator JavaDoc iter;
87
88       public Iter2Enumeration(final Iterator JavaDoc iter) {
89          this.iter = iter;
90       }
91
92       public boolean hasMoreElements() {
93          return iter.hasNext();
94       }
95
96       public Object JavaDoc nextElement() {
97          return iter.next();
98       }
99    }
100
101    /**
102     * Return an Enumeration for the given Iterator.
103     *
104     * @param iter Iterator to wrap.
105     * @return Enumeration wrapper.
106     */

107    public static Enumeration JavaDoc toEnumeration(final Iterator JavaDoc iter) {
108       return new Iter2Enumeration(iter);
109    }
110
111
112    /////////////////////////////////////////////////////////////////////////
113
// Iterator Wrappers //
114
/////////////////////////////////////////////////////////////////////////
115

116    /**
117     * Wraps an Iterator making it immutable, by disabling calls to
118     * <code>remove()</code>
119     */

120    private static final class ImmutableIterator
121       implements Iterator JavaDoc
122    {
123       private final Iterator JavaDoc iter;
124
125       public ImmutableIterator(final Iterator JavaDoc iter) {
126          this.iter = iter;
127       }
128
129       public boolean hasNext() {
130          return iter.hasNext();
131       }
132
133       public Object JavaDoc next() {
134          return iter.next();
135       }
136
137       public void remove() {
138          throw new UnsupportedOperationException JavaDoc("iterator is immutable");
139       }
140    }
141
142    /**
143     * Make an Iterator immutable
144     *
145     * @param iter Iterator to make immutable
146     * @return Imutable iterator
147     */

148    public static Iterator JavaDoc makeImmutable(final Iterator JavaDoc iter) {
149       return new ImmutableIterator(iter);
150    }
151
152    /**
153     * Wraps an Iterator making it synchronized.
154     */

155    private static final class SyncIterator
156       implements Iterator JavaDoc
157    {
158       private final Iterator JavaDoc iter;
159
160       public SyncIterator(final Iterator JavaDoc iter) {
161          this.iter = iter;
162       }
163
164       public synchronized boolean hasNext() {
165          return iter.hasNext();
166       }
167
168       public synchronized Object JavaDoc next() {
169          return iter.next();
170       }
171
172       public synchronized void remove() {
173          iter.remove();
174       }
175    }
176
177    /**
178     * Returns a synchronized version of the given Iterator.
179     *
180     * @param iter Iterator to synchronize.
181     * @return Synchronized Iterator.
182     */

183    public static Iterator JavaDoc makeSynchronized(final Iterator JavaDoc iter) {
184       return new SyncIterator(iter);
185    }
186
187    /**
188     * Wraps an Enumeration making it synchronized.
189     */

190    private static final class SyncEnumeration
191       implements Enumeration JavaDoc
192    {
193       private final Enumeration JavaDoc enumeration;
194
195       public SyncEnumeration(final Enumeration JavaDoc enumeration) {
196          this.enumeration = enumeration;
197       }
198
199       public synchronized boolean hasMoreElements() {
200          return enumeration.hasMoreElements();
201       }
202
203       public synchronized Object JavaDoc nextElement() {
204          return enumeration.nextElement();
205       }
206    }
207
208    /**
209     * Returns a synchronized version of the given Enumeration.
210     *
211     * @param enum Enumeration to synchronize.
212     * @return Synchronized Enumeration.
213     */

214    public static Enumeration JavaDoc makeSynchronized(final Enumeration JavaDoc enumeration) {
215       return new SyncEnumeration(enumeration);
216    }
217
218
219    /////////////////////////////////////////////////////////////////////////
220
// Empty Iterator //
221
/////////////////////////////////////////////////////////////////////////
222

223    /** An empty Iterator */
224    public static final Iterator JavaDoc EMPTY_ITERATOR = new EmptyIterator();
225
226    /**
227     * An empty Iterator
228     */

229    private static final class EmptyIterator
230       implements Iterator JavaDoc
231    {
232       public boolean hasNext() {
233          return false;
234       }
235    
236       public Object JavaDoc next() {
237          throw new NoSuchElementException JavaDoc("no more elements");
238       }
239    
240       public void remove() {
241          throw new IllegalStateException JavaDoc("no more elements");
242       }
243    }
244
245
246    /////////////////////////////////////////////////////////////////////////
247
// Misc Methods //
248
/////////////////////////////////////////////////////////////////////////
249

250    /**
251     * Returns an Iterator containing the <i>union</i> of all of the elements
252     * in the given iterator array.
253     *
254     * @param iters Array of iterators.
255     * @return Iterator containing the <i>union</i>.
256     */

257    public static Iterator JavaDoc union(final Iterator JavaDoc iters[]) {
258       Map JavaDoc map = new HashMap JavaDoc();
259
260       for (int i=0; i < iters.length; i++) {
261          if (iters[i] != null) {
262             while (iters[i].hasNext()) {
263                Object JavaDoc obj = iters[i].next();
264                if (!map.containsKey(obj)) {
265                   map.put(obj, Null.VALUE);
266                }
267             }
268          }
269       }
270
271       return map.keySet().iterator();
272    }
273
274    /**
275     * Return a delimited string representation of all of the elements
276     * in the given Iterator.
277     *
278     * @param iter Iterator to convert to string.
279     * @param delim Elemement delimiter.
280     * @return Delimited string value.
281     */

282    public static String JavaDoc toString(final Iterator JavaDoc iter, final String JavaDoc delim) {
283       StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
284       while (iter.hasNext()) {
285          buff.append(iter.next());
286
287          if (iter.hasNext()) {
288             buff.append(delim);
289          }
290       }
291
292       return buff.toString();
293    }
294
295    /**
296     * Return a comma delimited string representation of all of the elements
297     * in the given Iterator.
298     *
299     * @param iter Iterator to convert to string.
300     * @return Delimited string value.
301     */

302    public static String JavaDoc toString(final Iterator JavaDoc iter) {
303       return toString(iter, ",");
304    }
305 }
306
Popular Tags