Class Multisets

java.lang.Object
org.docx4j.com.google.common.collect.Multisets

@GwtCompatible public final class Multisets extends Object
Provides static utility methods for creating and working with Multiset instances.

See the Guava User Guide article on Multisets.

Since:
2.0
  • Constructor Details

    • Multisets

      private Multisets()
  • Method Details

    • toMultiset

      public static <T, E, M extends Multiset<E>> Collector<T,?,M> toMultiset(Function<? super T,E> elementFunction, ToIntFunction<? super T> countFunction, Supplier<M> multisetSupplier)
      Returns a Collector that accumulates elements into a multiset created via the specified Supplier, whose elements are the result of applying elementFunction to the inputs, with counts equal to the result of applying countFunction to the inputs. Elements are added in encounter order.

      If the mapped elements contain duplicates (according to Object.equals(java.lang.Object)), the element will be added more than once, with the count summed over all appearances of the element.

      Note that stream.collect(toMultiset(function, e -> 1, supplier)) is equivalent to stream.map(function).collect(Collectors.toCollection(supplier)).

      Since:
      22.0
    • containsOccurrences

      @CanIgnoreReturnValue public static boolean containsOccurrences(Multiset<?> superMultiset, Multiset<?> subMultiset)
      Returns true if subMultiset.count(o) <= superMultiset.count(o) for all o.
      Since:
      10.0
    • retainOccurrences

      @CanIgnoreReturnValue public static boolean retainOccurrences(Multiset<?> multisetToModify, Multiset<?> multisetToRetain)
      Modifies multisetToModify so that its count for an element e is at most multisetToRetain.count(e).

      To be precise, multisetToModify.count(e) is set to Math.min(multisetToModify.count(e), multisetToRetain.count(e)). This is similar to intersection (multisetToModify, multisetToRetain), but mutates multisetToModify instead of returning a view.

      In contrast, multisetToModify.retainAll(multisetToRetain) keeps all occurrences of elements that appear at all in multisetToRetain, and deletes all occurrences of all other elements.

      Returns:
      true if multisetToModify was changed as a result of this operation
      Since:
      10.0
    • retainOccurrencesImpl

      private static <E> boolean retainOccurrencesImpl(Multiset<E> multisetToModify, Multiset<?> occurrencesToRetain)
      Delegate implementation which cares about the element type.
    • removeOccurrences

      @CanIgnoreReturnValue public static boolean removeOccurrences(Multiset<?> multisetToModify, Iterable<?> occurrencesToRemove)
      For each occurrence of an element e in occurrencesToRemove, removes one occurrence of e in multisetToModify.

      Equivalently, this method modifies multisetToModify so that multisetToModify.count(e) is set to Math.max(0, multisetToModify.count(e) - Iterables.frequency(occurrencesToRemove, e)).

      This is not the same as multisetToModify. removeAll(occurrencesToRemove), which removes all occurrences of elements that appear in occurrencesToRemove. However, this operation is equivalent to, albeit sometimes more efficient than, the following:

      
       for (E e : occurrencesToRemove) {
         multisetToModify.remove(e);
       }
       
      Returns:
      true if multisetToModify was changed as a result of this operation
      Since:
      18.0 (present in 10.0 with a requirement that the second parameter be a Multiset)
    • removeOccurrences

      @CanIgnoreReturnValue public static boolean removeOccurrences(Multiset<?> multisetToModify, Multiset<?> occurrencesToRemove)
      For each occurrence of an element e in occurrencesToRemove, removes one occurrence of e in multisetToModify.

      Equivalently, this method modifies multisetToModify so that multisetToModify.count(e) is set to Math.max(0, multisetToModify.count(e) - occurrencesToRemove.count(e)).

      This is not the same as multisetToModify. removeAll(occurrencesToRemove), which removes all occurrences of elements that appear in occurrencesToRemove. However, this operation is equivalent to, albeit sometimes more efficient than, the following:

      
       for (E e : occurrencesToRemove) {
         multisetToModify.remove(e);
       }
       
      Returns:
      true if multisetToModify was changed as a result of this operation
      Since:
      10.0 (missing in 18.0 when only the overload taking an Iterable was present)
    • equalsImpl

      static boolean equalsImpl(Multiset<?> multiset, @Nullable Object object)
      An implementation of Multiset.equals(java.lang.Object).
    • addAllImpl

      static <E> boolean addAllImpl(Multiset<E> self, Collection<? extends E> elements)
    • addAllImpl

      private static <E> boolean addAllImpl(Multiset<E> self, Multiset<? extends E> elements)
      A specialization of addAllImpl for when elements is itself a Multiset.
    • removeAllImpl

      static boolean removeAllImpl(Multiset<?> self, Collection<?> elementsToRemove)
    • retainAllImpl

      static boolean retainAllImpl(Multiset<?> self, Collection<?> elementsToRetain)
    • iteratorImpl

      static <E> Iterator<E> iteratorImpl(Multiset<E> multiset)
      An implementation of Multiset.iterator().
    • spliteratorImpl

      static <E> Spliterator<E> spliteratorImpl(Multiset<E> multiset)
    • linearTimeSizeImpl

      static int linearTimeSizeImpl(Multiset<?> multiset)
      An implementation of Multiset.size().
    • cast

      static <T> Multiset<T> cast(Iterable<T> iterable)
      Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557