Top Description Fields Constructors Methods
org.apache.commons.collections4.multiset

public Class PredicatedMultiSet<E>

extends PredicatedCollection<E>
implements MultiSet<E>
Class Inheritance
All Implemented Interfaces
org.apache.commons.collections4.MultiSet, java.util.Collection, java.lang.Iterable
Type Parameters
<E>
the type held in the multiset
Imports
java.util.Set, org.apache.commons.collections4.MultiSet, .Predicate, org.apache.commons.collections4.collection.PredicatedCollection

Decorates another 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());
Since
4.1

Field Summary

Modifier and TypeField and Description
private static final long
Inherited from org.apache.commons.collections4.collection.PredicatedCollection:
predicate

Constructor Summary

AccessConstructor and Description
protected
PredicatedMultiSet(final MultiSet<E>
the multiset to decorate, must not be null
multiset
,
final Predicate<? super E>
the predicate to use for validation, must not be null
predicate
)

Constructor that wraps (not copies).

Method Summary

Modifier and TypeMethod and Description
public int
add(final E
the object to add
object
,
final int
the number of occurrences to add, may be zero, in which case no change is made to the multiset
count
)

Implements org.apache.commons.collections4.MultiSet.add.

Adds a number of occurrences of the specified object to the MultiSet.
protected MultiSet<E>

Returns:

the decorated multiset
decorated
()

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.decorated.

Gets the decorated multiset.
public Set<MultiSet.Entry<E>>
entrySet()

Implements org.apache.commons.collections4.MultiSet.entrySet.

Returns a Set of all entries contained in the MultiSet.
public boolean
equals(final Object
the object to compare to
object
)

Overrides java.lang.Object.equals.

Implements org.apache.commons.collections4.MultiSet.equals, java.util.Collection.equals.

Compares this MultiSet to another object.
public int
getCount(final Object
the object to search for
object
)

Implements org.apache.commons.collections4.MultiSet.getCount.

Returns the number of occurrences of the given object currently in the MultiSet.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements org.apache.commons.collections4.MultiSet.hashCode, java.util.Collection.hashCode.

Gets a hash code for the MultiSet compatible with the definition of equals.
public static <
the type of the elements in the multiset
E
>
PredicatedMultiSet<E>

Returns:

a new predicated MultiSet
predicatedMultiSet
(final MultiSet<E>
the multiset to decorate, must not be null
multiset
,
final Predicate<? super E>
the predicate to use for validation, must not be null
predicate
)

Factory method to create a predicated (validating) multiset.

public int
remove(final Object
the object to remove
object
,
final int
the number of occurrences to remove, may be zero, in which case no change is made to the multiset
count
)

Implements org.apache.commons.collections4.MultiSet.remove.

Removes a number of occurrences of the specified object from the MultiSet.
public int
setCount(final E
the object to update
object
,
final int
the number of occurrences of the object
count
)

Implements org.apache.commons.collections4.MultiSet.setCount.

Sets the number of occurrences of the specified object in the MultiSet to the given count.
public Set<E>
uniqueSet()

Implements org.apache.commons.collections4.MultiSet.uniqueSet.

Returns a Set of unique elements in the MultiSet.
Inherited from org.apache.commons.collections4.collection.PredicatedCollection:
addaddAllbuildernotNullBuilderpredicatedCollectionvalidate

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.apache.commons.collections4.collection.PredicatedCollection.serialVersionUID.

Serialization version

Constructor Detail

PredicatedMultiSetback to summary
protected PredicatedMultiSet(final MultiSet<E> multiset, final Predicate<? super E> predicate)

Constructor that wraps (not copies).

If there are any elements already in the multiset being decorated, they are validated.

Parameters
multiset:MultiSet<E>

the multiset to decorate, must not be null

predicate:Predicate<? super E>

the predicate to use for validation, must not be null

Exceptions
NullPointerException:
if multiset or predicate is null
IllegalArgumentException:
if the multiset contains invalid elements

Method Detail

addback to summary
public int add(final E object, final int count)

Implements org.apache.commons.collections4.MultiSet.add.

Doc from org.apache.commons.collections4.MultiSet.add.

Adds a number of occurrences of the specified object to the MultiSet.

If the object is already in the uniqueSet() then increment its count as reported by getCount(Object). Otherwise add it to the uniqueSet() and report its count as occurrences.

Parameters
object:E

the object to add

count:int

the number of occurrences to add, may be zero, in which case no change is made to the multiset

Returns:int

the number of occurrences of the object in the multiset before this operation; possibly zero

Annotations
@Override
decoratedback to summary
protected MultiSet<E> decorated()

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.decorated.

Gets the decorated multiset.

Returns:MultiSet<E>

the decorated multiset

Annotations
@Override
entrySetback to summary
public Set<MultiSet.Entry<E>> entrySet()

Implements org.apache.commons.collections4.MultiSet.entrySet.

Doc from org.apache.commons.collections4.MultiSet.entrySet.

Returns a Set of all entries contained in the MultiSet.

The returned set is backed by this multiset, so any change to either is immediately reflected in the other.

Returns:Set<MultiSet.Entry<E>>

the Set of MultiSet entries

Annotations
@Override
equalsback to summary
public boolean equals(final Object object)

Overrides java.lang.Object.equals.

Implements org.apache.commons.collections4.MultiSet.equals, java.util.Collection.equals.

Doc from org.apache.commons.collections4.MultiSet.equals.

Compares this MultiSet to another object.

This MultiSet equals another object if it is also a MultiSet that contains the same number of occurrences of the same elements.

Parameters
object:Object

the object to compare to

Returns:boolean

true if equal

Annotations
@Override
getCountback to summary
public int getCount(final Object object)

Implements org.apache.commons.collections4.MultiSet.getCount.

Doc from org.apache.commons.collections4.MultiSet.getCount.

Returns the number of occurrences of the given object currently in the MultiSet. If the object does not exist in the multiset, return 0.

Parameters
object:Object

the object to search for

Returns:int

the number of occurrences of the object, zero if not found

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements org.apache.commons.collections4.MultiSet.hashCode, java.util.Collection.hashCode.

Doc from org.apache.commons.collections4.MultiSet.hashCode.

Gets a hash code for the MultiSet compatible with the definition of equals. The hash code is defined as the sum total of a hash code for each element. The per element hash code is defined as (e==null ? 0 : e.hashCode()) ^ noOccurances).

Returns:int

the hash code of the MultiSet

Annotations
@Override
predicatedMultiSetback to summary
public static <E> PredicatedMultiSet<E> predicatedMultiSet(final MultiSet<E> multiset, final Predicate<? super E> predicate)

Factory method to create a predicated (validating) multiset.

If there are any elements already in the multiset being decorated, they are validated.

Parameters
<E>
the type of the elements in the multiset
multiset:MultiSet<E>

the multiset to decorate, must not be null

predicate:Predicate<? super E>

the predicate to use for validation, must not be null

Returns:PredicatedMultiSet<E>

a new predicated MultiSet

Exceptions
NullPointerException:
if multiset or predicate is null
IllegalArgumentException:
if the multiset contains invalid elements
removeback to summary
public int remove(final Object object, final int count)

Implements org.apache.commons.collections4.MultiSet.remove.

Doc from org.apache.commons.collections4.MultiSet.remove.

Removes a number of occurrences of the specified object from the 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.

Parameters
object:Object

the object to remove

count:int

the number of occurrences to remove, may be zero, in which case no change is made to the multiset

Returns:int

the number of occurrences of the object in the multiset before the operation; possibly zero

Annotations
@Override
setCountback to summary
public int setCount(final E object, final int count)

Implements org.apache.commons.collections4.MultiSet.setCount.

Doc from org.apache.commons.collections4.MultiSet.setCount.

Sets the number of occurrences of the specified object in the MultiSet to the given count.

If the provided count is zero, the object will be removed from the uniqueSet().

Parameters
object:E

the object to update

count:int

the number of occurrences of the object

Returns:int

the number of occurrences of the object before this operation, zero if the object was not contained in the multiset

Annotations
@Override
uniqueSetback to summary
public Set<E> uniqueSet()

Implements org.apache.commons.collections4.MultiSet.uniqueSet.

Doc from org.apache.commons.collections4.MultiSet.uniqueSet.

Returns a Set of unique elements in the MultiSet.

Uniqueness constraints are the same as those in java.util.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.

Returns:Set<E>

the Set of unique MultiSet elements

Annotations
@Override