1 16 package org.apache.commons.collections; 17 18 import java.lang.reflect.Array ; 19 import java.lang.reflect.Method ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Comparator ; 23 import java.util.Dictionary ; 24 import java.util.Enumeration ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.ListIterator ; 28 import java.util.Map ; 29 30 import org.apache.commons.collections.iterators.ArrayIterator; 31 import org.apache.commons.collections.iterators.ArrayListIterator; 32 import org.apache.commons.collections.iterators.CollatingIterator; 33 import org.apache.commons.collections.iterators.EmptyIterator; 34 import org.apache.commons.collections.iterators.EmptyListIterator; 35 import org.apache.commons.collections.iterators.EmptyMapIterator; 36 import org.apache.commons.collections.iterators.EmptyOrderedIterator; 37 import org.apache.commons.collections.iterators.EmptyOrderedMapIterator; 38 import org.apache.commons.collections.iterators.EnumerationIterator; 39 import org.apache.commons.collections.iterators.FilterIterator; 40 import org.apache.commons.collections.iterators.FilterListIterator; 41 import org.apache.commons.collections.iterators.IteratorChain; 42 import org.apache.commons.collections.iterators.IteratorEnumeration; 43 import org.apache.commons.collections.iterators.ListIteratorWrapper; 44 import org.apache.commons.collections.iterators.LoopingIterator; 45 import org.apache.commons.collections.iterators.ObjectArrayIterator; 46 import org.apache.commons.collections.iterators.ObjectArrayListIterator; 47 import org.apache.commons.collections.iterators.ObjectGraphIterator; 48 import org.apache.commons.collections.iterators.SingletonIterator; 49 import org.apache.commons.collections.iterators.SingletonListIterator; 50 import org.apache.commons.collections.iterators.TransformIterator; 51 import org.apache.commons.collections.iterators.UnmodifiableIterator; 52 import org.apache.commons.collections.iterators.UnmodifiableListIterator; 53 import org.apache.commons.collections.iterators.UnmodifiableMapIterator; 54 55 72 public class IteratorUtils { 73 76 82 public static final ResettableIterator EMPTY_ITERATOR = EmptyIterator.RESETTABLE_INSTANCE; 83 89 public static final ResettableListIterator EMPTY_LIST_ITERATOR = EmptyListIterator.RESETTABLE_INSTANCE; 90 93 public static final OrderedIterator EMPTY_ORDERED_ITERATOR = EmptyOrderedIterator.INSTANCE; 94 97 public static final MapIterator EMPTY_MAP_ITERATOR = EmptyMapIterator.INSTANCE; 98 101 public static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR = EmptyOrderedMapIterator.INSTANCE; 102 103 106 public IteratorUtils() { 107 } 108 109 122 public static ResettableIterator emptyIterator() { 123 return EMPTY_ITERATOR; 124 } 125 126 137 public static ResettableListIterator emptyListIterator() { 138 return EMPTY_LIST_ITERATOR; 139 } 140 141 149 public static OrderedIterator emptyOrderedIterator() { 150 return EMPTY_ORDERED_ITERATOR; 151 } 152 153 161 public static MapIterator emptyMapIterator() { 162 return EMPTY_MAP_ITERATOR; 163 } 164 165 173 public static OrderedMapIterator emptyOrderedMapIterator() { 174 return EMPTY_ORDERED_MAP_ITERATOR; 175 } 176 177 191 public static ResettableIterator singletonIterator(Object object) { 192 return new SingletonIterator(object); 193 } 194 195 204 public static ListIterator singletonListIterator(Object object) { 205 return new SingletonListIterator(object); 206 } 207 208 220 public static ResettableIterator arrayIterator(Object [] array) { 221 return new ObjectArrayIterator(array); 222 } 223 224 235 public static ResettableIterator arrayIterator(Object array) { 236 return new ArrayIterator(array); 237 } 238 239 252 public static ResettableIterator arrayIterator(Object [] array, int start) { 253 return new ObjectArrayIterator(array, start); 254 } 255 256 270 public static ResettableIterator arrayIterator(Object array, int start) { 271 return new ArrayIterator(array, start); 272 } 273 274 288 public static ResettableIterator arrayIterator(Object [] array, int start, int end) { 289 return new ObjectArrayIterator(array, start, end); 290 } 291 292 307 public static ResettableIterator arrayIterator(Object array, int start, int end) { 308 return new ArrayIterator(array, start, end); 309 } 310 311 319 public static ResettableListIterator arrayListIterator(Object [] array) { 320 return new ObjectArrayListIterator(array); 321 } 322 323 334 public static ResettableListIterator arrayListIterator(Object array) { 335 return new ArrayListIterator(array); 336 } 337 338 347 public static ResettableListIterator arrayListIterator(Object [] array, int start) { 348 return new ObjectArrayListIterator(array, start); 349 } 350 351 364 public static ResettableListIterator arrayListIterator(Object array, int start) { 365 return new ArrayListIterator(array, start); 366 } 367 368 379 public static ResettableListIterator arrayListIterator(Object [] array, int start, int end) { 380 return new ObjectArrayListIterator(array, start, end); 381 } 382 383 398 public static ResettableListIterator arrayListIterator(Object array, int start, int end) { 399 return new ArrayListIterator(array, start, end); 400 } 401 402 412 public static Iterator unmodifiableIterator(Iterator iterator) { 413 return UnmodifiableIterator.decorate(iterator); 414 } 415 416 425 public static ListIterator unmodifiableListIterator(ListIterator listIterator) { 426 return UnmodifiableListIterator.decorate(listIterator); 427 } 428 429 437 public static MapIterator unmodifiableMapIterator(MapIterator mapIterator) { 438 return UnmodifiableMapIterator.decorate(mapIterator); 439 } 440 441 452 public static Iterator chainedIterator(Iterator iterator1, Iterator iterator2) { 453 return new IteratorChain(iterator1, iterator2); 454 } 455 456 464 public static Iterator chainedIterator(Iterator [] iterators) { 465 return new IteratorChain(iterators); 466 } 467 468 477 public static Iterator chainedIterator(Collection iterators) { 478 return new IteratorChain(iterators); 479 } 480 481 499 public static Iterator collatedIterator(Comparator comparator, Iterator iterator1, Iterator iterator2) { 500 return new CollatingIterator(comparator, iterator1, iterator2); 501 } 502 503 518 public static Iterator collatedIterator(Comparator comparator, Iterator [] iterators) { 519 return new CollatingIterator(comparator, iterators); 520 } 521 522 538 public static Iterator collatedIterator(Comparator comparator, Collection iterators) { 539 return new CollatingIterator(comparator, iterators); 540 } 541 542 597 public static Iterator objectGraphIterator(Object root, Transformer transformer) { 598 return new ObjectGraphIterator(root, transformer); 599 } 600 601 614 public static Iterator transformedIterator(Iterator iterator, Transformer transform) { 615 if (iterator == null) { 616 throw new NullPointerException ("Iterator must not be null"); 617 } 618 if (transform == null) { 619 throw new NullPointerException ("Transformer must not be null"); 620 } 621 return new TransformIterator(iterator, transform); 622 } 623 624 637 public static Iterator filteredIterator(Iterator iterator, Predicate predicate) { 638 if (iterator == null) { 639 throw new NullPointerException ("Iterator must not be null"); 640 } 641 if (predicate == null) { 642 throw new NullPointerException ("Predicate must not be null"); 643 } 644 return new FilterIterator(iterator, predicate); 645 } 646 647 658 public static ListIterator filteredListIterator(ListIterator listIterator, Predicate predicate) { 659 if (listIterator == null) { 660 throw new NullPointerException ("ListIterator must not be null"); 661 } 662 if (predicate == null) { 663 throw new NullPointerException ("Predicate must not be null"); 664 } 665 return new FilterListIterator(listIterator, predicate); 666 } 667 668 681 public static ResettableIterator loopingIterator(Collection coll) { 682 if (coll == null) { 683 throw new NullPointerException ("Collection must not be null"); 684 } 685 return new LoopingIterator(coll); 686 } 687 688 696 public static Iterator asIterator(Enumeration enumeration) { 697 if (enumeration == null) { 698 throw new NullPointerException ("Enumeration must not be null"); 699 } 700 return new EnumerationIterator(enumeration); 701 } 702 703 711 public static Iterator asIterator(Enumeration enumeration, Collection removeCollection) { 712 if (enumeration == null) { 713 throw new NullPointerException ("Enumeration must not be null"); 714 } 715 if (removeCollection == null) { 716 throw new NullPointerException ("Collection must not be null"); 717 } 718 return new EnumerationIterator(enumeration, removeCollection); 719 } 720 721 728 public static Enumeration asEnumeration(Iterator iterator) { 729 if (iterator == null) { 730 throw new NullPointerException ("Iterator must not be null"); 731 } 732 return new IteratorEnumeration(iterator); 733 } 734 735 745 public static ListIterator toListIterator(Iterator iterator) { 746 if (iterator == null) { 747 throw new NullPointerException ("Iterator must not be null"); 748 } 749 return new ListIteratorWrapper(iterator); 750 } 751 752 762 public static Object [] toArray(Iterator iterator) { 763 if (iterator == null) { 764 throw new NullPointerException ("Iterator must not be null"); 765 } 766 List list = toList(iterator, 100); 767 return list.toArray(); 768 } 769 770 783 public static Object [] toArray(Iterator iterator, Class arrayClass) { 784 if (iterator == null) { 785 throw new NullPointerException ("Iterator must not be null"); 786 } 787 if (arrayClass == null) { 788 throw new NullPointerException ("Array class must not be null"); 789 } 790 List list = toList(iterator, 100); 791 return list.toArray((Object []) Array.newInstance(arrayClass, list.size())); 792 } 793 794 804 public static List toList(Iterator iterator) { 805 return toList(iterator, 10); 806 } 807 808 820 public static List toList(Iterator iterator, int estimatedSize) { 821 if (iterator == null) { 822 throw new NullPointerException ("Iterator must not be null"); 823 } 824 if (estimatedSize < 1) { 825 throw new IllegalArgumentException ("Estimated size must be greater than 0"); 826 } 827 List list = new ArrayList (estimatedSize); 828 while (iterator.hasNext()) { 829 list.add(iterator.next()); 830 } 831 return list; 832 } 833 834 853 public static Iterator getIterator(Object obj) { 854 if (obj == null) { 855 return emptyIterator(); 856 857 } else if (obj instanceof Iterator ) { 858 return (Iterator ) obj; 859 860 } else if (obj instanceof Collection ) { 861 return ((Collection ) obj).iterator(); 862 863 } else if (obj instanceof Object []) { 864 return new ObjectArrayIterator((Object []) obj); 865 866 } else if (obj instanceof Enumeration ) { 867 return new EnumerationIterator((Enumeration ) obj); 868 869 } else if (obj instanceof Map ) { 870 return ((Map ) obj).values().iterator(); 871 872 } else if (obj instanceof Dictionary ) { 873 return new EnumerationIterator(((Dictionary ) obj).elements()); 874 875 } else if (obj != null && obj.getClass().isArray()) { 876 return new ArrayIterator(obj); 877 878 } else { 879 try { 880 Method method = obj.getClass().getMethod("iterator", null); 881 if (Iterator .class.isAssignableFrom(method.getReturnType())) { 882 Iterator it = (Iterator ) method.invoke(obj, null); 883 if (it != null) { 884 return it; 885 } 886 } 887 } catch (Exception ex) { 888 } 890 return singletonIterator(obj); 891 } 892 } 893 894 } 895 | Popular Tags |