KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > CollectionUtilities


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Jun 9, 2005
23  */

24 package org.lobobrowser.util;
25
26 import java.util.*;
27
28 /**
29  * @author J. H. S.
30  */

31 public class CollectionUtilities {
32     /**
33      *
34      */

35     private CollectionUtilities() {
36         super();
37     }
38     
39     public static Enumeration getIteratorEnumeration(final Iterator i) {
40         return new Enumeration() {
41             public boolean hasMoreElements() {
42                 return i.hasNext();
43             }
44             
45             public Object JavaDoc nextElement() {
46                 return i.next();
47             }
48         };
49     }
50
51     public static Iterator iteratorUnion(final Iterator[] iterators) {
52         return new Iterator() {
53             private int iteratorIndex = 0;
54             private Iterator current = iterators.length > 0 ? iterators[0] : null;
55             
56             public boolean hasNext() {
57                 for(;;) {
58                     if(current == null) {
59                         return false;
60                     }
61                     if(current.hasNext()) {
62                         return true;
63                     }
64                     iteratorIndex++;
65                     current = iteratorIndex >= iterators.length ? null : iterators[iteratorIndex];
66                 }
67             }
68             
69             public Object JavaDoc next() {
70                 for(;;) {
71                     if(this.current == null) {
72                         throw new NoSuchElementException();
73                     }
74                     try {
75                         return this.current.next();
76                     } catch(NoSuchElementException nse) {
77                         this.iteratorIndex++;
78                         this.current = this.iteratorIndex >= iterators.length ? null : iterators[this.iteratorIndex];
79                     }
80                 }
81             }
82             
83             public void remove() {
84                 if(this.current == null) {
85                     throw new NoSuchElementException();
86                 }
87                 this.current.remove();
88             }
89         };
90     }
91     
92     public static Collection reverse(Collection collection) {
93         LinkedList newCollection = new LinkedList();
94         Iterator i = collection.iterator();
95         while(i.hasNext()) {
96             newCollection.addFirst(i.next());
97         }
98         return newCollection;
99     }
100 }
101
Popular Tags