1 21 24 package org.lobobrowser.util; 25 26 import java.util.*; 27 28 31 public class CollectionUtilities { 32 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 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 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 |