org.apache.commons.collections4.multiset

## Class PredicatedMultiSet<E>

- java.lang.Object
- - org.apache.commons.collections4.collection.AbstractCollectionDecorator<E>
  - - org.apache.commons.collections4.collection.PredicatedCollection<E>
    - - org.apache.commons.collections4.multiset.PredicatedMultiSet<E>

- All Implemented Interfaces:
    Serializable, Iterable<E>, Collection<E>, MultiSet<E>

      public class PredicatedMultiSet<E>
      extends PredicatedCollection<E>
      implements MultiSet<E>

  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
  Version:
    $Id: PredicatedMultiSet.java 1688308 2015-06-29 21:28:54Z tn $
  See Also:
    Serialized Form

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class org.apache.commons.collections4.collection.PredicatedCollection

      `PredicatedCollection.Builder<E>`

    - ### Nested classes/interfaces inherited from interface org.apache.commons.collections4.MultiSet

      `MultiSet.Entry<E>`

  - ### Field Summary

    - ### Fields inherited from class org.apache.commons.collections4.collection.PredicatedCollection

      `predicate`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `PredicatedMultiSet(MultiSet<E> multiset, Predicate<? super E> predicate)` Constructor that wraps (not copies).

  - ### Method Summary

    Methods
    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.

    - ### Methods inherited from class org.apache.commons.collections4.collection.PredicatedCollection

      `add, addAll, builder, notNullBuilder, predicatedCollection, validate`

    - ### Methods inherited from class org.apache.commons.collections4.collection.AbstractCollectionDecorator

      `clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, setCollection, size, toArray, toArray, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface org.apache.commons.collections4.MultiSet

      `add, containsAll, iterator, remove, removeAll, retainAll, size`

    - ### Methods inherited from interface java.util.Collection

      `addAll, clear, contains, isEmpty, toArray, toArray`

- - ### Constructor Detail

    - #### PredicatedMultiSet

          protected PredicatedMultiSet(MultiSet<E> multiset,
                            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` - the multiset to decorate, must not be null
        `predicate` - the predicate to use for validation, must not be null
      Throws:
        `NullPointerException` - if multiset or predicate is null
        `IllegalArgumentException` - if the multiset contains invalid elements

  - ### Method Detail

    - #### predicatedMultiSet

          public static <E> PredicatedMultiSet<E> predicatedMultiSet(MultiSet<E> multiset,
                                                     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.

      Type Parameters:
        `E` - the type of the elements in the multiset
      Parameters:
        `multiset` - the multiset to decorate, must not be null
        `predicate` - the predicate to use for validation, must not be null
      Returns:
        a new predicated MultiSet
      Throws:
        `NullPointerException` - if multiset or predicate is null
        `IllegalArgumentException` - if the multiset contains invalid elements

    - #### decorated

          protected MultiSet<E> decorated()

      Gets the decorated multiset.
      Overrides:
        `decorated` in class `AbstractCollectionDecorator<E>`
      Returns:
        the decorated multiset

    - #### equals

          public boolean equals(Object object)

      Description copied from interface: `MultiSet`
      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.

      Specified by:
        `equals` in interface `Collection<E>`
      Specified by:
        `equals` in interface `MultiSet<E>`
      Overrides:
        `equals` in class `Object`
      Parameters:
        `object` - the object to compare to
      Returns:
        true if equal

    - #### hashCode

          public int hashCode()

      Description copied from interface: `MultiSet`
      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)`.
      Specified by:
        `hashCode` in interface `Collection<E>`
      Specified by:
        `hashCode` in interface `MultiSet<E>`
      Overrides:
        `hashCode` in class `Object`
      Returns:
        the hash code of the MultiSet

    - #### add

          public int add(E object,
                int count)

      Description copied from interface: `MultiSet`
      Adds a number of occurrences of the specified object to the 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`.

      Specified by:
        `add` in interface `MultiSet<E>`
      Parameters:
        `object` - the object to add
        `count` - the number of occurrences to add, may be zero, in which case no change is made to the multiset
      Returns:
        the number of occurrences of the object in the multiset before this operation; possibly zero

    - #### remove

          public int remove(Object object,
                   int count)

      Description copied from interface: `MultiSet`
      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.

      Specified by:
        `remove` in interface `MultiSet<E>`
      Parameters:
        `object` - the object to remove
        `count` - the number of occurrences to remove, may be zero, in which case no change is made to the multiset
      Returns:
        the number of occurrences of the object in the multiset before the operation; possibly zero

    - #### getCount

          public int getCount(Object object)

      Description copied from interface: `MultiSet`
      Returns the number of occurrences of the given object currently in the MultiSet. If the object does not exist in the multiset, return 0.
      Specified by:
        `getCount` in interface `MultiSet<E>`
      Parameters:
        `object` - the object to search for
      Returns:
        the number of occurrences of the object, zero if not found

    - #### setCount

          public int setCount(E object,
                     int count)

      Description copied from interface: `MultiSet`
      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 `MultiSet.uniqueSet()`.

      Specified by:
        `setCount` in interface `MultiSet<E>`
      Parameters:
        `object` - the object to update
        `count` - the number of occurrences of the object
      Returns:
        the number of occurrences of the object before this operation, zero if the object was not contained in the multiset

    - #### uniqueSet

          public Set<E> uniqueSet()

      Description copied from interface: `MultiSet`
      Returns a `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.

      Specified by:
        `uniqueSet` in interface `MultiSet<E>`
      Returns:
        the Set of unique MultiSet elements

    - #### entrySet

          public Set<MultiSet.Entry<E>> entrySet()

      Description copied from interface: `MultiSet`
      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.

      Specified by:
        `entrySet` in interface `MultiSet<E>`
      Returns:
        the Set of MultiSet entries

Copyright © 2001–2015 The Apache Software Foundation. All rights reserved.