Class Multisets

java.lang.Object
eu.bandm.tools.util.multi.Multisets

public abstract class Multisets extends Object
Utility class of constants and combinators for Multiset.
  • Method Details

    • unmodifiableMultiset

      public static <A> UnmodifiableMultiset<A> unmodifiableMultiset(Multiset<A> peer)
      Returns a proxy for the given multiset which does not allow modifications. The result is backed by the argument. All changes to the argument are reflected by the result.
      Type Parameters:
      A - the type of the elements
      Parameters:
      peer - the wrapped multiset
      Returns:
      a proxy for the given multiset which does not allow modifications
    • empty

      public static <A> UnmodifiableMultiset<A> empty()
      Returns a multiset which is empty.
      Type Parameters:
      A - the type of the elements
      Returns:
      an empty multiset
    • singleton

      public static <A> Multiset<A> singleton(A first)
      Returns a multiset which contains onle the given element once.
      Type Parameters:
      A - the type of the elements
      Parameters:
      first - the only element contained
      Returns:
      a singleton multiset containing once the given element
    • add

      @SafeVarargs public static <A, R extends Multiset<? super A>> R add(Supplier<? extends R> newInstance, Multiset<? extends A>... sets)
      Return a new multiset which contains the sums of the cardinalities of all given multisets.
      Type Parameters:
      A - the type of the elements
      R - the type of the result
      Parameters:
      newInstance - a factory method (no parameters) to construct the result
      sets - the list of multisets to add
      Returns:
      a new multiset containing the addition result
    • add

      @SafeVarargs public static <A> Multiset<A> add(Multiset<? extends A>... sets)
      Return a new hash multiset which contains the sums of the cardinalities of all given multisets.
      Type Parameters:
      A - the type of the elements
      Parameters:
      sets - the list of multisets to add
      Returns:
      a new multiset containing the addition result
    • subtract

      public static <A, R extends Multiset<? super A>> R subtract(Supplier<? extends R> newInstance, Multiset<? extends A> first, Multiset<? extends A> second)
      Returns a new multiset which contains the difference of the cardinalities of the two multisets. Those in the second set are subtractd from those in the first set, but not below zero.
      Type Parameters:
      A - the type of the elements
      R - the type of the result
      Parameters:
      newInstance - a factory method (no parameters) to construct the result
      first - the multiset to subtract from
      second - the multiset to subtract
      Returns:
      a new multiset containing the difference
    • subtract

      public static <A> Multiset<A> subtract(Multiset<A> first, Multiset<A> second)
      Returns a new hash multiset which contains the difference of the cardinalities of the two multisets. Those in the second set are subtractd from those in the first set, but not below zero.
      Type Parameters:
      A - the type of the elements
      Parameters:
      first - the multiset to subtract from
      second - the multiset to subtract
      Returns:
      a new multiset containing the difference
    • partialCompare

      @Opt public static <A> @Opt Integer partialCompare(Multiset<A> left, Multiset<A> right)
      Compare two multisets, if possible. Returns -1, 0, or 1, as usual for comparators, whenever the left set is smaller, equal, or larger than the right set, respectively. Additionally it delivers null when the sets are not comparable, that means both contain at least one element the other has not.
      Type Parameters:
      A - the type of the elements
      Parameters:
      left - one set to compare
      right - the other set to compare
      Returns:
      -1, 0, or 1 whenever the left set is smaller, equal, or larger than the right set, resp., or null when the sets are not comparable,
    • compareInner

      @Opt protected static <A> @Opt Integer compareInner(Multiset<A> left, Multiset<A> right, Set<A> dom)
      Auxiliary method to compare two multisets restricted to the given set of elements. Returns -1, 0, or 1 whenever the left multiset (restricted to the given set) is smaller, equal, or larger than the right multiset (restricted to the given set). Returns null when both are uncomparble, as with {a, a, b} vs. {a, b, b}.
      Type Parameters:
      A - the type of the elements
      Parameters:
      left - one set to compare
      right - the other set to compare
      dom - the set of elements to compare
      Returns:
      -1, 0, or 1 whenever the left set is smaller, equal, or larger than the right set, resp., or null when the sets are not comparable,
    • ascendingCardinalities

      public static <A> Iterable<Map.Entry<Integer,A>> ascendingCardinalities(Multiset<A> collected)
      Returns a view to this instance of pairs of elements and their cardinalities in ascending cardinality. This view is backed by this multiset and changes to this multiset will be reflected by the view.
      Type Parameters:
      A - the type of the elements
      Parameters:
      collected - the multiset to visit
      Returns:
      an iterable of all map entries, sorted by ascending cardinality.