1 16 package org.apache.commons.collections; 17 18 import java.lang.reflect.Array ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Enumeration ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.ListIterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 import org.apache.commons.collections.collection.PredicatedCollection; 31 import org.apache.commons.collections.collection.SynchronizedCollection; 32 import org.apache.commons.collections.collection.TransformedCollection; 33 import org.apache.commons.collections.collection.TypedCollection; 34 import org.apache.commons.collections.collection.UnmodifiableBoundedCollection; 35 import org.apache.commons.collections.collection.UnmodifiableCollection; 36 37 55 public class CollectionUtils { 56 57 58 private static Integer INTEGER_ONE = new Integer (1); 59 60 66 public static final Collection EMPTY_COLLECTION = UnmodifiableCollection.decorate(new ArrayList ()); 67 68 71 public CollectionUtils() { 72 } 73 74 87 public static Collection union(final Collection a, final Collection b) { 88 ArrayList list = new ArrayList (); 89 Map mapa = getCardinalityMap(a); 90 Map mapb = getCardinalityMap(b); 91 Set elts = new HashSet (a); 92 elts.addAll(b); 93 Iterator it = elts.iterator(); 94 while(it.hasNext()) { 95 Object obj = it.next(); 96 for(int i=0,m=Math.max(getFreq(obj,mapa),getFreq(obj,mapb));i<m;i++) { 97 list.add(obj); 98 } 99 } 100 return list; 101 } 102 103 117 public static Collection intersection(final Collection a, final Collection b) { 118 ArrayList list = new ArrayList (); 119 Map mapa = getCardinalityMap(a); 120 Map mapb = getCardinalityMap(b); 121 Set elts = new HashSet (a); 122 elts.addAll(b); 123 Iterator it = elts.iterator(); 124 while(it.hasNext()) { 125 Object obj = it.next(); 126 for(int i=0,m=Math.min(getFreq(obj,mapa),getFreq(obj,mapb));i<m;i++) { 127 list.add(obj); 128 } 129 } 130 return list; 131 } 132 133 150 public static Collection disjunction(final Collection a, final Collection b) { 151 ArrayList list = new ArrayList (); 152 Map mapa = getCardinalityMap(a); 153 Map mapb = getCardinalityMap(b); 154 Set elts = new HashSet (a); 155 elts.addAll(b); 156 Iterator it = elts.iterator(); 157 while(it.hasNext()) { 158 Object obj = it.next(); 159 for(int i=0,m=((Math.max(getFreq(obj,mapa),getFreq(obj,mapb)))-(Math.min(getFreq(obj,mapa),getFreq(obj,mapb))));i<m;i++) { 160 list.add(obj); 161 } 162 } 163 return list; 164 } 165 166 177 public static Collection subtract(final Collection a, final Collection b) { 178 ArrayList list = new ArrayList ( a ); 179 for (Iterator it = b.iterator(); it.hasNext();) { 180 list.remove(it.next()); 181 } 182 return list; 183 } 184 185 197 public static boolean containsAny(final Collection coll1, final Collection coll2) { 198 if (coll1.size() < coll2.size()) { 199 for (Iterator it = coll1.iterator(); it.hasNext();) { 200 if (coll2.contains(it.next())) { 201 return true; 202 } 203 } 204 } else { 205 for (Iterator it = coll2.iterator(); it.hasNext();) { 206 if (coll1.contains(it.next())) { 207 return true; 208 } 209 } 210 } 211 return false; 212 } 213 214 225 public static Map getCardinalityMap(final Collection coll) { 226 Map count = new HashMap (); 227 for (Iterator it = coll.iterator(); it.hasNext();) { 228 Object obj = it.next(); 229 Integer c = (Integer ) (count.get(obj)); 230 if (c == null) { 231 count.put(obj,INTEGER_ONE); 232 } else { 233 count.put(obj,new Integer (c.intValue() + 1)); 234 } 235 } 236 return count; 237 } 238 239 251 public static boolean isSubCollection(final Collection a, final Collection b) { 252 Map mapa = getCardinalityMap(a); 253 Map mapb = getCardinalityMap(b); 254 Iterator it = a.iterator(); 255 while (it.hasNext()) { 256 Object obj = it.next(); 257 if (getFreq(obj, mapa) > getFreq(obj, mapb)) { 258 return false; 259 } 260 } 261 return true; 262 } 263 264 285 public static boolean isProperSubCollection(final Collection a, final Collection b) { 286 return (a.size() < b.size()) && CollectionUtils.isSubCollection(a,b); 287 } 288 289 301 public static boolean isEqualCollection(final Collection a, final Collection b) { 302 if(a.size() != b.size()) { 303 return false; 304 } else { 305 Map mapa = getCardinalityMap(a); 306 Map mapb = getCardinalityMap(b); 307 if(mapa.size() != mapb.size()) { 308 return false; 309 } else { 310 Iterator it = mapa.keySet().iterator(); 311 while(it.hasNext()) { 312 Object obj = it.next(); 313 if(getFreq(obj,mapa) != getFreq(obj,mapb)) { 314 return false; 315 } 316 } 317 return true; 318 } 319 } 320 } 321 322 329 public static int cardinality(Object obj, final Collection coll) { 330 if (coll instanceof Set ) { 331 return (coll.contains(obj) ? 1 : 0); 332 } 333 if (coll instanceof Bag) { 334 return ((Bag) coll).getCount(obj); 335 } 336 int count = 0; 337 if (obj == null) { 338 for (Iterator it = coll.iterator();it.hasNext();) { 339 if (it.next() == null) { 340 count++; 341 } 342 } 343 } else { 344 for (Iterator it = coll.iterator();it.hasNext();) { 345 if (obj.equals(it.next())) { 346 count++; 347 } 348 } 349 } 350 return count; 351 } 352 353 363 public static Object find(Collection collection, Predicate predicate) { 364 if (collection != null && predicate != null) { 365 for (Iterator iter = collection.iterator(); iter.hasNext();) { 366 Object item = iter.next(); 367 if (predicate.evaluate(item)) { 368 return item; 369 } 370 } 371 } 372 return null; 373 } 374 375 383 public static void forAllDo(Collection collection, Closure closure) { 384 if (collection != null && closure != null) { 385 for (Iterator it = collection.iterator(); it.hasNext();) { 386 closure.execute(it.next()); 387 } 388 } 389 } 390 391 400 public static void filter(Collection collection, Predicate predicate) { 401 if (collection != null && predicate != null) { 402 for (Iterator it = collection.iterator(); it.hasNext();) { 403 if (predicate.evaluate(it.next()) == false) { 404 it.remove(); 405 } 406 } 407 } 408 } 409 410 426 public static void transform(Collection collection, Transformer transformer) { 427 if (collection != null && transformer != null) { 428 if (collection instanceof List ) { 429 List list = (List ) collection; 430 for (ListIterator it = list.listIterator(); it.hasNext();) { 431 it.set(transformer.transform(it.next())); 432 } 433 } else { 434 Collection resultCollection = collect(collection, transformer); 435 collection.clear(); 436 collection.addAll(resultCollection); 437 } 438 } 439 } 440 441 450 public static int countMatches(Collection inputCollection, Predicate predicate) { 451 int count = 0; 452 if (inputCollection != null && predicate != null) { 453 for (Iterator it = inputCollection.iterator(); it.hasNext();) { 454 if (predicate.evaluate(it.next())) { 455 count++; 456 } 457 } 458 } 459 return count; 460 } 461 462 471 public static boolean exists(Collection collection, Predicate predicate) { 472 if (collection != null && predicate != null) { 473 for (Iterator it = collection.iterator(); it.hasNext();) { 474 if (predicate.evaluate(it.next())) { 475 return true; 476 } 477 } 478 } 479 return false; 480 } 481 482 493 public static Collection select(Collection inputCollection, Predicate predicate) { 494 ArrayList answer = new ArrayList (inputCollection.size()); 495 select(inputCollection, predicate, answer); 496 return answer; 497 } 498 499 510 public static void select(Collection inputCollection, Predicate predicate, Collection outputCollection) { 511 if (inputCollection != null && predicate != null) { 512 for (Iterator iter = inputCollection.iterator(); iter.hasNext();) { 513 Object item = iter.next(); 514 if (predicate.evaluate(item)) { 515 outputCollection.add(item); 516 } 517 } 518 } 519 } 520 521 532 public static Collection selectRejected(Collection inputCollection, Predicate predicate) { 533 ArrayList answer = new ArrayList (inputCollection.size()); 534 selectRejected(inputCollection, predicate, answer); 535 return answer; 536 } 537 538 548 public static void selectRejected(Collection inputCollection, Predicate predicate, Collection outputCollection) { 549 if (inputCollection != null && predicate != null) { 550 for (Iterator iter = inputCollection.iterator(); iter.hasNext();) { 551 Object item = iter.next(); 552 if (predicate.evaluate(item) == false) { 553 outputCollection.add(item); 554 } 555 } 556 } 557 } 558 559 570 public static Collection collect(Collection inputCollection, Transformer transformer) { 571 ArrayList answer = new ArrayList (inputCollection.size()); 572 collect(inputCollection, transformer, answer); 573 return answer; 574 } 575 576 586 public static Collection collect(Iterator inputIterator, Transformer transformer) { 587 ArrayList answer = new ArrayList (); 588 collect(inputIterator, transformer, answer); 589 return answer; 590 } 591 592 605 public static Collection collect(Collection inputCollection, final Transformer transformer, final Collection outputCollection) { 606 if (inputCollection != null) { 607 return collect(inputCollection.iterator(), transformer, outputCollection); 608 } 609 return outputCollection; 610 } 611 612 625 public static Collection collect(Iterator inputIterator, final Transformer transformer, final Collection outputCollection) { 626 if (inputIterator != null && transformer != null) { 627 while (inputIterator.hasNext()) { 628 Object item = inputIterator.next(); 629 Object value = transformer.transform(item); 630 outputCollection.add(value); 631 } 632 } 633 return outputCollection; 634 } 635 636 643 public static void addAll(Collection collection, Iterator iterator) { 644 while (iterator.hasNext()) { 645 collection.add(iterator.next()); 646 } 647 } 648 649 656 public static void addAll(Collection collection, Enumeration enumeration) { 657 while (enumeration.hasMoreElements()) { 658 collection.add(enumeration.nextElement()); 659 } 660 } 661 662 669 public static void addAll(Collection collection, Object [] elements) { 670 for (int i = 0, size = elements.length; i < size; i++) { 671 collection.add(elements[i]); 672 } 673 } 674 675 697 public static Object index(Object obj, int idx) { 698 return index(obj, new Integer (idx)); 699 } 700 701 724 public static Object index(Object obj, Object index) { 725 if(obj instanceof Map ) { 726 Map map = (Map )obj; 727 if(map.containsKey(index)) { 728 return map.get(index); 729 } 730 } 731 int idx = -1; 732 if(index instanceof Integer ) { 733 idx = ((Integer )index).intValue(); 734 } 735 if(idx < 0) { 736 return obj; 737 } 738 else if(obj instanceof Map ) { 739 Map map = (Map )obj; 740 Iterator iterator = map.keySet().iterator(); 741 return index(iterator, idx); 742 } 743 else if(obj instanceof List ) { 744 return ((List )obj).get(idx); 745 } 746 else if(obj instanceof Object []) { 747 return ((Object [])obj)[idx]; 748 } 749 else if(obj instanceof Enumeration ) { 750 Enumeration it = (Enumeration )obj; 751 while(it.hasMoreElements()) { 752 idx--; 753 if(idx == -1) { 754 return it.nextElement(); 755 } else { 756 it.nextElement(); 757 } 758 } 759 } 760 else if(obj instanceof Iterator ) { 761 return index((Iterator )obj, idx); 762 } 763 else if(obj instanceof Collection ) { 764 Iterator iterator = ((Collection )obj).iterator(); 765 return index(iterator, idx); 766 } 767 return obj; 768 } 769 770 private static Object index(Iterator iterator, int idx) { 771 while(iterator.hasNext()) { 772 idx--; 773 if(idx == -1) { 774 return iterator.next(); 775 } else { 776 iterator.next(); 777 } 778 } 779 return iterator; 780 } 781 782 812 public static Object get(Object object, int index) { 813 if (index < 0) { 814 throw new IndexOutOfBoundsException ("Index cannot be negative: " + index); 815 } 816 if (object instanceof Map ) { 817 Map map = (Map ) object; 818 Iterator iterator = map.entrySet().iterator(); 819 return get(iterator, index); 820 } else if (object instanceof List ) { 821 return ((List ) object).get(index); 822 } else if (object instanceof Object []) { 823 return ((Object []) object)[index]; 824 } else if (object instanceof Iterator ) { 825 Iterator it = (Iterator ) object; 826 while (it.hasNext()) { 827 index--; 828 if (index == -1) { 829 return it.next(); 830 } else { 831 it.next(); 832 } 833 } 834 throw new IndexOutOfBoundsException ("Entry does not exist: " + index); 835 } else if (object instanceof Collection ) { 836 Iterator iterator = ((Collection ) object).iterator(); 837 return get(iterator, index); 838 } else if (object instanceof Enumeration ) { 839 Enumeration it = (Enumeration ) object; 840 while (it.hasMoreElements()) { 841 index--; 842 if (index == -1) { 843 return it.nextElement(); 844 } else { 845 it.nextElement(); 846 } 847 } 848 throw new IndexOutOfBoundsException ("Entry does not exist: " + index); 849 } else if (object == null) { 850 throw new IllegalArgumentException ("Unsupported object type: null"); 851 } else { 852 try { 853 return Array.get(object, index); 854 } catch (IllegalArgumentException ex) { 855 throw new IllegalArgumentException ("Unsupported object type: " + object.getClass().getName()); 856 } 857 } 858 } 859 860 877 public static int size(Object object) { 878 int total = 0; 879 if (object instanceof Map ) { 880 total = ((Map ) object).size(); 881 } else if (object instanceof Collection ) { 882 total = ((Collection ) object).size(); 883 } else if (object instanceof Object []) { 884 total = ((Object []) object).length; 885 } else if (object instanceof Iterator ) { 886 Iterator it = (Iterator ) object; 887 while (it.hasNext()) { 888 total++; 889 it.next(); 890 } 891 } else if (object instanceof Enumeration ) { 892 Enumeration it = (Enumeration ) object; 893 while (it.hasMoreElements()) { 894 total++; 895 it.nextElement(); 896 } 897 } else if (object == null) { 898 throw new IllegalArgumentException ("Unsupported object type: null"); 899 } else { 900 try { 901 total = Array.getLength(object); 902 } catch (IllegalArgumentException ex) { 903 throw new IllegalArgumentException ("Unsupported object type: " + object.getClass().getName()); 904 } 905 } 906 return total; 907 } 908 909 914 public static void reverseArray(Object [] array) { 915 int i = 0; 916 int j = array.length - 1; 917 Object tmp; 918 919 while (j > i) { 920 tmp = array[j]; 921 array[j] = array[i]; 922 array[i] = tmp; 923 j--; 924 i++; 925 } 926 } 927 928 private static final int getFreq(final Object obj, final Map freqMap) { 929 Integer count = (Integer ) freqMap.get(obj); 930 if (count != null) { 931 return count.intValue(); 932 } 933 return 0; 934 } 935 936 951 public static boolean isFull(Collection coll) { 952 if (coll == null) { 953 throw new NullPointerException ("The collection must not be null"); 954 } 955 if (coll instanceof BoundedCollection) { 956 return ((BoundedCollection) coll).isFull(); 957 } 958 try { 959 BoundedCollection bcoll = UnmodifiableBoundedCollection.decorateUsing(coll); 960 return bcoll.isFull(); 961 962 } catch (IllegalArgumentException ex) { 963 return false; 964 } 965 } 966 967 982 public static int maxSize(Collection coll) { 983 if (coll == null) { 984 throw new NullPointerException ("The collection must not be null"); 985 } 986 if (coll instanceof BoundedCollection) { 987 return ((BoundedCollection) coll).maxSize(); 988 } 989 try { 990 BoundedCollection bcoll = UnmodifiableBoundedCollection.decorateUsing(coll); 991 return bcoll.maxSize(); 992 993 } catch (IllegalArgumentException ex) { 994 return -1; 995 } 996 } 997 998 1021 public static Collection synchronizedCollection(Collection collection) { 1022 return SynchronizedCollection.decorate(collection); 1023 } 1024 1025 1034 public static Collection unmodifiableCollection(Collection collection) { 1035 return UnmodifiableCollection.decorate(collection); 1036 } 1037 1038 1051 public static Collection predicatedCollection(Collection collection, Predicate predicate) { 1052 return PredicatedCollection.decorate(collection, predicate); 1053 } 1054 1055 1064 public static Collection typedCollection(Collection collection, Class type) { 1065 return TypedCollection.decorate(collection, type); 1066 } 1067 1068 1080 public static Collection transformedCollection(Collection collection, Transformer transformer) { 1081 return TransformedCollection.decorate(collection, transformer); 1082 } 1083 1084} 1085 | Popular Tags |