001/* 002 * Copyright (C) 2008 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import static com.google.common.base.Preconditions.checkArgument; 020import static com.google.common.base.Preconditions.checkNotNull; 021import static com.google.common.collect.CollectPreconditions.checkNonnegative; 022import static com.google.common.math.LongMath.binomial; 023 024import com.google.common.annotations.Beta; 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.base.Function; 027import com.google.common.base.Predicate; 028import com.google.common.base.Predicates; 029import com.google.common.math.IntMath; 030import com.google.common.primitives.Ints; 031import java.util.AbstractCollection; 032import java.util.ArrayList; 033import java.util.Arrays; 034import java.util.Collection; 035import java.util.Collections; 036import java.util.Comparator; 037import java.util.Iterator; 038import java.util.List; 039import java.util.Spliterator; 040import java.util.function.Consumer; 041import javax.annotation.Nullable; 042 043/** 044 * Provides static methods for working with {@code Collection} instances. 045 * 046 * <p><b>Java 8 users:</b> several common uses for this class are now more comprehensively addressed 047 * by the new {@link java.util.stream.Stream} library. Read the method documentation below for 048 * comparisons. These methods are not being deprecated, but we gently encourage you to migrate to 049 * streams. 050 * 051 * @author Chris Povirk 052 * @author Mike Bostock 053 * @author Jared Levy 054 * @since 2.0 055 */ 056@GwtCompatible 057public final class Collections2 { 058 private Collections2() {} 059 060 /** 061 * Returns the elements of {@code unfiltered} that satisfy a predicate. The 062 * returned collection is a live view of {@code unfiltered}; changes to one 063 * affect the other. 064 * 065 * <p>The resulting collection's iterator does not support {@code remove()}, 066 * but all other collection methods are supported. When given an element that 067 * doesn't satisfy the predicate, the collection's {@code add()} and {@code 068 * addAll()} methods throw an {@link IllegalArgumentException}. When methods 069 * such as {@code removeAll()} and {@code clear()} are called on the filtered 070 * collection, only elements that satisfy the filter will be removed from the 071 * underlying collection. 072 * 073 * <p>The returned collection isn't threadsafe or serializable, even if 074 * {@code unfiltered} is. 075 * 076 * <p>Many of the filtered collection's methods, such as {@code size()}, 077 * iterate across every element in the underlying collection and determine 078 * which elements satisfy the filter. When a live view is <i>not</i> needed, 079 * it may be faster to copy {@code Iterables.filter(unfiltered, predicate)} 080 * and use the copy. 081 * 082 * <p><b>Warning:</b> {@code predicate} must be <i>consistent with equals</i>, 083 * as documented at {@link Predicate#apply}. Do not provide a predicate such 084 * as {@code Predicates.instanceOf(ArrayList.class)}, which is inconsistent 085 * with equals. (See {@link Iterables#filter(Iterable, Class)} for related 086 * functionality.) 087 * 088 * <p><b>{@code Stream} equivalent:</b> {@link java.util.stream.Stream#filter Stream.filter}. 089 */ 090 // TODO(kevinb): how can we omit that Iterables link when building gwt 091 // javadoc? 092 public static <E> Collection<E> filter(Collection<E> unfiltered, Predicate<? super E> predicate) { 093 if (unfiltered instanceof FilteredCollection) { 094 // Support clear(), removeAll(), and retainAll() when filtering a filtered 095 // collection. 096 return ((FilteredCollection<E>) unfiltered).createCombined(predicate); 097 } 098 099 return new FilteredCollection<E>(checkNotNull(unfiltered), checkNotNull(predicate)); 100 } 101 102 /** 103 * Delegates to {@link Collection#contains}. Returns {@code false} if the 104 * {@code contains} method throws a {@code ClassCastException} or 105 * {@code NullPointerException}. 106 */ 107 static boolean safeContains(Collection<?> collection, @Nullable Object object) { 108 checkNotNull(collection); 109 try { 110 return collection.contains(object); 111 } catch (ClassCastException e) { 112 return false; 113 } catch (NullPointerException e) { 114 return false; 115 } 116 } 117 118 /** 119 * Delegates to {@link Collection#remove}. Returns {@code false} if the 120 * {@code remove} method throws a {@code ClassCastException} or 121 * {@code NullPointerException}. 122 */ 123 static boolean safeRemove(Collection<?> collection, @Nullable Object object) { 124 checkNotNull(collection); 125 try { 126 return collection.remove(object); 127 } catch (ClassCastException e) { 128 return false; 129 } catch (NullPointerException e) { 130 return false; 131 } 132 } 133 134 static class FilteredCollection<E> extends AbstractCollection<E> { 135 final Collection<E> unfiltered; 136 final Predicate<? super E> predicate; 137 138 FilteredCollection(Collection<E> unfiltered, Predicate<? super E> predicate) { 139 this.unfiltered = unfiltered; 140 this.predicate = predicate; 141 } 142 143 FilteredCollection<E> createCombined(Predicate<? super E> newPredicate) { 144 return new FilteredCollection<E>(unfiltered, Predicates.<E>and(predicate, newPredicate)); 145 // .<E> above needed to compile in JDK 5 146 } 147 148 @Override 149 public boolean add(E element) { 150 checkArgument(predicate.apply(element)); 151 return unfiltered.add(element); 152 } 153 154 @Override 155 public boolean addAll(Collection<? extends E> collection) { 156 for (E element : collection) { 157 checkArgument(predicate.apply(element)); 158 } 159 return unfiltered.addAll(collection); 160 } 161 162 @Override 163 public void clear() { 164 Iterables.removeIf(unfiltered, predicate); 165 } 166 167 @Override 168 public boolean contains(@Nullable Object element) { 169 if (safeContains(unfiltered, element)) { 170 @SuppressWarnings("unchecked") // element is in unfiltered, so it must be an E 171 E e = (E) element; 172 return predicate.apply(e); 173 } 174 return false; 175 } 176 177 @Override 178 public boolean containsAll(Collection<?> collection) { 179 return containsAllImpl(this, collection); 180 } 181 182 @Override 183 public boolean isEmpty() { 184 return !Iterables.any(unfiltered, predicate); 185 } 186 187 @Override 188 public Iterator<E> iterator() { 189 return Iterators.filter(unfiltered.iterator(), predicate); 190 } 191 192 @Override 193 public Spliterator<E> spliterator() { 194 return CollectSpliterators.filter(unfiltered.spliterator(), predicate); 195 } 196 197 @Override 198 public void forEach(Consumer<? super E> action) { 199 checkNotNull(action); 200 unfiltered.forEach( 201 (E e) -> { 202 if (predicate.test(e)) { 203 action.accept(e); 204 } 205 }); 206 } 207 208 @Override 209 public boolean remove(Object element) { 210 return contains(element) && unfiltered.remove(element); 211 } 212 213 @Override 214 public boolean removeAll(final Collection<?> collection) { 215 return removeIf(collection::contains); 216 } 217 218 @Override 219 public boolean retainAll(final Collection<?> collection) { 220 return removeIf(element -> !collection.contains(element)); 221 } 222 223 @Override 224 public boolean removeIf(java.util.function.Predicate<? super E> filter) { 225 checkNotNull(filter); 226 return unfiltered.removeIf(element -> predicate.apply(element) && filter.test(element)); 227 } 228 229 @Override 230 public int size() { 231 return Iterators.size(iterator()); 232 } 233 234 @Override 235 public Object[] toArray() { 236 // creating an ArrayList so filtering happens once 237 return Lists.newArrayList(iterator()).toArray(); 238 } 239 240 @Override 241 public <T> T[] toArray(T[] array) { 242 return Lists.newArrayList(iterator()).toArray(array); 243 } 244 } 245 246 /** 247 * Returns a collection that applies {@code function} to each element of 248 * {@code fromCollection}. The returned collection is a live view of {@code 249 * fromCollection}; changes to one affect the other. 250 * 251 * <p>The returned collection's {@code add()} and {@code addAll()} methods 252 * throw an {@link UnsupportedOperationException}. All other collection 253 * methods are supported, as long as {@code fromCollection} supports them. 254 * 255 * <p>The returned collection isn't threadsafe or serializable, even if 256 * {@code fromCollection} is. 257 * 258 * <p>When a live view is <i>not</i> needed, it may be faster to copy the 259 * transformed collection and use the copy. 260 * 261 * <p>If the input {@code Collection} is known to be a {@code List}, consider 262 * {@link Lists#transform}. If only an {@code Iterable} is available, use 263 * {@link Iterables#transform}. 264 * 265 * <p><b>{@code Stream} equivalent:</b> {@link java.util.stream.Stream#map Stream.map}. 266 */ 267 public static <F, T> Collection<T> transform( 268 Collection<F> fromCollection, Function<? super F, T> function) { 269 return new TransformedCollection<F, T>(fromCollection, function); 270 } 271 272 static class TransformedCollection<F, T> extends AbstractCollection<T> { 273 final Collection<F> fromCollection; 274 final Function<? super F, ? extends T> function; 275 276 TransformedCollection(Collection<F> fromCollection, Function<? super F, ? extends T> function) { 277 this.fromCollection = checkNotNull(fromCollection); 278 this.function = checkNotNull(function); 279 } 280 281 @Override 282 public void clear() { 283 fromCollection.clear(); 284 } 285 286 @Override 287 public boolean isEmpty() { 288 return fromCollection.isEmpty(); 289 } 290 291 @Override 292 public Iterator<T> iterator() { 293 return Iterators.transform(fromCollection.iterator(), function); 294 } 295 296 @Override 297 public Spliterator<T> spliterator() { 298 return CollectSpliterators.map(fromCollection.spliterator(), function); 299 } 300 301 @Override 302 public void forEach(Consumer<? super T> action) { 303 checkNotNull(action); 304 fromCollection.forEach((F f) -> action.accept(function.apply(f))); 305 } 306 307 @Override 308 public boolean removeIf(java.util.function.Predicate<? super T> filter) { 309 checkNotNull(filter); 310 return fromCollection.removeIf(element -> filter.test(function.apply(element))); 311 } 312 313 @Override 314 public int size() { 315 return fromCollection.size(); 316 } 317 } 318 319 /** 320 * Returns {@code true} if the collection {@code self} contains all of the 321 * elements in the collection {@code c}. 322 * 323 * <p>This method iterates over the specified collection {@code c}, checking 324 * each element returned by the iterator in turn to see if it is contained in 325 * the specified collection {@code self}. If all elements are so contained, 326 * {@code true} is returned, otherwise {@code false}. 327 * 328 * @param self a collection which might contain all elements in {@code c} 329 * @param c a collection whose elements might be contained by {@code self} 330 */ 331 static boolean containsAllImpl(Collection<?> self, Collection<?> c) { 332 return Iterables.all(c, Predicates.in(self)); 333 } 334 335 /** 336 * An implementation of {@link Collection#toString()}. 337 */ 338 static String toStringImpl(final Collection<?> collection) { 339 StringBuilder sb = newStringBuilderForCollection(collection.size()).append('['); 340 boolean first = true; 341 for (Object o : collection) { 342 if (!first) { 343 sb.append(", "); 344 } 345 first = false; 346 if (o == collection) { 347 sb.append("(this Collection)"); 348 } else { 349 sb.append(o); 350 } 351 } 352 return sb.append(']').toString(); 353 } 354 355 /** 356 * Returns best-effort-sized StringBuilder based on the given collection size. 357 */ 358 static StringBuilder newStringBuilderForCollection(int size) { 359 checkNonnegative(size, "size"); 360 return new StringBuilder((int) Math.min(size * 8L, Ints.MAX_POWER_OF_TWO)); 361 } 362 363 /** 364 * Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557 365 */ 366 static <T> Collection<T> cast(Iterable<T> iterable) { 367 return (Collection<T>) iterable; 368 } 369 370 /** 371 * Returns a {@link Collection} of all the permutations of the specified 372 * {@link Iterable}. 373 * 374 * <p><i>Notes:</i> This is an implementation of the algorithm for 375 * Lexicographical Permutations Generation, described in Knuth's "The Art of 376 * Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2. The 377 * iteration order follows the lexicographical order. This means that 378 * the first permutation will be in ascending order, and the last will be in 379 * descending order. 380 * 381 * <p>Duplicate elements are considered equal. For example, the list [1, 1] 382 * will have only one permutation, instead of two. This is why the elements 383 * have to implement {@link Comparable}. 384 * 385 * <p>An empty iterable has only one permutation, which is an empty list. 386 * 387 * <p>This method is equivalent to 388 * {@code Collections2.orderedPermutations(list, Ordering.natural())}. 389 * 390 * @param elements the original iterable whose elements have to be permuted. 391 * @return an immutable {@link Collection} containing all the different 392 * permutations of the original iterable. 393 * @throws NullPointerException if the specified iterable is null or has any 394 * null elements. 395 * @since 12.0 396 */ 397 @Beta 398 public static <E extends Comparable<? super E>> Collection<List<E>> orderedPermutations( 399 Iterable<E> elements) { 400 return orderedPermutations(elements, Ordering.natural()); 401 } 402 403 /** 404 * Returns a {@link Collection} of all the permutations of the specified 405 * {@link Iterable} using the specified {@link Comparator} for establishing 406 * the lexicographical ordering. 407 * 408 * <p>Examples: <pre> {@code 409 * 410 * for (List<String> perm : orderedPermutations(asList("b", "c", "a"))) { 411 * println(perm); 412 * } 413 * // -> ["a", "b", "c"] 414 * // -> ["a", "c", "b"] 415 * // -> ["b", "a", "c"] 416 * // -> ["b", "c", "a"] 417 * // -> ["c", "a", "b"] 418 * // -> ["c", "b", "a"] 419 * 420 * for (List<Integer> perm : orderedPermutations(asList(1, 2, 2, 1))) { 421 * println(perm); 422 * } 423 * // -> [1, 1, 2, 2] 424 * // -> [1, 2, 1, 2] 425 * // -> [1, 2, 2, 1] 426 * // -> [2, 1, 1, 2] 427 * // -> [2, 1, 2, 1] 428 * // -> [2, 2, 1, 1]}</pre> 429 * 430 * <p><i>Notes:</i> This is an implementation of the algorithm for 431 * Lexicographical Permutations Generation, described in Knuth's "The Art of 432 * Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2. The 433 * iteration order follows the lexicographical order. This means that 434 * the first permutation will be in ascending order, and the last will be in 435 * descending order. 436 * 437 * <p>Elements that compare equal are considered equal and no new permutations 438 * are created by swapping them. 439 * 440 * <p>An empty iterable has only one permutation, which is an empty list. 441 * 442 * @param elements the original iterable whose elements have to be permuted. 443 * @param comparator a comparator for the iterable's elements. 444 * @return an immutable {@link Collection} containing all the different 445 * permutations of the original iterable. 446 * @throws NullPointerException If the specified iterable is null, has any 447 * null elements, or if the specified comparator is null. 448 * @since 12.0 449 */ 450 @Beta 451 public static <E> Collection<List<E>> orderedPermutations( 452 Iterable<E> elements, Comparator<? super E> comparator) { 453 return new OrderedPermutationCollection<E>(elements, comparator); 454 } 455 456 private static final class OrderedPermutationCollection<E> extends AbstractCollection<List<E>> { 457 final ImmutableList<E> inputList; 458 final Comparator<? super E> comparator; 459 final int size; 460 461 OrderedPermutationCollection(Iterable<E> input, Comparator<? super E> comparator) { 462 this.inputList = Ordering.from(comparator).immutableSortedCopy(input); 463 this.comparator = comparator; 464 this.size = calculateSize(inputList, comparator); 465 } 466 467 /** 468 * The number of permutations with repeated elements is calculated as 469 * follows: 470 * <ul> 471 * <li>For an empty list, it is 1 (base case).</li> 472 * <li>When r numbers are added to a list of n-r elements, the number of 473 * permutations is increased by a factor of (n choose r).</li> 474 * </ul> 475 */ 476 private static <E> int calculateSize( 477 List<E> sortedInputList, Comparator<? super E> comparator) { 478 long permutations = 1; 479 int n = 1; 480 int r = 1; 481 while (n < sortedInputList.size()) { 482 int comparison = comparator.compare(sortedInputList.get(n - 1), sortedInputList.get(n)); 483 if (comparison < 0) { 484 // We move to the next non-repeated element. 485 permutations *= binomial(n, r); 486 r = 0; 487 if (!isPositiveInt(permutations)) { 488 return Integer.MAX_VALUE; 489 } 490 } 491 n++; 492 r++; 493 } 494 permutations *= binomial(n, r); 495 if (!isPositiveInt(permutations)) { 496 return Integer.MAX_VALUE; 497 } 498 return (int) permutations; 499 } 500 501 @Override 502 public int size() { 503 return size; 504 } 505 506 @Override 507 public boolean isEmpty() { 508 return false; 509 } 510 511 @Override 512 public Iterator<List<E>> iterator() { 513 return new OrderedPermutationIterator<E>(inputList, comparator); 514 } 515 516 @Override 517 public boolean contains(@Nullable Object obj) { 518 if (obj instanceof List) { 519 List<?> list = (List<?>) obj; 520 return isPermutation(inputList, list); 521 } 522 return false; 523 } 524 525 @Override 526 public String toString() { 527 return "orderedPermutationCollection(" + inputList + ")"; 528 } 529 } 530 531 private static final class OrderedPermutationIterator<E> extends AbstractIterator<List<E>> { 532 533 List<E> nextPermutation; 534 final Comparator<? super E> comparator; 535 536 OrderedPermutationIterator(List<E> list, Comparator<? super E> comparator) { 537 this.nextPermutation = Lists.newArrayList(list); 538 this.comparator = comparator; 539 } 540 541 @Override 542 protected List<E> computeNext() { 543 if (nextPermutation == null) { 544 return endOfData(); 545 } 546 ImmutableList<E> next = ImmutableList.copyOf(nextPermutation); 547 calculateNextPermutation(); 548 return next; 549 } 550 551 void calculateNextPermutation() { 552 int j = findNextJ(); 553 if (j == -1) { 554 nextPermutation = null; 555 return; 556 } 557 558 int l = findNextL(j); 559 Collections.swap(nextPermutation, j, l); 560 int n = nextPermutation.size(); 561 Collections.reverse(nextPermutation.subList(j + 1, n)); 562 } 563 564 int findNextJ() { 565 for (int k = nextPermutation.size() - 2; k >= 0; k--) { 566 if (comparator.compare(nextPermutation.get(k), nextPermutation.get(k + 1)) < 0) { 567 return k; 568 } 569 } 570 return -1; 571 } 572 573 int findNextL(int j) { 574 E ak = nextPermutation.get(j); 575 for (int l = nextPermutation.size() - 1; l > j; l--) { 576 if (comparator.compare(ak, nextPermutation.get(l)) < 0) { 577 return l; 578 } 579 } 580 throw new AssertionError("this statement should be unreachable"); 581 } 582 } 583 584 /** 585 * Returns a {@link Collection} of all the permutations of the specified 586 * {@link Collection}. 587 * 588 * <p><i>Notes:</i> This is an implementation of the Plain Changes algorithm 589 * for permutations generation, described in Knuth's "The Art of Computer 590 * Programming", Volume 4, Chapter 7, Section 7.2.1.2. 591 * 592 * <p>If the input list contains equal elements, some of the generated 593 * permutations will be equal. 594 * 595 * <p>An empty collection has only one permutation, which is an empty list. 596 * 597 * @param elements the original collection whose elements have to be permuted. 598 * @return an immutable {@link Collection} containing all the different 599 * permutations of the original collection. 600 * @throws NullPointerException if the specified collection is null or has any 601 * null elements. 602 * @since 12.0 603 */ 604 @Beta 605 public static <E> Collection<List<E>> permutations(Collection<E> elements) { 606 return new PermutationCollection<E>(ImmutableList.copyOf(elements)); 607 } 608 609 private static final class PermutationCollection<E> extends AbstractCollection<List<E>> { 610 final ImmutableList<E> inputList; 611 612 PermutationCollection(ImmutableList<E> input) { 613 this.inputList = input; 614 } 615 616 @Override 617 public int size() { 618 return IntMath.factorial(inputList.size()); 619 } 620 621 @Override 622 public boolean isEmpty() { 623 return false; 624 } 625 626 @Override 627 public Iterator<List<E>> iterator() { 628 return new PermutationIterator<E>(inputList); 629 } 630 631 @Override 632 public boolean contains(@Nullable Object obj) { 633 if (obj instanceof List) { 634 List<?> list = (List<?>) obj; 635 return isPermutation(inputList, list); 636 } 637 return false; 638 } 639 640 @Override 641 public String toString() { 642 return "permutations(" + inputList + ")"; 643 } 644 } 645 646 private static class PermutationIterator<E> extends AbstractIterator<List<E>> { 647 final List<E> list; 648 final int[] c; 649 final int[] o; 650 int j; 651 652 PermutationIterator(List<E> list) { 653 this.list = new ArrayList<E>(list); 654 int n = list.size(); 655 c = new int[n]; 656 o = new int[n]; 657 Arrays.fill(c, 0); 658 Arrays.fill(o, 1); 659 j = Integer.MAX_VALUE; 660 } 661 662 @Override 663 protected List<E> computeNext() { 664 if (j <= 0) { 665 return endOfData(); 666 } 667 ImmutableList<E> next = ImmutableList.copyOf(list); 668 calculateNextPermutation(); 669 return next; 670 } 671 672 void calculateNextPermutation() { 673 j = list.size() - 1; 674 int s = 0; 675 676 // Handle the special case of an empty list. Skip the calculation of the 677 // next permutation. 678 if (j == -1) { 679 return; 680 } 681 682 while (true) { 683 int q = c[j] + o[j]; 684 if (q < 0) { 685 switchDirection(); 686 continue; 687 } 688 if (q == j + 1) { 689 if (j == 0) { 690 break; 691 } 692 s++; 693 switchDirection(); 694 continue; 695 } 696 697 Collections.swap(list, j - c[j] + s, j - q + s); 698 c[j] = q; 699 break; 700 } 701 } 702 703 void switchDirection() { 704 o[j] = -o[j]; 705 j--; 706 } 707 } 708 709 /** 710 * Returns {@code true} if the second list is a permutation of the first. 711 */ 712 private static boolean isPermutation(List<?> first, List<?> second) { 713 if (first.size() != second.size()) { 714 return false; 715 } 716 Multiset<?> firstMultiset = HashMultiset.create(first); 717 Multiset<?> secondMultiset = HashMultiset.create(second); 718 return firstMultiset.equals(secondMultiset); 719 } 720 721 private static boolean isPositiveInt(long n) { 722 return n >= 0 && n <= Integer.MAX_VALUE; 723 } 724}