Class PredicatedSortedMultiSet<E>

Type Parameters:
E - The type held in the multiset
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, MultiSet<E>, SortedMultiSet<E>

public class PredicatedSortedMultiSet<E> extends PredicatedMultiSet<E> implements SortedMultiSet<E>
Decorates another SortedMultiSet 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.

 SortedMultiSet<E> set =
      PredicatedSortedMultiSet.predicatedSortedMultiSet(new TreeMultiSet<E>(),
                                                        NotNullPredicate.notNullPredicate());
 
Since:
4.6.0
See Also:
  • Constructor Details

    • PredicatedSortedMultiSet

      protected PredicatedSortedMultiSet(SortedMultiSet<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 Details

    • predicatedSortedMultiSet

      public static <E> PredicatedSortedMultiSet<E> predicatedSortedMultiSet(SortedMultiSet<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 SortedMultiSet
      Throws:
      NullPointerException - if multiset or predicate is null
      IllegalArgumentException - if the multiset contains invalid elements
    • comparator

      public Comparator<? super E> comparator()
      Description copied from interface: SortedMultiSet
      Returns the comparator associated with this sorted multiset, or null if it uses its elements' natural ordering.
      Specified by:
      comparator in interface SortedMultiSet<E>
      Returns:
      The comparator in use, or null if natural ordering.
    • decorated

      protected SortedMultiSet<E> decorated()
      Gets the decorated sorted multiset.
      Overrides:
      decorated in class PredicatedMultiSet<E>
      Returns:
      The decorated multiset
    • first

      public E first()
      Description copied from interface: SortedMultiSet
      Returns the first (lowest) member.
      Specified by:
      first in interface SortedMultiSet<E>
      Returns:
      The first element in the sorted multiset.
    • last

      public E last()
      Description copied from interface: SortedMultiSet
      Returns the last (highest) member.
      Specified by:
      last in interface SortedMultiSet<E>
      Returns:
      The last element in the sorted multiset.