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

public Class MultiSetUtils

extends Object
Class Inheritance
Imports
org.apache.commons.collections4.multiset.HashMultiSet, .PredicatedMultiSet, .SynchronizedMultiSet, .UnmodifiableMultiSet

Provides utility methods and decorators for MultiSet instances.
Since
4.1

Field Summary

Modifier and TypeField and Description
public static final MultiSet<E>
EMPTY_MULTISET

An empty unmodifiable multiset.

Constructor Summary

AccessConstructor and Description
private
MultiSetUtils()

Instantiation of MultiSetUtils is not intended or required.

Method Summary

Modifier and TypeMethod and Description
public static <
the element type
E
>
MultiSet<E>

Returns:

an empty MultiSet
emptyMultiSet
()

Get an empty MultiSet.

public static <
the element type
E
>
MultiSet<E>

Returns:

a predicated multiset backed by the given multiset
predicatedMultiSet
(final MultiSet<E>
the multiset to predicate, must not be null
multiset
,
final Predicate<? super E>
the predicate for the multiset, must not be null
predicate
)

Returns a predicated (validating) multiset backed by the given multiset.

public static <
the element type
E
>
MultiSet<E>

Returns:

a synchronized multiset backed by that multiset
synchronizedMultiSet
(final MultiSet<E>
the multiset to synchronize, must not be null
multiset
)

Returns a synchronized (thread-safe) multiset backed by the given multiset.

public static <
the element type
E
>
MultiSet<E>

Returns:

an unmodifiable view of that multiset
unmodifiableMultiSet
(final MultiSet<? extends E>
the multiset whose unmodifiable view is to be returned, must not be null
multiset
)

Returns an unmodifiable view of the given multiset.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EMPTY_MULTISETback to summary
public static final MultiSet<E> EMPTY_MULTISET

An empty unmodifiable multiset.

Annotations
@SuppressWarnings:rawtypes

Constructor Detail

MultiSetUtilsback to summary
private MultiSetUtils()

Instantiation of MultiSetUtils is not intended or required.

Method Detail

emptyMultiSetback to summary
public static <E> MultiSet<E> emptyMultiSet()

Get an empty MultiSet.

Parameters
<E>
the element type
Returns:MultiSet<E>

an empty MultiSet

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

Returns a predicated (validating) multiset backed by the given multiset.

Only objects that pass the test in the given predicate can be added to the multiset. Trying to add an invalid object results in an IllegalArgumentException. It is important not to use the original multiset after invoking this method, as it is a backdoor for adding invalid objects.

Parameters
<E>
the element type
multiset:MultiSet<E>

the multiset to predicate, must not be null

predicate:Predicate<? super E>

the predicate for the multiset, must not be null

Returns:MultiSet<E>

a predicated multiset backed by the given multiset

Exceptions
NullPointerException:
if the MultiSet or Predicate is null
synchronizedMultiSetback to summary
public static <E> MultiSet<E> synchronizedMultiSet(final MultiSet<E> multiset)

Returns a synchronized (thread-safe) multiset backed by the given multiset. In order to guarantee serial access, it is critical that all access to the backing multiset is accomplished through the returned multiset.

It is imperative that the user manually synchronize on the returned multiset when iterating over it:

MultiSet multiset = MultiSetUtils.synchronizedMultiSet(new HashMultiSet());
...
synchronized(multiset) {
    Iterator i = multiset.iterator(); // Must be in synchronized block
    while (i.hasNext())
        foo(i.next());
    }
}
Failure to follow this advice may result in non-deterministic behavior.
Parameters
<E>
the element type
multiset:MultiSet<E>

the multiset to synchronize, must not be null

Returns:MultiSet<E>

a synchronized multiset backed by that multiset

Exceptions
NullPointerException:
if the MultiSet is null
unmodifiableMultiSetback to summary
public static <E> MultiSet<E> unmodifiableMultiSet(final MultiSet<? extends E> multiset)

Returns an unmodifiable view of the given multiset. Any modification attempts to the returned multiset will raise an UnsupportedOperationException.

Parameters
<E>
the element type
multiset:MultiSet<? extends E>

the multiset whose unmodifiable view is to be returned, must not be null

Returns:MultiSet<E>

an unmodifiable view of that multiset

Exceptions
NullPointerException:
if the MultiSet is null