001/*
002 * Copyright (C) 2007 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.collect.CollectPreconditions.checkRemove;
023
024import com.google.common.annotations.Beta;
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.base.Objects;
027import com.google.common.base.Predicate;
028import com.google.common.base.Predicates;
029import com.google.common.collect.Multiset.Entry;
030import com.google.common.math.IntMath;
031import com.google.common.primitives.Ints;
032import com.google.errorprone.annotations.CanIgnoreReturnValue;
033import java.io.Serializable;
034import java.util.Collection;
035import java.util.Collections;
036import java.util.Iterator;
037import java.util.List;
038import java.util.NoSuchElementException;
039import java.util.Set;
040import javax.annotation.Nullable;
041
042/**
043 * Provides static utility methods for creating and working with {@link
044 * Multiset} instances.
045 *
046 * <p>See the Guava User Guide article on <a href=
047 * "https://github.com/google/guava/wiki/CollectionUtilitiesExplained#multisets">
048 * {@code Multisets}</a>.
049 *
050 * @author Kevin Bourrillion
051 * @author Mike Bostock
052 * @author Louis Wasserman
053 * @since 2.0
054 */
055@GwtCompatible
056public final class Multisets {
057  private Multisets() {}
058
059  /**
060   * Returns an unmodifiable view of the specified multiset. Query operations on
061   * the returned multiset "read through" to the specified multiset, and
062   * attempts to modify the returned multiset result in an
063   * {@link UnsupportedOperationException}.
064   *
065   * <p>The returned multiset will be serializable if the specified multiset is
066   * serializable.
067   *
068   * @param multiset the multiset for which an unmodifiable view is to be
069   *     generated
070   * @return an unmodifiable view of the multiset
071   */
072  public static <E> Multiset<E> unmodifiableMultiset(Multiset<? extends E> multiset) {
073    if (multiset instanceof UnmodifiableMultiset || multiset instanceof ImmutableMultiset) {
074      @SuppressWarnings("unchecked") // Since it's unmodifiable, the covariant cast is safe
075      Multiset<E> result = (Multiset<E>) multiset;
076      return result;
077    }
078    return new UnmodifiableMultiset<E>(checkNotNull(multiset));
079  }
080
081  /**
082   * Simply returns its argument.
083   *
084   * @deprecated no need to use this
085   * @since 10.0
086   */
087  @Deprecated
088  public static <E> Multiset<E> unmodifiableMultiset(ImmutableMultiset<E> multiset) {
089    return checkNotNull(multiset);
090  }
091
092  static class UnmodifiableMultiset<E> extends ForwardingMultiset<E> implements Serializable {
093    final Multiset<? extends E> delegate;
094
095    UnmodifiableMultiset(Multiset<? extends E> delegate) {
096      this.delegate = delegate;
097    }
098
099    @SuppressWarnings("unchecked")
100    @Override
101    protected Multiset<E> delegate() {
102      // This is safe because all non-covariant methods are overriden
103      return (Multiset<E>) delegate;
104    }
105
106    transient Set<E> elementSet;
107
108    Set<E> createElementSet() {
109      return Collections.<E>unmodifiableSet(delegate.elementSet());
110    }
111
112    @Override
113    public Set<E> elementSet() {
114      Set<E> es = elementSet;
115      return (es == null) ? elementSet = createElementSet() : es;
116    }
117
118    transient Set<Multiset.Entry<E>> entrySet;
119
120    @SuppressWarnings("unchecked")
121    @Override
122    public Set<Multiset.Entry<E>> entrySet() {
123      Set<Multiset.Entry<E>> es = entrySet;
124      return (es == null)
125          // Safe because the returned set is made unmodifiable and Entry
126          // itself is readonly
127          ? entrySet = (Set) Collections.unmodifiableSet(delegate.entrySet())
128          : es;
129    }
130
131    @Override
132    public Iterator<E> iterator() {
133      return Iterators.<E>unmodifiableIterator(delegate.iterator());
134    }
135
136    @Override
137    public boolean add(E element) {
138      throw new UnsupportedOperationException();
139    }
140
141    @Override
142    public int add(E element, int occurences) {
143      throw new UnsupportedOperationException();
144    }
145
146    @Override
147    public boolean addAll(Collection<? extends E> elementsToAdd) {
148      throw new UnsupportedOperationException();
149    }
150
151    @Override
152    public boolean remove(Object element) {
153      throw new UnsupportedOperationException();
154    }
155
156    @Override
157    public int remove(Object element, int occurrences) {
158      throw new UnsupportedOperationException();
159    }
160
161    @Override
162    public boolean removeAll(Collection<?> elementsToRemove) {
163      throw new UnsupportedOperationException();
164    }
165
166    @Override
167    public boolean retainAll(Collection<?> elementsToRetain) {
168      throw new UnsupportedOperationException();
169    }
170
171    @Override
172    public void clear() {
173      throw new UnsupportedOperationException();
174    }
175
176    @Override
177    public int setCount(E element, int count) {
178      throw new UnsupportedOperationException();
179    }
180
181    @Override
182    public boolean setCount(E element, int oldCount, int newCount) {
183      throw new UnsupportedOperationException();
184    }
185
186    private static final long serialVersionUID = 0;
187  }
188
189  /**
190   * Returns an unmodifiable view of the specified sorted multiset. Query
191   * operations on the returned multiset "read through" to the specified
192   * multiset, and attempts to modify the returned multiset result in an {@link
193   * UnsupportedOperationException}.
194   *
195   * <p>The returned multiset will be serializable if the specified multiset is
196   * serializable.
197   *
198   * @param sortedMultiset the sorted multiset for which an unmodifiable view is
199   *     to be generated
200   * @return an unmodifiable view of the multiset
201   * @since 11.0
202   */
203  @Beta
204  public static <E> SortedMultiset<E> unmodifiableSortedMultiset(SortedMultiset<E> sortedMultiset) {
205    // it's in its own file so it can be emulated for GWT
206    return new UnmodifiableSortedMultiset<E>(checkNotNull(sortedMultiset));
207  }
208
209  /**
210   * Returns an immutable multiset entry with the specified element and count.
211   * The entry will be serializable if {@code e} is.
212   *
213   * @param e the element to be associated with the returned entry
214   * @param n the count to be associated with the returned entry
215   * @throws IllegalArgumentException if {@code n} is negative
216   */
217  public static <E> Multiset.Entry<E> immutableEntry(@Nullable E e, int n) {
218    return new ImmutableEntry<E>(e, n);
219  }
220
221  static class ImmutableEntry<E> extends AbstractEntry<E> implements Serializable {
222    @Nullable private final E element;
223    private final int count;
224
225    ImmutableEntry(@Nullable E element, int count) {
226      this.element = element;
227      this.count = count;
228      checkNonnegative(count, "count");
229    }
230
231    @Override
232    @Nullable
233    public final E getElement() {
234      return element;
235    }
236
237    @Override
238    public final int getCount() {
239      return count;
240    }
241
242    public ImmutableEntry<E> nextInBucket() {
243      return null;
244    }
245
246    private static final long serialVersionUID = 0;
247  }
248
249  /**
250   * Returns a view of the elements of {@code unfiltered} that satisfy a predicate. The returned
251   * multiset is a live view of {@code unfiltered}; changes to one affect the other.
252   *
253   * <p>The resulting multiset's iterators, and those of its {@code entrySet()} and
254   * {@code elementSet()}, do not support {@code remove()}.  However, all other multiset methods
255   * supported by {@code unfiltered} are supported by the returned multiset. When given an element
256   * that doesn't satisfy the predicate, the multiset's {@code add()} and {@code addAll()} methods
257   * throw an {@link IllegalArgumentException}. When methods such as {@code removeAll()} and
258   * {@code clear()} are called on the filtered multiset, only elements that satisfy the filter
259   * will be removed from the underlying multiset.
260   *
261   * <p>The returned multiset isn't threadsafe or serializable, even if {@code unfiltered} is.
262   *
263   * <p>Many of the filtered multiset's methods, such as {@code size()}, iterate across every
264   * element in the underlying multiset and determine which elements satisfy the filter. When a
265   * live view is <i>not</i> needed, it may be faster to copy the returned multiset and use the
266   * copy.
267   *
268   * <p><b>Warning:</b> {@code predicate} must be <i>consistent with equals</i>, as documented at
269   * {@link Predicate#apply}. Do not provide a predicate such as
270   * {@code Predicates.instanceOf(ArrayList.class)}, which is inconsistent with equals. (See
271   * {@link Iterables#filter(Iterable, Class)} for related functionality.)
272   *
273   * @since 14.0
274   */
275  @Beta
276  public static <E> Multiset<E> filter(Multiset<E> unfiltered, Predicate<? super E> predicate) {
277    if (unfiltered instanceof FilteredMultiset) {
278      // Support clear(), removeAll(), and retainAll() when filtering a filtered
279      // collection.
280      FilteredMultiset<E> filtered = (FilteredMultiset<E>) unfiltered;
281      Predicate<E> combinedPredicate = Predicates.<E>and(filtered.predicate, predicate);
282      return new FilteredMultiset<E>(filtered.unfiltered, combinedPredicate);
283    }
284    return new FilteredMultiset<E>(unfiltered, predicate);
285  }
286
287  private static final class FilteredMultiset<E> extends AbstractMultiset<E> {
288    final Multiset<E> unfiltered;
289    final Predicate<? super E> predicate;
290
291    FilteredMultiset(Multiset<E> unfiltered, Predicate<? super E> predicate) {
292      this.unfiltered = checkNotNull(unfiltered);
293      this.predicate = checkNotNull(predicate);
294    }
295
296    @Override
297    public UnmodifiableIterator<E> iterator() {
298      return Iterators.filter(unfiltered.iterator(), predicate);
299    }
300
301    @Override
302    Set<E> createElementSet() {
303      return Sets.filter(unfiltered.elementSet(), predicate);
304    }
305
306    @Override
307    Set<Entry<E>> createEntrySet() {
308      return Sets.filter(
309          unfiltered.entrySet(),
310          new Predicate<Entry<E>>() {
311            @Override
312            public boolean apply(Entry<E> entry) {
313              return predicate.apply(entry.getElement());
314            }
315          });
316    }
317
318    @Override
319    Iterator<Entry<E>> entryIterator() {
320      throw new AssertionError("should never be called");
321    }
322
323    @Override
324    int distinctElements() {
325      return elementSet().size();
326    }
327
328    @Override
329    public int count(@Nullable Object element) {
330      int count = unfiltered.count(element);
331      if (count > 0) {
332        @SuppressWarnings("unchecked") // element is equal to an E
333        E e = (E) element;
334        return predicate.apply(e) ? count : 0;
335      }
336      return 0;
337    }
338
339    @Override
340    public int add(@Nullable E element, int occurrences) {
341      checkArgument(
342          predicate.apply(element), "Element %s does not match predicate %s", element, predicate);
343      return unfiltered.add(element, occurrences);
344    }
345
346    @Override
347    public int remove(@Nullable Object element, int occurrences) {
348      checkNonnegative(occurrences, "occurrences");
349      if (occurrences == 0) {
350        return count(element);
351      } else {
352        return contains(element) ? unfiltered.remove(element, occurrences) : 0;
353      }
354    }
355
356    @Override
357    public void clear() {
358      elementSet().clear();
359    }
360  }
361
362  /**
363   * Returns the expected number of distinct elements given the specified
364   * elements. The number of distinct elements is only computed if {@code
365   * elements} is an instance of {@code Multiset}; otherwise the default value
366   * of 11 is returned.
367   */
368  static int inferDistinctElements(Iterable<?> elements) {
369    if (elements instanceof Multiset) {
370      return ((Multiset<?>) elements).elementSet().size();
371    }
372    return 11; // initial capacity will be rounded up to 16
373  }
374
375  /**
376   * Returns an unmodifiable view of the union of two multisets.
377   * In the returned multiset, the count of each element is the <i>maximum</i>
378   * of its counts in the two backing multisets. The iteration order of the
379   * returned multiset matches that of the element set of {@code multiset1}
380   * followed by the members of the element set of {@code multiset2} that are
381   * not contained in {@code multiset1}, with repeated occurrences of the same
382   * element appearing consecutively.
383   *
384   * <p>Results are undefined if {@code multiset1} and {@code multiset2} are
385   * based on different equivalence relations (as {@code HashMultiset} and
386   * {@code TreeMultiset} are).
387   *
388   * @since 14.0
389   */
390  @Beta
391  public static <E> Multiset<E> union(
392      final Multiset<? extends E> multiset1, final Multiset<? extends E> multiset2) {
393    checkNotNull(multiset1);
394    checkNotNull(multiset2);
395
396    return new AbstractMultiset<E>() {
397      @Override
398      public boolean contains(@Nullable Object element) {
399        return multiset1.contains(element) || multiset2.contains(element);
400      }
401
402      @Override
403      public boolean isEmpty() {
404        return multiset1.isEmpty() && multiset2.isEmpty();
405      }
406
407      @Override
408      public int count(Object element) {
409        return Math.max(multiset1.count(element), multiset2.count(element));
410      }
411
412      @Override
413      Set<E> createElementSet() {
414        return Sets.union(multiset1.elementSet(), multiset2.elementSet());
415      }
416
417      @Override
418      Iterator<Entry<E>> entryIterator() {
419        final Iterator<? extends Entry<? extends E>> iterator1 = multiset1.entrySet().iterator();
420        final Iterator<? extends Entry<? extends E>> iterator2 = multiset2.entrySet().iterator();
421        // TODO(lowasser): consider making the entries live views
422        return new AbstractIterator<Entry<E>>() {
423          @Override
424          protected Entry<E> computeNext() {
425            if (iterator1.hasNext()) {
426              Entry<? extends E> entry1 = iterator1.next();
427              E element = entry1.getElement();
428              int count = Math.max(entry1.getCount(), multiset2.count(element));
429              return immutableEntry(element, count);
430            }
431            while (iterator2.hasNext()) {
432              Entry<? extends E> entry2 = iterator2.next();
433              E element = entry2.getElement();
434              if (!multiset1.contains(element)) {
435                return immutableEntry(element, entry2.getCount());
436              }
437            }
438            return endOfData();
439          }
440        };
441      }
442
443      @Override
444      int distinctElements() {
445        return elementSet().size();
446      }
447    };
448  }
449
450  /**
451   * Returns an unmodifiable view of the intersection of two multisets.
452   * In the returned multiset, the count of each element is the <i>minimum</i>
453   * of its counts in the two backing multisets, with elements that would have
454   * a count of 0 not included. The iteration order of the returned multiset
455   * matches that of the element set of {@code multiset1}, with repeated
456   * occurrences of the same element appearing consecutively.
457   *
458   * <p>Results are undefined if {@code multiset1} and {@code multiset2} are
459   * based on different equivalence relations (as {@code HashMultiset} and
460   * {@code TreeMultiset} are).
461   *
462   * @since 2.0
463   */
464  public static <E> Multiset<E> intersection(
465      final Multiset<E> multiset1, final Multiset<?> multiset2) {
466    checkNotNull(multiset1);
467    checkNotNull(multiset2);
468
469    return new AbstractMultiset<E>() {
470      @Override
471      public int count(Object element) {
472        int count1 = multiset1.count(element);
473        return (count1 == 0) ? 0 : Math.min(count1, multiset2.count(element));
474      }
475
476      @Override
477      Set<E> createElementSet() {
478        return Sets.intersection(multiset1.elementSet(), multiset2.elementSet());
479      }
480
481      @Override
482      Iterator<Entry<E>> entryIterator() {
483        final Iterator<Entry<E>> iterator1 = multiset1.entrySet().iterator();
484        // TODO(lowasser): consider making the entries live views
485        return new AbstractIterator<Entry<E>>() {
486          @Override
487          protected Entry<E> computeNext() {
488            while (iterator1.hasNext()) {
489              Entry<E> entry1 = iterator1.next();
490              E element = entry1.getElement();
491              int count = Math.min(entry1.getCount(), multiset2.count(element));
492              if (count > 0) {
493                return immutableEntry(element, count);
494              }
495            }
496            return endOfData();
497          }
498        };
499      }
500
501      @Override
502      int distinctElements() {
503        return elementSet().size();
504      }
505    };
506  }
507
508  /**
509   * Returns an unmodifiable view of the sum of two multisets.
510   * In the returned multiset, the count of each element is the <i>sum</i> of
511   * its counts in the two backing multisets. The iteration order of the
512   * returned multiset matches that of the element set of {@code multiset1}
513   * followed by the members of the element set of {@code multiset2} that
514   * are not contained in {@code multiset1}, with repeated occurrences of the
515   * same element appearing consecutively.
516   *
517   * <p>Results are undefined if {@code multiset1} and {@code multiset2} are
518   * based on different equivalence relations (as {@code HashMultiset} and
519   * {@code TreeMultiset} are).
520   *
521   * @since 14.0
522   */
523  @Beta
524  public static <E> Multiset<E> sum(
525      final Multiset<? extends E> multiset1, final Multiset<? extends E> multiset2) {
526    checkNotNull(multiset1);
527    checkNotNull(multiset2);
528
529    // TODO(lowasser): consider making the entries live views
530    return new AbstractMultiset<E>() {
531      @Override
532      public boolean contains(@Nullable Object element) {
533        return multiset1.contains(element) || multiset2.contains(element);
534      }
535
536      @Override
537      public boolean isEmpty() {
538        return multiset1.isEmpty() && multiset2.isEmpty();
539      }
540
541      @Override
542      public int size() {
543        return IntMath.saturatedAdd(multiset1.size(), multiset2.size());
544      }
545
546      @Override
547      public int count(Object element) {
548        return multiset1.count(element) + multiset2.count(element);
549      }
550
551      @Override
552      Set<E> createElementSet() {
553        return Sets.union(multiset1.elementSet(), multiset2.elementSet());
554      }
555
556      @Override
557      Iterator<Entry<E>> entryIterator() {
558        final Iterator<? extends Entry<? extends E>> iterator1 = multiset1.entrySet().iterator();
559        final Iterator<? extends Entry<? extends E>> iterator2 = multiset2.entrySet().iterator();
560        return new AbstractIterator<Entry<E>>() {
561          @Override
562          protected Entry<E> computeNext() {
563            if (iterator1.hasNext()) {
564              Entry<? extends E> entry1 = iterator1.next();
565              E element = entry1.getElement();
566              int count = entry1.getCount() + multiset2.count(element);
567              return immutableEntry(element, count);
568            }
569            while (iterator2.hasNext()) {
570              Entry<? extends E> entry2 = iterator2.next();
571              E element = entry2.getElement();
572              if (!multiset1.contains(element)) {
573                return immutableEntry(element, entry2.getCount());
574              }
575            }
576            return endOfData();
577          }
578        };
579      }
580
581      @Override
582      int distinctElements() {
583        return elementSet().size();
584      }
585    };
586  }
587
588  /**
589   * Returns an unmodifiable view of the difference of two multisets.
590   * In the returned multiset, the count of each element is the result of the
591   * <i>zero-truncated subtraction</i> of its count in the second multiset from
592   * its count in the first multiset, with elements that would have a count of
593   * 0 not included. The iteration order of the returned multiset matches that
594   * of the element set of {@code multiset1}, with repeated occurrences of the
595   * same element appearing consecutively.
596   *
597   * <p>Results are undefined if {@code multiset1} and {@code multiset2} are
598   * based on different equivalence relations (as {@code HashMultiset} and
599   * {@code TreeMultiset} are).
600   *
601   * @since 14.0
602   */
603  @Beta
604  public static <E> Multiset<E> difference(
605      final Multiset<E> multiset1, final Multiset<?> multiset2) {
606    checkNotNull(multiset1);
607    checkNotNull(multiset2);
608
609    // TODO(lowasser): consider making the entries live views
610    return new AbstractMultiset<E>() {
611      @Override
612      public int count(@Nullable Object element) {
613        int count1 = multiset1.count(element);
614        return (count1 == 0) ? 0 : Math.max(0, count1 - multiset2.count(element));
615      }
616
617      @Override
618      Iterator<Entry<E>> entryIterator() {
619        final Iterator<Entry<E>> iterator1 = multiset1.entrySet().iterator();
620        return new AbstractIterator<Entry<E>>() {
621          @Override
622          protected Entry<E> computeNext() {
623            while (iterator1.hasNext()) {
624              Entry<E> entry1 = iterator1.next();
625              E element = entry1.getElement();
626              int count = entry1.getCount() - multiset2.count(element);
627              if (count > 0) {
628                return immutableEntry(element, count);
629              }
630            }
631            return endOfData();
632          }
633        };
634      }
635
636      @Override
637      int distinctElements() {
638        return Iterators.size(entryIterator());
639      }
640    };
641  }
642
643  /**
644   * Returns {@code true} if {@code subMultiset.count(o) <=
645   * superMultiset.count(o)} for all {@code o}.
646   *
647   * @since 10.0
648   */
649  @CanIgnoreReturnValue
650  public static boolean containsOccurrences(Multiset<?> superMultiset, Multiset<?> subMultiset) {
651    checkNotNull(superMultiset);
652    checkNotNull(subMultiset);
653    for (Entry<?> entry : subMultiset.entrySet()) {
654      int superCount = superMultiset.count(entry.getElement());
655      if (superCount < entry.getCount()) {
656        return false;
657      }
658    }
659    return true;
660  }
661
662  /**
663   * Modifies {@code multisetToModify} so that its count for an element
664   * {@code e} is at most {@code multisetToRetain.count(e)}.
665   *
666   * <p>To be precise, {@code multisetToModify.count(e)} is set to
667   * {@code Math.min(multisetToModify.count(e),
668   * multisetToRetain.count(e))}. This is similar to
669   * {@link #intersection(Multiset, Multiset) intersection}
670   * {@code (multisetToModify, multisetToRetain)}, but mutates
671   * {@code multisetToModify} instead of returning a view.
672   *
673   * <p>In contrast, {@code multisetToModify.retainAll(multisetToRetain)} keeps
674   * all occurrences of elements that appear at all in {@code
675   * multisetToRetain}, and deletes all occurrences of all other elements.
676   *
677   * @return {@code true} if {@code multisetToModify} was changed as a result
678   *         of this operation
679   * @since 10.0
680   */
681  @CanIgnoreReturnValue
682  public static boolean retainOccurrences(
683      Multiset<?> multisetToModify, Multiset<?> multisetToRetain) {
684    return retainOccurrencesImpl(multisetToModify, multisetToRetain);
685  }
686
687  /**
688   * Delegate implementation which cares about the element type.
689   */
690  private static <E> boolean retainOccurrencesImpl(
691      Multiset<E> multisetToModify, Multiset<?> occurrencesToRetain) {
692    checkNotNull(multisetToModify);
693    checkNotNull(occurrencesToRetain);
694    // Avoiding ConcurrentModificationExceptions is tricky.
695    Iterator<Entry<E>> entryIterator = multisetToModify.entrySet().iterator();
696    boolean changed = false;
697    while (entryIterator.hasNext()) {
698      Entry<E> entry = entryIterator.next();
699      int retainCount = occurrencesToRetain.count(entry.getElement());
700      if (retainCount == 0) {
701        entryIterator.remove();
702        changed = true;
703      } else if (retainCount < entry.getCount()) {
704        multisetToModify.setCount(entry.getElement(), retainCount);
705        changed = true;
706      }
707    }
708    return changed;
709  }
710
711  /**
712   * For each occurrence of an element {@code e} in {@code occurrencesToRemove},
713   * removes one occurrence of {@code e} in {@code multisetToModify}.
714   *
715   * <p>Equivalently, this method modifies {@code multisetToModify} so that
716   * {@code multisetToModify.count(e)} is set to
717   * {@code Math.max(0, multisetToModify.count(e) -
718   * Iterables.frequency(occurrencesToRemove, e))}.
719   *
720   * <p>This is <i>not</i> the same as {@code multisetToModify.}
721   * {@link Multiset#removeAll removeAll}{@code (occurrencesToRemove)}, which
722   * removes all occurrences of elements that appear in
723   * {@code occurrencesToRemove}. However, this operation <i>is</i> equivalent
724   * to, albeit sometimes more efficient than, the following: <pre>   {@code
725   *
726   *   for (E e : occurrencesToRemove) {
727   *     multisetToModify.remove(e);
728   *   }}</pre>
729   *
730   * @return {@code true} if {@code multisetToModify} was changed as a result of
731   *         this operation
732   * @since 18.0 (present in 10.0 with a requirement that the second parameter
733   *     be a {@code Multiset})
734   */
735  @CanIgnoreReturnValue
736  public static boolean removeOccurrences(
737      Multiset<?> multisetToModify, Iterable<?> occurrencesToRemove) {
738    if (occurrencesToRemove instanceof Multiset) {
739      return removeOccurrences(multisetToModify, (Multiset<?>) occurrencesToRemove);
740    } else {
741      checkNotNull(multisetToModify);
742      checkNotNull(occurrencesToRemove);
743      boolean changed = false;
744      for (Object o : occurrencesToRemove) {
745        changed |= multisetToModify.remove(o);
746      }
747      return changed;
748    }
749  }
750
751  /**
752   * For each occurrence of an element {@code e} in {@code occurrencesToRemove},
753   * removes one occurrence of {@code e} in {@code multisetToModify}.
754   *
755   * <p>Equivalently, this method modifies {@code multisetToModify} so that
756   * {@code multisetToModify.count(e)} is set to
757   * {@code Math.max(0, multisetToModify.count(e) -
758   * occurrencesToRemove.count(e))}.
759   *
760   * <p>This is <i>not</i> the same as {@code multisetToModify.}
761   * {@link Multiset#removeAll removeAll}{@code (occurrencesToRemove)}, which
762   * removes all occurrences of elements that appear in
763   * {@code occurrencesToRemove}. However, this operation <i>is</i> equivalent
764   * to, albeit sometimes more efficient than, the following: <pre>   {@code
765   *
766   *   for (E e : occurrencesToRemove) {
767   *     multisetToModify.remove(e);
768   *   }}</pre>
769   *
770   * @return {@code true} if {@code multisetToModify} was changed as a result of
771   *         this operation
772   * @since 10.0 (missing in 18.0 when only the overload taking an {@code Iterable} was present)
773   */
774  @CanIgnoreReturnValue
775  public static boolean removeOccurrences(
776      Multiset<?> multisetToModify, Multiset<?> occurrencesToRemove) {
777    checkNotNull(multisetToModify);
778    checkNotNull(occurrencesToRemove);
779
780    boolean changed = false;
781    Iterator<? extends Entry<?>> entryIterator = multisetToModify.entrySet().iterator();
782    while (entryIterator.hasNext()) {
783      Entry<?> entry = entryIterator.next();
784      int removeCount = occurrencesToRemove.count(entry.getElement());
785      if (removeCount >= entry.getCount()) {
786        entryIterator.remove();
787        changed = true;
788      } else if (removeCount > 0) {
789        multisetToModify.remove(entry.getElement(), removeCount);
790        changed = true;
791      }
792    }
793    return changed;
794  }
795
796  /**
797   * Implementation of the {@code equals}, {@code hashCode}, and
798   * {@code toString} methods of {@link Multiset.Entry}.
799   */
800  abstract static class AbstractEntry<E> implements Multiset.Entry<E> {
801    /**
802     * Indicates whether an object equals this entry, following the behavior
803     * specified in {@link Multiset.Entry#equals}.
804     */
805    @Override
806    public boolean equals(@Nullable Object object) {
807      if (object instanceof Multiset.Entry) {
808        Multiset.Entry<?> that = (Multiset.Entry<?>) object;
809        return this.getCount() == that.getCount()
810            && Objects.equal(this.getElement(), that.getElement());
811      }
812      return false;
813    }
814
815    /**
816     * Return this entry's hash code, following the behavior specified in
817     * {@link Multiset.Entry#hashCode}.
818     */
819    @Override
820    public int hashCode() {
821      E e = getElement();
822      return ((e == null) ? 0 : e.hashCode()) ^ getCount();
823    }
824
825    /**
826     * Returns a string representation of this multiset entry. The string
827     * representation consists of the associated element if the associated count
828     * is one, and otherwise the associated element followed by the characters
829     * " x " (space, x and space) followed by the count. Elements and counts are
830     * converted to strings as by {@code String.valueOf}.
831     */
832    @Override
833    public String toString() {
834      String text = String.valueOf(getElement());
835      int n = getCount();
836      return (n == 1) ? text : (text + " x " + n);
837    }
838  }
839
840  /**
841   * An implementation of {@link Multiset#equals}.
842   */
843  static boolean equalsImpl(Multiset<?> multiset, @Nullable Object object) {
844    if (object == multiset) {
845      return true;
846    }
847    if (object instanceof Multiset) {
848      Multiset<?> that = (Multiset<?>) object;
849      /*
850       * We can't simply check whether the entry sets are equal, since that
851       * approach fails when a TreeMultiset has a comparator that returns 0
852       * when passed unequal elements.
853       */
854
855      if (multiset.size() != that.size() || multiset.entrySet().size() != that.entrySet().size()) {
856        return false;
857      }
858      for (Entry<?> entry : that.entrySet()) {
859        if (multiset.count(entry.getElement()) != entry.getCount()) {
860          return false;
861        }
862      }
863      return true;
864    }
865    return false;
866  }
867
868  /**
869   * An implementation of {@link Multiset#addAll}.
870   */
871  static <E> boolean addAllImpl(Multiset<E> self, Collection<? extends E> elements) {
872    if (elements.isEmpty()) {
873      return false;
874    }
875    if (elements instanceof Multiset) {
876      Multiset<? extends E> that = cast(elements);
877      for (Entry<? extends E> entry : that.entrySet()) {
878        self.add(entry.getElement(), entry.getCount());
879      }
880    } else {
881      Iterators.addAll(self, elements.iterator());
882    }
883    return true;
884  }
885
886  /**
887   * An implementation of {@link Multiset#removeAll}.
888   */
889  static boolean removeAllImpl(Multiset<?> self, Collection<?> elementsToRemove) {
890    Collection<?> collection =
891        (elementsToRemove instanceof Multiset)
892            ? ((Multiset<?>) elementsToRemove).elementSet()
893            : elementsToRemove;
894
895    return self.elementSet().removeAll(collection);
896  }
897
898  /**
899   * An implementation of {@link Multiset#retainAll}.
900   */
901  static boolean retainAllImpl(Multiset<?> self, Collection<?> elementsToRetain) {
902    checkNotNull(elementsToRetain);
903    Collection<?> collection =
904        (elementsToRetain instanceof Multiset)
905            ? ((Multiset<?>) elementsToRetain).elementSet()
906            : elementsToRetain;
907
908    return self.elementSet().retainAll(collection);
909  }
910
911  /**
912   * An implementation of {@link Multiset#setCount(Object, int)}.
913   */
914  static <E> int setCountImpl(Multiset<E> self, E element, int count) {
915    checkNonnegative(count, "count");
916
917    int oldCount = self.count(element);
918
919    int delta = count - oldCount;
920    if (delta > 0) {
921      self.add(element, delta);
922    } else if (delta < 0) {
923      self.remove(element, -delta);
924    }
925
926    return oldCount;
927  }
928
929  /**
930   * An implementation of {@link Multiset#setCount(Object, int, int)}.
931   */
932  static <E> boolean setCountImpl(Multiset<E> self, E element, int oldCount, int newCount) {
933    checkNonnegative(oldCount, "oldCount");
934    checkNonnegative(newCount, "newCount");
935
936    if (self.count(element) == oldCount) {
937      self.setCount(element, newCount);
938      return true;
939    } else {
940      return false;
941    }
942  }
943
944  abstract static class ElementSet<E> extends Sets.ImprovedAbstractSet<E> {
945    abstract Multiset<E> multiset();
946
947    @Override
948    public void clear() {
949      multiset().clear();
950    }
951
952    @Override
953    public boolean contains(Object o) {
954      return multiset().contains(o);
955    }
956
957    @Override
958    public boolean containsAll(Collection<?> c) {
959      return multiset().containsAll(c);
960    }
961
962    @Override
963    public boolean isEmpty() {
964      return multiset().isEmpty();
965    }
966
967    @Override
968    public Iterator<E> iterator() {
969      return new TransformedIterator<Entry<E>, E>(multiset().entrySet().iterator()) {
970        @Override
971        E transform(Entry<E> entry) {
972          return entry.getElement();
973        }
974      };
975    }
976
977    @Override
978    public boolean remove(Object o) {
979      return multiset().remove(o, Integer.MAX_VALUE) > 0;
980    }
981
982    @Override
983    public int size() {
984      return multiset().entrySet().size();
985    }
986  }
987
988  abstract static class EntrySet<E> extends Sets.ImprovedAbstractSet<Entry<E>> {
989    abstract Multiset<E> multiset();
990
991    @Override
992    public boolean contains(@Nullable Object o) {
993      if (o instanceof Entry) {
994        /*
995         * The GWT compiler wrongly issues a warning here.
996         */
997        @SuppressWarnings("cast")
998        Entry<?> entry = (Entry<?>) o;
999        if (entry.getCount() <= 0) {
1000          return false;
1001        }
1002        int count = multiset().count(entry.getElement());
1003        return count == entry.getCount();
1004      }
1005      return false;
1006    }
1007
1008    // GWT compiler warning; see contains().
1009    @SuppressWarnings("cast")
1010    @Override
1011    public boolean remove(Object object) {
1012      if (object instanceof Multiset.Entry) {
1013        Entry<?> entry = (Entry<?>) object;
1014        Object element = entry.getElement();
1015        int entryCount = entry.getCount();
1016        if (entryCount != 0) {
1017          // Safe as long as we never add a new entry, which we won't.
1018          @SuppressWarnings("unchecked")
1019          Multiset<Object> multiset = (Multiset) multiset();
1020          return multiset.setCount(element, entryCount, 0);
1021        }
1022      }
1023      return false;
1024    }
1025
1026    @Override
1027    public void clear() {
1028      multiset().clear();
1029    }
1030  }
1031
1032  /**
1033   * An implementation of {@link Multiset#iterator}.
1034   */
1035  static <E> Iterator<E> iteratorImpl(Multiset<E> multiset) {
1036    return new MultisetIteratorImpl<E>(multiset, multiset.entrySet().iterator());
1037  }
1038
1039  static final class MultisetIteratorImpl<E> implements Iterator<E> {
1040    private final Multiset<E> multiset;
1041    private final Iterator<Entry<E>> entryIterator;
1042    private Entry<E> currentEntry;
1043
1044    /** Count of subsequent elements equal to current element */
1045    private int laterCount;
1046
1047    /** Count of all elements equal to current element */
1048    private int totalCount;
1049
1050    private boolean canRemove;
1051
1052    MultisetIteratorImpl(Multiset<E> multiset, Iterator<Entry<E>> entryIterator) {
1053      this.multiset = multiset;
1054      this.entryIterator = entryIterator;
1055    }
1056
1057    @Override
1058    public boolean hasNext() {
1059      return laterCount > 0 || entryIterator.hasNext();
1060    }
1061
1062    @Override
1063    public E next() {
1064      if (!hasNext()) {
1065        throw new NoSuchElementException();
1066      }
1067      if (laterCount == 0) {
1068        currentEntry = entryIterator.next();
1069        totalCount = laterCount = currentEntry.getCount();
1070      }
1071      laterCount--;
1072      canRemove = true;
1073      return currentEntry.getElement();
1074    }
1075
1076    @Override
1077    public void remove() {
1078      checkRemove(canRemove);
1079      if (totalCount == 1) {
1080        entryIterator.remove();
1081      } else {
1082        multiset.remove(currentEntry.getElement());
1083      }
1084      totalCount--;
1085      canRemove = false;
1086    }
1087  }
1088
1089  /**
1090   * An implementation of {@link Multiset#size}.
1091   */
1092  static int sizeImpl(Multiset<?> multiset) {
1093    long size = 0;
1094    for (Entry<?> entry : multiset.entrySet()) {
1095      size += entry.getCount();
1096    }
1097    return Ints.saturatedCast(size);
1098  }
1099
1100  /**
1101   * Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557
1102   */
1103  static <T> Multiset<T> cast(Iterable<T> iterable) {
1104    return (Multiset<T>) iterable;
1105  }
1106
1107  private static final Ordering<Entry<?>> DECREASING_COUNT_ORDERING =
1108      new Ordering<Entry<?>>() {
1109        @Override
1110        public int compare(Entry<?> entry1, Entry<?> entry2) {
1111          return Ints.compare(entry2.getCount(), entry1.getCount());
1112        }
1113      };
1114
1115  /**
1116   * Returns a copy of {@code multiset} as an {@link ImmutableMultiset} whose iteration order is
1117   * highest count first, with ties broken by the iteration order of the original multiset.
1118   *
1119   * @since 11.0
1120   */
1121  @Beta
1122  public static <E> ImmutableMultiset<E> copyHighestCountFirst(Multiset<E> multiset) {
1123    List<Entry<E>> sortedEntries =
1124        Multisets.DECREASING_COUNT_ORDERING.immutableSortedCopy(multiset.entrySet());
1125    return ImmutableMultiset.copyFromEntries(sortedEntries);
1126  }
1127}