Package eu.bandm.tools.util.multi
Class Multisets
java.lang.Object
eu.bandm.tools.util.multi.Multisets
Utility class of constants and combinators for
Multiset
.-
Method Summary
Modifier and TypeMethodDescriptionstatic <A> Multiset
<A> Return a new hash multiset which contains the sums of the cardinalities of all given multisets.static <A,
R extends Multiset<? super A>>
RReturn a new multiset which contains the sums of the cardinalities of all given multisets.ascendingCardinalities
(Multiset<A> collected) Returns a view to this instance of pairs of elements and their cardinalities in ascending cardinality.compareInner
(Multiset<A> left, Multiset<A> right, Set<A> dom) Auxiliary method to compare two multisets restricted to the given set of elements.static <A> UnmodifiableMultiset
<A> empty()
Returns a multiset which is empty.partialCompare
(Multiset<A> left, Multiset<A> right) Compare two multisets, if possible.static <A> Multiset
<A> singleton
(A first) Returns a multiset which contains onle the given element once.static <A> Multiset
<A> Returns a new hash multiset which contains the difference of the cardinalities of the two multisets.static <A,
R extends Multiset<? super A>>
Rsubtract
(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.static <A> UnmodifiableMultiset
<A> unmodifiableMultiset
(Multiset<A> peer) Returns a proxy for the given multiset which does not allow modifications.
-
Method Details
-
unmodifiableMultiset
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
Returns a multiset which is empty.- Type Parameters:
A
- the type of the elements- Returns:
- an empty multiset
-
singleton
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 elementsR
- the type of the result- Parameters:
newInstance
- a factory method (no parameters) to construct the resultsets
- the list of multisets to add- Returns:
- a new multiset containing the addition result
-
add
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 elementsR
- the type of the result- Parameters:
newInstance
- a factory method (no parameters) to construct the resultfirst
- the multiset to subtract fromsecond
- the multiset to subtract- Returns:
- a new multiset containing the difference
-
subtract
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 fromsecond
- the multiset to subtract- Returns:
- a new multiset containing the difference
-
partialCompare
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 compareright
- 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 compareright
- the other set to comparedom
- 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
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.
-