| 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 |