1 22 package org.jboss.util.collection; 23 24 import java.util.Iterator ; 25 import java.util.Enumeration ; 26 import java.util.NoSuchElementException ; 27 import java.util.Map ; 28 import java.util.HashMap ; 29 30 import org.jboss.util.Null; 31 32 39 public final class Iterators 40 { 41 45 48 private static final class Enum2Iterator 49 implements Iterator 50 { 51 private final Enumeration enumeration; 52 53 public Enum2Iterator(final Enumeration enumeration) { 54 this.enumeration = enumeration; 55 } 56 57 public boolean hasNext() { 58 return enumeration.hasMoreElements(); 59 } 60 61 public Object next() { 62 return enumeration.nextElement(); 63 } 64 65 public void remove() { 66 throw new UnsupportedOperationException ("Enumerations are immutable"); 67 } 68 } 69 70 76 public static Iterator forEnumeration(final Enumeration enumeration) { 77 return new Enum2Iterator(enumeration); 78 } 79 80 83 private static final class Iter2Enumeration 84 implements Enumeration 85 { 86 private final Iterator iter; 87 88 public Iter2Enumeration(final Iterator iter) { 89 this.iter = iter; 90 } 91 92 public boolean hasMoreElements() { 93 return iter.hasNext(); 94 } 95 96 public Object nextElement() { 97 return iter.next(); 98 } 99 } 100 101 107 public static Enumeration toEnumeration(final Iterator iter) { 108 return new Iter2Enumeration(iter); 109 } 110 111 112 116 120 private static final class ImmutableIterator 121 implements Iterator 122 { 123 private final Iterator iter; 124 125 public ImmutableIterator(final Iterator iter) { 126 this.iter = iter; 127 } 128 129 public boolean hasNext() { 130 return iter.hasNext(); 131 } 132 133 public Object next() { 134 return iter.next(); 135 } 136 137 public void remove() { 138 throw new UnsupportedOperationException ("iterator is immutable"); 139 } 140 } 141 142 148 public static Iterator makeImmutable(final Iterator iter) { 149 return new ImmutableIterator(iter); 150 } 151 152 155 private static final class SyncIterator 156 implements Iterator 157 { 158 private final Iterator iter; 159 160 public SyncIterator(final Iterator iter) { 161 this.iter = iter; 162 } 163 164 public synchronized boolean hasNext() { 165 return iter.hasNext(); 166 } 167 168 public synchronized Object next() { 169 return iter.next(); 170 } 171 172 public synchronized void remove() { 173 iter.remove(); 174 } 175 } 176 177 183 public static Iterator makeSynchronized(final Iterator iter) { 184 return new SyncIterator(iter); 185 } 186 187 190 private static final class SyncEnumeration 191 implements Enumeration 192 { 193 private final Enumeration enumeration; 194 195 public SyncEnumeration(final Enumeration enumeration) { 196 this.enumeration = enumeration; 197 } 198 199 public synchronized boolean hasMoreElements() { 200 return enumeration.hasMoreElements(); 201 } 202 203 public synchronized Object nextElement() { 204 return enumeration.nextElement(); 205 } 206 } 207 208 214 public static Enumeration makeSynchronized(final Enumeration enumeration) { 215 return new SyncEnumeration(enumeration); 216 } 217 218 219 223 224 public static final Iterator EMPTY_ITERATOR = new EmptyIterator(); 225 226 229 private static final class EmptyIterator 230 implements Iterator 231 { 232 public boolean hasNext() { 233 return false; 234 } 235 236 public Object next() { 237 throw new NoSuchElementException ("no more elements"); 238 } 239 240 public void remove() { 241 throw new IllegalStateException ("no more elements"); 242 } 243 } 244 245 246 250 257 public static Iterator union(final Iterator iters[]) { 258 Map map = new HashMap (); 259 260 for (int i=0; i < iters.length; i++) { 261 if (iters[i] != null) { 262 while (iters[i].hasNext()) { 263 Object 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 282 public static String toString(final Iterator iter, final String delim) { 283 StringBuffer buff = new StringBuffer (); 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 302 public static String toString(final Iterator iter) { 303 return toString(iter, ","); 304 } 305 } 306 | Popular Tags |