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

public Class PredicatedCollection<E>

extends AbstractCollectionDecorator<E>
Class Inheritance
Known Direct Subclasses
org.apache.commons.collections4.list.PredicatedList, org.apache.commons.collections4.multiset.PredicatedMultiSet, org.apache.commons.collections4.queue.PredicatedQueue, org.apache.commons.collections4.set.PredicatedSet, org.apache.commons.collections4.bag.PredicatedBag
Type Parameters
<E>
the type of the elements in the collection
Imports
java.util.ArrayList, .Collection, .Collections, .HashSet, .LinkedList, .List, .Queue, .Set, org.apache.commons.collections4.Bag, .MultiSet, .Predicate, org.apache.commons.collections4.bag.HashBag, .PredicatedBag, org.apache.commons.collections4.functors.NotNullPredicate, org.apache.commons.collections4.list.PredicatedList, org.apache.commons.collections4.multiset.HashMultiSet, .PredicatedMultiSet, org.apache.commons.collections4.queue.PredicatedQueue, org.apache.commons.collections4.set.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

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
PredicatedCollection.Builder<
the element type
E
>

Builder for creating predicated collections.

Field Summary

Modifier and TypeField and Description
protected final Predicate<? super E>
predicate

The predicate to use

private static final long

Constructor Summary

AccessConstructor and Description
protected
PredicatedCollection(final Collection<E>
the collection to decorate, must not be null
coll
,
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 boolean

Returns:

the result of adding to the underlying collection
add
(final E
the object being added
object
)

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

Implements java.util.Collection.add.

Override to validate the object being added to ensure it matches the predicate.
public boolean

Returns:

the result of adding to the underlying collection
addAll
(final Collection<? extends E>
the collection being added
coll
)

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

Implements java.util.Collection.addAll.

Override to validate the objects being added to ensure they match the predicate.
public static <
the element type
E
>
PredicatedCollection.Builder<E>

Returns:

a new Builder for predicated collections
builder
(final Predicate<? super E>
the predicate to use
predicate
)

Returns a Builder with the given predicate.

public static <
the element type
E
>
PredicatedCollection.Builder<E>

Returns:

a new Builder for predicated collections that ignores null values.
notNullBuilder
()

Returns a Builder with a NotNullPredicate.

public static <
the type of the elements in the collection
T
>
PredicatedCollection<T>

Returns:

a new predicated collection
predicatedCollection
(final Collection<T>
the collection to decorate, must not be null
coll
,
final Predicate<? super T>
the predicate to use for validation, must not be null
predicate
)

Factory method to create a predicated (validating) collection.

protected void
validate(final E
the object being added
object
)

Validates the object being added to ensure it matches the predicate.

Inherited from org.apache.commons.collections4.collection.AbstractCollectionDecorator:
clearcontainscontainsAlldecoratedisEmptyiteratorremoveremoveAllremoveIfretainAllsetCollectionsizetoArraytoArraytoString

Field Detail

predicateback to summary
protected final Predicate<? super E> predicate

The predicate to use

serialVersionUIDback to summary
private static final long serialVersionUID

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

Serialization version

Constructor Detail

PredicatedCollectionback to summary
protected PredicatedCollection(final Collection<E> coll, final Predicate<? super E> predicate)

Constructor that wraps (not copies).

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

Parameters
coll:Collection<E>

the collection to decorate, must not be null

predicate:Predicate<? super E>

the predicate to use for validation, must not be null

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

Method Detail

addback to summary
public boolean add(final E object)

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

Implements java.util.Collection.add.

Override to validate the object being added to ensure it matches the predicate.

Parameters
object:E

the object being added

Returns:boolean

the result of adding to the underlying collection

Annotations
@Override
Exceptions
IllegalArgumentException:
if the add is invalid
addAllback to summary
public boolean addAll(final Collection<? extends E> coll)

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

Implements java.util.Collection.addAll.

Override to validate the objects being added to ensure they match the predicate. If any one fails, no update is made to the underlying collection.

Parameters
coll:Collection<? extends E>

the collection being added

Returns:boolean

the result of adding to the underlying collection

Annotations
@Override
Exceptions
IllegalArgumentException:
if the add is invalid
builderback to summary
public static <E> PredicatedCollection.Builder<E> builder(final Predicate<? super E> predicate)

Returns a Builder with the given predicate.

Parameters
<E>
the element type
predicate:Predicate<? super E>

the predicate to use

Returns:PredicatedCollection.Builder<E>

a new Builder for predicated collections

Since
4.1
notNullBuilderback to summary
public static <E> PredicatedCollection.Builder<E> notNullBuilder()

Returns a Builder with a NotNullPredicate.

Parameters
<E>
the element type
Returns:PredicatedCollection.Builder<E>

a new Builder for predicated collections that ignores null values.

Since
4.1
predicatedCollectionback to summary
public static <T> PredicatedCollection<T> predicatedCollection(final Collection<T> coll, final 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.

Parameters
<T>
the type of the elements in the collection
coll:Collection<T>

the collection to decorate, must not be null

predicate:Predicate<? super T>

the predicate to use for validation, must not be null

Returns:PredicatedCollection<T>

a new predicated collection

Exceptions
NullPointerException:
if collection or predicate is null
IllegalArgumentException:
if the collection contains invalid elements
Since
4.0
validateback to summary
protected void validate(final 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:E

the object being added

Exceptions
IllegalArgumentException:
if the add is invalid
org.apache.commons.collections4.collection back to summary

public Class PredicatedCollection.Builder<E>

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.commons.collections4.collection.PredicatedCollection.Builder
Type Parameters
<E>
the element type

Builder for creating predicated collections.

Create a Builder with a predicate to validate elements against, then add any elements to the builder. Elements that fail the predicate will be added to a rejected list. Finally create or decorate a collection using the createPredicated[List,Set,Bag,Queue] methods.

An example:

  Predicate<String> predicate = NotNullPredicate.notNullPredicate();
  PredicatedCollectionBuilder<String> builder = PredicatedCollection.builder(predicate);
  builder.add("item1");
  builder.add(null);
  builder.add("item2");
  List<String> predicatedList = builder.createPredicatedList();

At the end of the code fragment above predicatedList is protected by the predicate supplied to the builder and it contains item1 and item2.

More elements can be added to the builder once a predicated collection has been created, but these elements will not be reflected in already created collections.

Since
4.1

Field Summary

Modifier and TypeField and Description
private final List<E>
accepted

The buffer containing valid elements.

private final Predicate<? super E>
predicate

The predicate to use.

private final List<E>
rejected

The buffer containing rejected elements.

Constructor Summary

AccessConstructor and Description
public
Builder(final Predicate<? super E>
the predicate to use
predicate
)

Constructs a PredicatedCollectionBuilder with the specified Predicate.

Method Summary

Modifier and TypeMethod and Description
public PredicatedCollection.Builder<E>

Returns:

the PredicatedCollectionBuilder.
add
(final E
the element to add
item
)

Adds the item to the builder.

public PredicatedCollection.Builder<E>

Returns:

the PredicatedCollectionBuilder.
addAll
(final Collection<? extends E>
the elements to add to the builder
items
)

Adds all elements from the given collection to the builder.

public Bag<E>

Returns:

a new predicated bag.
createPredicatedBag
()

Create a new predicated bag filled with the accepted elements.

public Bag<E>

Returns:

the decorated bag.
createPredicatedBag
(final Bag<E>
the bag to decorate, must not be null
bag
)

Decorates the given bag with validating behavior using the predicate.

public List<E>

Returns:

a new predicated list.
createPredicatedList
()

Create a new predicated list filled with the accepted elements.

public List<E>

Returns:

the decorated list.
createPredicatedList
(final List<E>
the List to decorate, must not be null
list
)

Decorates the given list with validating behavior using the predicate.

public MultiSet<E>

Returns:

a new predicated multiset.
createPredicatedMultiSet
()

Create a new predicated multiset filled with the accepted elements.

public MultiSet<E>

Returns:

the decorated multiset.
createPredicatedMultiSet
(final MultiSet<E>
the multiset to decorate, must not be null
multiset
)

Decorates the given multiset with validating behavior using the predicate.

public Queue<E>

Returns:

a new predicated queue.
createPredicatedQueue
()

Create a new predicated queue filled with the accepted elements.

public Queue<E>

Returns:

the decorated queue.
createPredicatedQueue
(final Queue<E>
the queue to decorate, must not be null
queue
)

Decorates the given queue with validating behavior using the predicate.

public Set<E>

Returns:

a new predicated set.
createPredicatedSet
()

Create a new predicated set filled with the accepted elements.

public Set<E>

Returns:

the decorated set.
createPredicatedSet
(final Set<E>
the set to decorate, must not be null
set
)

Decorates the given list with validating behavior using the predicate.

public Collection<E>

Returns:

an unmodifiable collection
rejectedElements
()

Returns an unmodifiable collection containing all rejected elements.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

acceptedback to summary
private final List<E> accepted

The buffer containing valid elements.

predicateback to summary
private final Predicate<? super E> predicate

The predicate to use.

rejectedback to summary
private final List<E> rejected

The buffer containing rejected elements.

Constructor Detail

Builderback to summary
public Builder(final Predicate<? super E> predicate)

Constructs a PredicatedCollectionBuilder with the specified Predicate.

Parameters
predicate:Predicate<? super E>

the predicate to use

Exceptions
NullPointerException:
if predicate is null

Method Detail

addback to summary
public PredicatedCollection.Builder<E> add(final E item)

Adds the item to the builder.

If the predicate is true, it is added to the list of accepted elements, otherwise it is added to the rejected list.

Parameters
item:E

the element to add

Returns:PredicatedCollection.Builder<E>

the PredicatedCollectionBuilder.

addAllback to summary
public PredicatedCollection.Builder<E> addAll(final Collection<? extends E> items)

Adds all elements from the given collection to the builder.

All elements for which the predicate evaluates to true will be added to the list of accepted elements, otherwise they are added to the rejected list.

Parameters
items:Collection<? extends E>

the elements to add to the builder

Returns:PredicatedCollection.Builder<E>

the PredicatedCollectionBuilder.

createPredicatedBagback to summary
public Bag<E> createPredicatedBag()

Create a new predicated bag filled with the accepted elements.

The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned bag.

Returns:Bag<E>

a new predicated bag.

createPredicatedBagback to summary
public Bag<E> createPredicatedBag(final Bag<E> bag)

Decorates the given bag with validating behavior using the predicate. All accepted elements are appended to the bag. If the bag already contains elements, they are validated.

The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned bag.

Parameters
bag:Bag<E>

the bag to decorate, must not be null

Returns:Bag<E>

the decorated bag.

Exceptions
NullPointerException:
if bag is null
IllegalArgumentException:
if bag contains invalid elements
createPredicatedListback to summary
public List<E> createPredicatedList()

Create a new predicated list filled with the accepted elements.

The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned list.

Returns:List<E>

a new predicated list.

createPredicatedListback to summary
public List<E> createPredicatedList(final List<E> list)

Decorates the given list with validating behavior using the predicate. All accepted elements are appended to the list. If the list already contains elements, they are validated.

The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned list.

Parameters
list:List<E>

the List to decorate, must not be null

Returns:List<E>

the decorated list.

Exceptions
NullPointerException:
if list is null
IllegalArgumentException:
if list contains invalid elements
createPredicatedMultiSetback to summary
public MultiSet<E> createPredicatedMultiSet()

Create a new predicated multiset filled with the accepted elements.

The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned multiset.

Returns:MultiSet<E>

a new predicated multiset.

createPredicatedMultiSetback to summary
public MultiSet<E> createPredicatedMultiSet(final MultiSet<E> multiset)

Decorates the given multiset with validating behavior using the predicate. All accepted elements are appended to the multiset. If the multiset already contains elements, they are validated.

The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned multiset.

Parameters
multiset:MultiSet<E>

the multiset to decorate, must not be null

Returns:MultiSet<E>

the decorated multiset.

Exceptions
NullPointerException:
if multiset is null
IllegalArgumentException:
if multiset contains invalid elements
createPredicatedQueueback to summary
public Queue<E> createPredicatedQueue()

Create a new predicated queue filled with the accepted elements.

The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned queue.

Returns:Queue<E>

a new predicated queue.

createPredicatedQueueback to summary
public Queue<E> createPredicatedQueue(final Queue<E> queue)

Decorates the given queue with validating behavior using the predicate. All accepted elements are appended to the queue. If the queue already contains elements, they are validated.

The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned queue.

Parameters
queue:Queue<E>

the queue to decorate, must not be null

Returns:Queue<E>

the decorated queue.

Exceptions
NullPointerException:
if queue is null
IllegalArgumentException:
if queue contains invalid elements
createPredicatedSetback to summary
public Set<E> createPredicatedSet()

Create a new predicated set filled with the accepted elements.

The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned set.

Returns:Set<E>

a new predicated set.

createPredicatedSetback to summary
public Set<E> createPredicatedSet(final Set<E> set)

Decorates the given list with validating behavior using the predicate. All accepted elements are appended to the set. If the set already contains elements, they are validated.

The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned set.

Parameters
set:Set<E>

the set to decorate, must not be null

Returns:Set<E>

the decorated set.

Exceptions
NullPointerException:
if set is null
IllegalArgumentException:
if set contains invalid elements
rejectedElementsback to summary
public Collection<E> rejectedElements()

Returns an unmodifiable collection containing all rejected elements.

Returns:Collection<E>

an unmodifiable collection