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