1 19 20 package org.openide.util; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.Enumeration ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.NoSuchElementException ; 30 import java.util.Set ; 31 32 41 public final class Enumerations extends Object { 42 43 private Enumerations() { 44 } 45 46 53 public static final <T> Enumeration <T> empty() { 54 Collection <T> emptyL = Collections.emptyList(); 55 return Collections.enumeration(emptyL); 56 } 57 58 63 public static <T> Enumeration <T> singleton(T obj) { 64 return Collections.enumeration(Collections.singleton(obj)); 65 } 66 67 78 public static <T> Enumeration <T> concat(Enumeration <? extends T> en1, Enumeration <? extends T> en2) { 79 ArrayList <Enumeration <? extends T>> two = new ArrayList <Enumeration <? extends T>>(); 80 two.add(en1); 81 two.add(en2); 82 return new SeqEn<T>(Collections.enumeration(two)); 83 } 84 85 96 public static <T> Enumeration <T> concat(Enumeration <? extends Enumeration <? extends T>> enumOfEnums) { 97 return new SeqEn<T>(enumOfEnums); 98 } 99 100 109 public static <T> Enumeration <T> removeDuplicates(Enumeration <T> en) { 110 class RDupls implements Processor<T,T> { 111 private Set <T> set = new HashSet <T>(); 112 113 public T process(T o, Collection <T> nothing) { 114 return set.add(o) ? o : null; 115 } 116 } 117 118 return filter(en, new RDupls()); 119 } 120 121 126 public static <T> Enumeration <T> array(T... arr) { 127 return Collections.enumeration(Arrays.asList(arr)); 128 } 129 130 135 public static <T> Enumeration <T> removeNulls(Enumeration <T> en) { 136 return filter(en, new RNulls<T>()); 137 } 138 139 158 public static <T,R> Enumeration <R> convert(Enumeration <? extends T> en, Processor<T,R> processor) { 159 return new AltEn<T,R>(en, processor); 160 } 161 162 187 public static <T,R> Enumeration <R> filter(Enumeration <? extends T> en, Processor<T,R> filter) { 188 return new FilEn<T,R>(en, filter); 189 } 190 191 220 public static <T,R> Enumeration <R> queue(Enumeration <? extends T> en, Processor<T,R> filter) { 221 QEn<T,R> q = new QEn<T,R>(filter); 222 223 while (en.hasMoreElements()) { 224 q.put(en.nextElement()); 225 } 226 227 return q; 228 } 229 230 234 public static interface Processor<T,R> { 235 239 public R process(T original, Collection <T> toAdd); 240 } 241 242 243 private static final class AltEn<T,R> extends Object implements Enumeration <R> { 244 245 private Enumeration <? extends T> en; 246 247 248 private Processor<T,R> process; 249 250 253 public AltEn(Enumeration <? extends T> en, Processor<T,R> process) { 254 this.en = en; 255 this.process = process; 256 } 257 258 260 public boolean hasMoreElements() { 261 return en.hasMoreElements(); 262 } 263 264 268 public R nextElement() { 269 return process.process(en.nextElement(), null); 270 } 271 } 272 274 275 private static final class SeqEn<T> extends Object implements Enumeration <T> { 276 277 private Enumeration <? extends Enumeration <? extends T>> en; 278 279 280 private Enumeration <? extends T> current; 281 282 287 private boolean checked = false; 288 289 295 public SeqEn(Enumeration <? extends Enumeration <? extends T>> en) { 296 this.en = en; 297 } 298 299 302 private void ensureCurrent() { 303 while ((current == null) || !current.hasMoreElements()) { 304 if (en.hasMoreElements()) { 305 current = en.nextElement(); 306 } else { 307 current = null; 309 310 return; 311 } 312 } 313 } 314 315 316 public boolean hasMoreElements() { 317 if (!checked) { 318 ensureCurrent(); 319 checked = true; 320 } 321 322 return current != null; 323 } 324 325 328 public T nextElement() { 329 if (!checked) { 330 ensureCurrent(); 331 } 332 333 if (current != null) { 334 checked = false; 335 336 return current.nextElement(); 337 } else { 338 checked = true; 339 throw new java.util.NoSuchElementException (); 340 } 341 } 342 } 343 345 347 private static class QEn<T,R> extends Object implements Enumeration <R> { 348 349 private ListItem<T> next = null; 350 351 352 private ListItem<T> last = null; 353 354 355 private Processor<T,R> processor; 356 357 public QEn(Processor<T,R> p) { 358 this.processor = p; 359 } 360 361 364 public void put(T o) { 365 if (last != null) { 366 ListItem<T> li = new ListItem<T>(o); 367 last.next = li; 368 last = li; 369 } else { 370 next = last = new ListItem<T>(o); 371 } 372 } 373 374 377 public void put(Collection <? extends T> arr) { 378 for (T e : arr) { 379 put(e); 380 } 381 } 382 383 386 public boolean hasMoreElements() { 387 return next != null; 388 } 389 390 393 public R nextElement() { 394 if (next == null) { 395 throw new NoSuchElementException (); 396 } 397 398 T res = next.object; 399 400 if ((next = next.next) == null) { 401 last = null; 402 } 403 404 ; 405 406 ToAdd<T,R> toAdd = new ToAdd<T,R>(this); 407 R out = processor.process(res, toAdd); 408 toAdd.finish(); 409 410 return out; 411 } 412 413 414 private static final class ListItem<T> { 415 T object; 416 ListItem<T> next; 417 418 419 ListItem(T o) { 420 object = o; 421 } 422 } 423 424 425 private static final class ToAdd<T,R> extends Object implements Collection <T> { 426 private QEn<T,R> q; 427 428 public ToAdd(QEn<T,R> q) { 429 this.q = q; 430 } 431 432 public void finish() { 433 this.q = null; 434 } 435 436 public boolean add(T o) { 437 q.put(o); 438 439 return true; 440 } 441 442 public boolean addAll(Collection <? extends T> c) { 443 q.put(c); 444 445 return true; 446 } 447 448 private String msg() { 449 return "Only add and addAll are implemented"; } 451 452 public void clear() { 453 throw new UnsupportedOperationException (msg()); 454 } 455 456 public boolean contains(Object o) { 457 throw new UnsupportedOperationException (msg()); 458 } 459 460 public boolean containsAll(Collection c) { 461 throw new UnsupportedOperationException (msg()); 462 } 463 464 public boolean isEmpty() { 465 throw new UnsupportedOperationException (msg()); 466 } 467 468 public Iterator <T> iterator() { 469 throw new UnsupportedOperationException (msg()); 470 } 471 472 public boolean remove(Object o) { 473 throw new UnsupportedOperationException (msg()); 474 } 475 476 public boolean removeAll(Collection c) { 477 throw new UnsupportedOperationException (msg()); 478 } 479 480 public boolean retainAll(Collection c) { 481 throw new UnsupportedOperationException (msg()); 482 } 483 484 public int size() { 485 throw new UnsupportedOperationException (msg()); 486 } 487 488 public Object [] toArray() { 489 throw new UnsupportedOperationException (msg()); 490 } 491 492 public<X> X[] toArray(X[] a) { 493 throw new UnsupportedOperationException (msg()); 494 } 495 } 496 } 498 500 501 private static final class FilEn<T,R> extends Object implements Enumeration <R> { 502 503 private static final Object EMPTY = new Object (); 504 505 506 private Enumeration <? extends T> en; 507 508 510 private R next = empty(); 511 512 513 private Processor<T,R> filter; 514 515 518 public FilEn(Enumeration <? extends T> en, Processor<T,R> filter) { 519 this.en = en; 520 this.filter = filter; 521 } 522 523 525 public boolean hasMoreElements() { 526 if (next != empty()) { 527 return true; 529 } 530 531 while (en.hasMoreElements()) { 532 next = filter.process(en.nextElement(), null); 534 535 if (next != null) { 536 return true; 538 } 539 540 ; 541 } 542 543 next = empty(); 544 545 return false; 546 } 547 548 552 public R nextElement() { 553 if ((next == EMPTY) && !hasMoreElements()) { 554 throw new NoSuchElementException (); 555 } 556 557 R res = next; 558 next = empty(); 559 560 return res; 561 } 562 563 @SuppressWarnings ("unchecked") 564 private R empty() { 565 return (R)EMPTY; 566 } 567 } 568 570 571 private static class RNulls<T> implements Processor<T,T> { 572 public T process(T original, Collection <T> toAdd) { 573 return original; 574 } 575 } 576 } 578 | Popular Tags |