E
- the type held in the multisetpublic class PredicatedMultiSet<E> extends PredicatedCollection<E> implements MultiSet<E>
MultiSet
to validate that additions
match a specified predicate.
This multiset exists to provide validation for the decorated multiset.
It is normally created to decorate an empty multiset.
If an object cannot be added to the multiset, an IllegalArgumentException
is thrown.
One usage would be to ensure that no null entries are added to the multiset.
MultiSet<E> set = PredicatedMultiSet.predicatedMultiSet(new HashMultiSet<E>(), NotNullPredicate.notNullPredicate());
PredicatedCollection.Builder<E>
MultiSet.Entry<E>
predicate
Modifier | Constructor and Description |
---|---|
protected |
PredicatedMultiSet(MultiSet<E> multiset,
Predicate<? super E> predicate)
Constructor that wraps (not copies).
|
Modifier and Type | Method and Description |
---|---|
int |
add(E object,
int count)
Adds a number of occurrences of the specified object to the MultiSet.
|
protected MultiSet<E> |
decorated()
Gets the decorated multiset.
|
Set<MultiSet.Entry<E>> |
entrySet()
Returns a
Set of all entries contained in the MultiSet. |
boolean |
equals(Object object)
Compares this MultiSet to another object.
|
int |
getCount(Object object)
Returns the number of occurrences of the given object currently
in the MultiSet.
|
int |
hashCode()
Gets a hash code for the MultiSet compatible with the definition of equals.
|
static <E> PredicatedMultiSet<E> |
predicatedMultiSet(MultiSet<E> multiset,
Predicate<? super E> predicate)
Factory method to create a predicated (validating) multiset.
|
int |
remove(Object object,
int count)
Removes a number of occurrences of the specified object from the MultiSet.
|
int |
setCount(E object,
int count)
Sets the number of occurrences of the specified object in the MultiSet
to the given count.
|
Set<E> |
uniqueSet()
Returns a
Set of unique elements in the MultiSet. |
add, addAll, builder, notNullBuilder, predicatedCollection, validate
clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, setCollection, size, toArray, toArray, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
add, containsAll, iterator, remove, removeAll, retainAll, size
addAll, clear, contains, isEmpty, parallelStream, removeIf, spliterator, stream, toArray, toArray
protected PredicatedMultiSet(MultiSet<E> multiset, Predicate<? super E> predicate)
If there are any elements already in the multiset being decorated, they are validated.
multiset
- the multiset to decorate, must not be nullpredicate
- the predicate to use for validation, must not be nullNullPointerException
- if multiset or predicate is nullIllegalArgumentException
- if the multiset contains invalid elementspublic static <E> PredicatedMultiSet<E> predicatedMultiSet(MultiSet<E> multiset, Predicate<? super E> predicate)
If there are any elements already in the multiset being decorated, they are validated.
E
- the type of the elements in the multisetmultiset
- the multiset to decorate, must not be nullpredicate
- the predicate to use for validation, must not be nullNullPointerException
- if multiset or predicate is nullIllegalArgumentException
- if the multiset contains invalid elementsprotected MultiSet<E> decorated()
decorated
in class AbstractCollectionDecorator<E>
public boolean equals(Object object)
MultiSet
This MultiSet equals another object if it is also a MultiSet that contains the same number of occurrences of the same elements.
public int hashCode()
MultiSet
(e==null ? 0 : e.hashCode()) ^ noOccurances)
.public int add(E object, int count)
MultiSet
If the object is already in the MultiSet.uniqueSet()
then increment its
count as reported by MultiSet.getCount(Object)
. Otherwise add it to the
MultiSet.uniqueSet()
and report its count as occurrences
.
public int remove(Object object, int count)
MultiSet
If the number of occurrences to remove is greater than the actual number of occurrences in the multiset, the object will be removed from the multiset.
public int getCount(Object object)
MultiSet
public int setCount(E object, int count)
MultiSet
If the provided count is zero, the object will be removed from the
MultiSet.uniqueSet()
.
public Set<E> uniqueSet()
MultiSet
Set
of unique elements in the MultiSet.
Uniqueness constraints are the same as those in Set
.
The returned set is backed by this multiset, so any change to either is immediately reflected in the other. Only removal operations are supported, in which case all occurrences of the element are removed from the backing multiset.
Copyright © 2001–2018 The Apache Software Foundation. All rights reserved.