Class PredicatedCollection<E>

java.lang.Object
org.apache.commons.collections4.collection.AbstractCollectionDecorator<E>
org.apache.commons.collections4.collection.PredicatedCollection<E>
Type Parameters:
E - the type of the elements in the collection
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>
Direct Known Subclasses:
PredicatedBag, PredicatedList, PredicatedMultiSet, PredicatedQueue, PredicatedSet

Decorates another Collection to validate that additions match a specified predicate.

This collection exists to provide validation for the decorated collection. It is normally created to decorate an empty collection. If an object cannot be added to the collection, an IllegalArgumentException is thrown.

One usage would be to ensure that no null entries are added to the collection:

 Collection coll = PredicatedCollection.predicatedCollection(new ArrayList(), NotNullPredicate.INSTANCE);
 

This class is Serializable from Commons Collections 3.1.

Since:
3.0
See Also:
  • Field Details

  • Constructor Details

    • PredicatedCollection

      protected PredicatedCollection(Collection<E> collection, Predicate<? super E> predicate)
      Constructor that wraps (not copies).

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

      Parameters:
      collection - the collection to decorate, must not be null
      predicate - the predicate to use for validation, must not be null
      Throws:
      NullPointerException - if collection or predicate is null
      IllegalArgumentException - if the collection contains invalid elements
  • Method Details

    • builder

      public static <E> PredicatedCollection.Builder<E> builder(Predicate<? super E> predicate)
      Returns a Builder with the given predicate.
      Type Parameters:
      E - the element type
      Parameters:
      predicate - the predicate to use
      Returns:
      a new Builder for predicated collections
      Since:
      4.1
    • notNullBuilder

      Returns a Builder with a NotNullPredicate.
      Type Parameters:
      E - the element type
      Returns:
      a new Builder for predicated collections that ignores null values.
      Since:
      4.1
    • predicatedCollection

      public static <T> PredicatedCollection<T> predicatedCollection(Collection<T> coll, Predicate<? super T> predicate)
      Factory method to create a predicated (validating) collection.

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

      Type Parameters:
      T - the type of the elements in the collection
      Parameters:
      coll - the collection to decorate, must not be null
      predicate - the predicate to use for validation, must not be null
      Returns:
      a new predicated collection
      Throws:
      NullPointerException - if collection or predicate is null
      IllegalArgumentException - if the collection contains invalid elements
      Since:
      4.0
    • add

      public boolean add(E object)
      Override to validate the object being added to ensure it matches the predicate.
      Specified by:
      add in interface Collection<E>
      Overrides:
      add in class AbstractCollectionDecorator<E>
      Parameters:
      object - the object being added
      Returns:
      the result of adding to the underlying collection
      Throws:
      IllegalArgumentException - if the add is invalid
    • addAll

      public boolean addAll(Collection<? extends E> coll)
      Override to validate the objects being added to ensure they match the predicate. If anyone fails, no update is made to the underlying collection.
      Specified by:
      addAll in interface Collection<E>
      Overrides:
      addAll in class AbstractCollectionDecorator<E>
      Parameters:
      coll - the collection being added
      Returns:
      the result of adding to the underlying collection
      Throws:
      IllegalArgumentException - if the add is invalid
    • validate

      protected void validate(E object)
      Validates the object being added to ensure it matches the predicate.

      The predicate itself should not throw an exception, but return false to indicate that the object cannot be added.

      Parameters:
      object - the object being added
      Throws:
      IllegalArgumentException - if the add is invalid