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 | NullPointerException e) {
110      return false;
111    }
112  }
113
114  /**
115   * Delegates to {@link Collection#remove}. Returns {@code false} if the
116   * {@code remove} method throws a {@code ClassCastException} or
117   * {@code NullPointerException}.
118   */
119  static boolean safeRemove(Collection<?> collection, @Nullable Object object) {
120    checkNotNull(collection);
121    try {
122      return collection.remove(object);
123    } catch (ClassCastException | NullPointerException e) {
124      return false;
125    }
126  }
127
128  static class FilteredCollection<E> extends AbstractCollection<E> {
129    final Collection<E> unfiltered;
130    final Predicate<? super E> predicate;
131
132    FilteredCollection(Collection<E> unfiltered, Predicate<? super E> predicate) {
133      this.unfiltered = unfiltered;
134      this.predicate = predicate;
135    }
136
137    FilteredCollection<E> createCombined(Predicate<? super E> newPredicate) {
138      return new FilteredCollection<E>(unfiltered, Predicates.<E>and(predicate, newPredicate));
139      // .<E> above needed to compile in JDK 5
140    }
141
142    @Override
143    public boolean add(E element) {
144      checkArgument(predicate.apply(element));
145      return unfiltered.add(element);
146    }
147
148    @Override
149    public boolean addAll(Collection<? extends E> collection) {
150      for (E element : collection) {
151        checkArgument(predicate.apply(element));
152      }
153      return unfiltered.addAll(collection);
154    }
155
156    @Override
157    public void clear() {
158      Iterables.removeIf(unfiltered, predicate);
159    }
160
161    @Override
162    public boolean contains(@Nullable Object element) {
163      if (safeContains(unfiltered, element)) {
164        @SuppressWarnings("unchecked") // element is in unfiltered, so it must be an E
165        E e = (E) element;
166        return predicate.apply(e);
167      }
168      return false;
169    }
170
171    @Override
172    public boolean containsAll(Collection<?> collection) {
173      return containsAllImpl(this, collection);
174    }
175
176    @Override
177    public boolean isEmpty() {
178      return !Iterables.any(unfiltered, predicate);
179    }
180
181    @Override
182    public Iterator<E> iterator() {
183      return Iterators.filter(unfiltered.iterator(), predicate);
184    }
185
186    @Override
187    public boolean remove(Object element) {
188      return contains(element) && unfiltered.remove(element);
189    }
190
191    @Override
192    public boolean removeAll(final Collection<?> collection) {
193      boolean changed = false;
194      Iterator<E> itr = unfiltered.iterator();
195      while (itr.hasNext()) {
196        E e = itr.next();
197        if (predicate.apply(e) && collection.contains(e)) {
198          itr.remove();
199          changed = true;
200        }
201      }
202      return changed;
203    }
204
205    @Override
206    public boolean retainAll(final Collection<?> collection) {
207      boolean changed = false;
208      Iterator<E> itr = unfiltered.iterator();
209      while (itr.hasNext()) {
210        E e = itr.next();
211        if (predicate.apply(e) && !collection.contains(e)) {
212          itr.remove();
213          changed = true;
214        }
215      }
216      return changed;
217    }
218
219    @Override
220    public int size() {
221      int size = 0;
222      for (E e : unfiltered) {
223        if (predicate.apply(e)) {
224          size++;
225        }
226      }
227      return size;
228    }
229
230    @Override
231    public Object[] toArray() {
232      // creating an ArrayList so filtering happens once
233      return Lists.newArrayList(iterator()).toArray();
234    }
235
236    @Override
237    public <T> T[] toArray(T[] array) {
238      return Lists.newArrayList(iterator()).toArray(array);
239    }
240  }
241
242  /**
243   * Returns a collection that applies {@code function} to each element of
244   * {@code fromCollection}. The returned collection is a live view of {@code
245   * fromCollection}; changes to one affect the other.
246   *
247   * <p>The returned collection's {@code add()} and {@code addAll()} methods
248   * throw an {@link UnsupportedOperationException}. All other collection
249   * methods are supported, as long as {@code fromCollection} supports them.
250   *
251   * <p>The returned collection isn't threadsafe or serializable, even if
252   * {@code fromCollection} is.
253   *
254   * <p>When a live view is <i>not</i> needed, it may be faster to copy the
255   * transformed collection and use the copy.
256   *
257   * <p>If the input {@code Collection} is known to be a {@code List}, consider
258   * {@link Lists#transform}. If only an {@code Iterable} is available, use
259   * {@link Iterables#transform}.
260   *
261   * <p><b>{@code Stream} equivalent:</b> {@link java.util.stream.Stream#map Stream.map}.
262   */
263  public static <F, T> Collection<T> transform(
264      Collection<F> fromCollection, Function<? super F, T> function) {
265    return new TransformedCollection<>(fromCollection, function);
266  }
267
268  static class TransformedCollection<F, T> extends AbstractCollection<T> {
269    final Collection<F> fromCollection;
270    final Function<? super F, ? extends T> function;
271
272    TransformedCollection(Collection<F> fromCollection, Function<? super F, ? extends T> function) {
273      this.fromCollection = checkNotNull(fromCollection);
274      this.function = checkNotNull(function);
275    }
276
277    @Override
278    public void clear() {
279      fromCollection.clear();
280    }
281
282    @Override
283    public boolean isEmpty() {
284      return fromCollection.isEmpty();
285    }
286
287    @Override
288    public Iterator<T> iterator() {
289      return Iterators.transform(fromCollection.iterator(), function);
290    }
291
292    @Override
293    public int size() {
294      return fromCollection.size();
295    }
296  }
297
298  /**
299   * Returns {@code true} if the collection {@code self} contains all of the
300   * elements in the collection {@code c}.
301   *
302   * <p>This method iterates over the specified collection {@code c}, checking
303   * each element returned by the iterator in turn to see if it is contained in
304   * the specified collection {@code self}. If all elements are so contained,
305   * {@code true} is returned, otherwise {@code false}.
306   *
307   * @param self a collection which might contain all elements in {@code c}
308   * @param c a collection whose elements might be contained by {@code self}
309   */
310  static boolean containsAllImpl(Collection<?> self, Collection<?> c) {
311    for (Object o : c) {
312      if (!self.contains(o)) {
313        return false;
314      }
315    }
316    return true;
317  }
318
319  /**
320   * An implementation of {@link Collection#toString()}.
321   */
322  static String toStringImpl(final Collection<?> collection) {
323    StringBuilder sb = newStringBuilderForCollection(collection.size()).append('[');
324    boolean first = true;
325    for (Object o : collection) {
326      if (!first) {
327        sb.append(", ");
328      }
329      first = false;
330      if (o == collection) {
331        sb.append("(this Collection)");
332      } else {
333        sb.append(o);
334      }
335    }
336    return sb.append(']').toString();
337  }
338
339  /**
340   * Returns best-effort-sized StringBuilder based on the given collection size.
341   */
342  static StringBuilder newStringBuilderForCollection(int size) {
343    checkNonnegative(size, "size");
344    return new StringBuilder((int) Math.min(size * 8L, Ints.MAX_POWER_OF_TWO));
345  }
346
347  /**
348   * Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557
349   */
350  static <T> Collection<T> cast(Iterable<T> iterable) {
351    return (Collection<T>) iterable;
352  }
353
354  /**
355   * Returns a {@link Collection} of all the permutations of the specified
356   * {@link Iterable}.
357   *
358   * <p><i>Notes:</i> This is an implementation of the algorithm for
359   * Lexicographical Permutations Generation, described in Knuth's "The Art of
360   * Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2. The
361   * iteration order follows the lexicographical order. This means that
362   * the first permutation will be in ascending order, and the last will be in
363   * descending order.
364   *
365   * <p>Duplicate elements are considered equal. For example, the list [1, 1]
366   * will have only one permutation, instead of two. This is why the elements
367   * have to implement {@link Comparable}.
368   *
369   * <p>An empty iterable has only one permutation, which is an empty list.
370   *
371   * <p>This method is equivalent to
372   * {@code Collections2.orderedPermutations(list, Ordering.natural())}.
373   *
374   * @param elements the original iterable whose elements have to be permuted.
375   * @return an immutable {@link Collection} containing all the different
376   *     permutations of the original iterable.
377   * @throws NullPointerException if the specified iterable is null or has any
378   *     null elements.
379   * @since 12.0
380   */
381  @Beta
382  public static <E extends Comparable<? super E>> Collection<List<E>> orderedPermutations(
383      Iterable<E> elements) {
384    return orderedPermutations(elements, Ordering.natural());
385  }
386
387  /**
388   * Returns a {@link Collection} of all the permutations of the specified
389   * {@link Iterable} using the specified {@link Comparator} for establishing
390   * the lexicographical ordering.
391   *
392   * <p>Examples: <pre>   {@code
393   *
394   *   for (List<String> perm : orderedPermutations(asList("b", "c", "a"))) {
395   *     println(perm);
396   *   }
397   *   // -> ["a", "b", "c"]
398   *   // -> ["a", "c", "b"]
399   *   // -> ["b", "a", "c"]
400   *   // -> ["b", "c", "a"]
401   *   // -> ["c", "a", "b"]
402   *   // -> ["c", "b", "a"]
403   *
404   *   for (List<Integer> perm : orderedPermutations(asList(1, 2, 2, 1))) {
405   *     println(perm);
406   *   }
407   *   // -> [1, 1, 2, 2]
408   *   // -> [1, 2, 1, 2]
409   *   // -> [1, 2, 2, 1]
410   *   // -> [2, 1, 1, 2]
411   *   // -> [2, 1, 2, 1]
412   *   // -> [2, 2, 1, 1]}</pre>
413   *
414   * <p><i>Notes:</i> This is an implementation of the algorithm for
415   * Lexicographical Permutations Generation, described in Knuth's "The Art of
416   * Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2. The
417   * iteration order follows the lexicographical order. This means that
418   * the first permutation will be in ascending order, and the last will be in
419   * descending order.
420   *
421   * <p>Elements that compare equal are considered equal and no new permutations
422   * are created by swapping them.
423   *
424   * <p>An empty iterable has only one permutation, which is an empty list.
425   *
426   * @param elements the original iterable whose elements have to be permuted.
427   * @param comparator a comparator for the iterable's elements.
428   * @return an immutable {@link Collection} containing all the different
429   *     permutations of the original iterable.
430   * @throws NullPointerException If the specified iterable is null, has any
431   *     null elements, or if the specified comparator is null.
432   * @since 12.0
433   */
434  @Beta
435  public static <E> Collection<List<E>> orderedPermutations(
436      Iterable<E> elements, Comparator<? super E> comparator) {
437    return new OrderedPermutationCollection<E>(elements, comparator);
438  }
439
440  private static final class OrderedPermutationCollection<E> extends AbstractCollection<List<E>> {
441    final ImmutableList<E> inputList;
442    final Comparator<? super E> comparator;
443    final int size;
444
445    OrderedPermutationCollection(Iterable<E> input, Comparator<? super E> comparator) {
446      this.inputList = ImmutableList.sortedCopyOf(comparator, input);
447      this.comparator = comparator;
448      this.size = calculateSize(inputList, comparator);
449    }
450
451    /**
452     * The number of permutations with repeated elements is calculated as
453     * follows:
454     * <ul>
455     * <li>For an empty list, it is 1 (base case).</li>
456     * <li>When r numbers are added to a list of n-r elements, the number of
457     * permutations is increased by a factor of (n choose r).</li>
458     * </ul>
459     */
460    private static <E> int calculateSize(
461        List<E> sortedInputList, Comparator<? super E> comparator) {
462      int permutations = 1;
463      int n = 1;
464      int r = 1;
465      while (n < sortedInputList.size()) {
466        int comparison = comparator.compare(sortedInputList.get(n - 1), sortedInputList.get(n));
467        if (comparison < 0) {
468          // We move to the next non-repeated element.
469          permutations = IntMath.saturatedMultiply(permutations, IntMath.binomial(n, r));
470          r = 0;
471          if (permutations == Integer.MAX_VALUE) {
472            return Integer.MAX_VALUE;
473          }
474        }
475        n++;
476        r++;
477      }
478      return IntMath.saturatedMultiply(permutations, IntMath.binomial(n, r));
479    }
480
481    @Override
482    public int size() {
483      return size;
484    }
485
486    @Override
487    public boolean isEmpty() {
488      return false;
489    }
490
491    @Override
492    public Iterator<List<E>> iterator() {
493      return new OrderedPermutationIterator<E>(inputList, comparator);
494    }
495
496    @Override
497    public boolean contains(@Nullable Object obj) {
498      if (obj instanceof List) {
499        List<?> list = (List<?>) obj;
500        return isPermutation(inputList, list);
501      }
502      return false;
503    }
504
505    @Override
506    public String toString() {
507      return "orderedPermutationCollection(" + inputList + ")";
508    }
509  }
510
511  private static final class OrderedPermutationIterator<E> extends AbstractIterator<List<E>> {
512
513    List<E> nextPermutation;
514    final Comparator<? super E> comparator;
515
516    OrderedPermutationIterator(List<E> list, Comparator<? super E> comparator) {
517      this.nextPermutation = Lists.newArrayList(list);
518      this.comparator = comparator;
519    }
520
521    @Override
522    protected List<E> computeNext() {
523      if (nextPermutation == null) {
524        return endOfData();
525      }
526      ImmutableList<E> next = ImmutableList.copyOf(nextPermutation);
527      calculateNextPermutation();
528      return next;
529    }
530
531    void calculateNextPermutation() {
532      int j = findNextJ();
533      if (j == -1) {
534        nextPermutation = null;
535        return;
536      }
537
538      int l = findNextL(j);
539      Collections.swap(nextPermutation, j, l);
540      int n = nextPermutation.size();
541      Collections.reverse(nextPermutation.subList(j + 1, n));
542    }
543
544    int findNextJ() {
545      for (int k = nextPermutation.size() - 2; k >= 0; k--) {
546        if (comparator.compare(nextPermutation.get(k), nextPermutation.get(k + 1)) < 0) {
547          return k;
548        }
549      }
550      return -1;
551    }
552
553    int findNextL(int j) {
554      E ak = nextPermutation.get(j);
555      for (int l = nextPermutation.size() - 1; l > j; l--) {
556        if (comparator.compare(ak, nextPermutation.get(l)) < 0) {
557          return l;
558        }
559      }
560      throw new AssertionError("this statement should be unreachable");
561    }
562  }
563
564  /**
565   * Returns a {@link Collection} of all the permutations of the specified
566   * {@link Collection}.
567   *
568   * <p><i>Notes:</i> This is an implementation of the Plain Changes algorithm
569   * for permutations generation, described in Knuth's "The Art of Computer
570   * Programming", Volume 4, Chapter 7, Section 7.2.1.2.
571   *
572   * <p>If the input list contains equal elements, some of the generated
573   * permutations will be equal.
574   *
575   * <p>An empty collection has only one permutation, which is an empty list.
576   *
577   * @param elements the original collection whose elements have to be permuted.
578   * @return an immutable {@link Collection} containing all the different
579   *     permutations of the original collection.
580   * @throws NullPointerException if the specified collection is null or has any
581   *     null elements.
582   * @since 12.0
583   */
584  @Beta
585  public static <E> Collection<List<E>> permutations(Collection<E> elements) {
586    return new PermutationCollection<E>(ImmutableList.copyOf(elements));
587  }
588
589  private static final class PermutationCollection<E> extends AbstractCollection<List<E>> {
590    final ImmutableList<E> inputList;
591
592    PermutationCollection(ImmutableList<E> input) {
593      this.inputList = input;
594    }
595
596    @Override
597    public int size() {
598      return IntMath.factorial(inputList.size());
599    }
600
601    @Override
602    public boolean isEmpty() {
603      return false;
604    }
605
606    @Override
607    public Iterator<List<E>> iterator() {
608      return new PermutationIterator<E>(inputList);
609    }
610
611    @Override
612    public boolean contains(@Nullable Object obj) {
613      if (obj instanceof List) {
614        List<?> list = (List<?>) obj;
615        return isPermutation(inputList, list);
616      }
617      return false;
618    }
619
620    @Override
621    public String toString() {
622      return "permutations(" + inputList + ")";
623    }
624  }
625
626  private static class PermutationIterator<E> extends AbstractIterator<List<E>> {
627    final List<E> list;
628    final int[] c;
629    final int[] o;
630    int j;
631
632    PermutationIterator(List<E> list) {
633      this.list = new ArrayList<E>(list);
634      int n = list.size();
635      c = new int[n];
636      o = new int[n];
637      Arrays.fill(c, 0);
638      Arrays.fill(o, 1);
639      j = Integer.MAX_VALUE;
640    }
641
642    @Override
643    protected List<E> computeNext() {
644      if (j <= 0) {
645        return endOfData();
646      }
647      ImmutableList<E> next = ImmutableList.copyOf(list);
648      calculateNextPermutation();
649      return next;
650    }
651
652    void calculateNextPermutation() {
653      j = list.size() - 1;
654      int s = 0;
655
656      // Handle the special case of an empty list. Skip the calculation of the
657      // next permutation.
658      if (j == -1) {
659        return;
660      }
661
662      while (true) {
663        int q = c[j] + o[j];
664        if (q < 0) {
665          switchDirection();
666          continue;
667        }
668        if (q == j + 1) {
669          if (j == 0) {
670            break;
671          }
672          s++;
673          switchDirection();
674          continue;
675        }
676
677        Collections.swap(list, j - c[j] + s, j - q + s);
678        c[j] = q;
679        break;
680      }
681    }
682
683    void switchDirection() {
684      o[j] = -o[j];
685      j--;
686    }
687  }
688
689  /**
690   * Returns {@code true} if the second list is a permutation of the first.
691   */
692  private static boolean isPermutation(List<?> first, List<?> second) {
693    return first.size() == second.size() && counts(first).equals(counts(second));
694  }
695  
696  private static <E> Set<Multiset.Entry<E>> counts(Collection<E> collection) {
697    AbstractObjectCountMap<E> map = new ObjectCountHashMap<>();
698    for (E e : collection) {
699      map.put(e, map.get(e) + 1);
700    }
701    return map.entrySet();
702  }
703}