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

public Class PredicatedBag<E>

extends PredicatedCollection<E>
implements Bag<E>
Class Inheritance
All Implemented Interfaces
org.apache.commons.collections4.Bag, java.util.Collection, java.lang.Iterable
Known Direct Subclasses
org.apache.commons.collections4.bag.PredicatedSortedBag
Type Parameters
<E>
the type of elements in this bag
Imports
java.util.Set, org.apache.commons.collections4.Bag, .Predicate, org.apache.commons.collections4.collection.PredicatedCollection

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

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

One usage would be to ensure that no null entries are added to the bag.

Bag bag = PredicatedBag.predicatedBag(new HashBag(), NotNullPredicate.INSTANCE);

This class is Serializable from Commons Collections 3.1.

Since
3.0

Field Summary

Modifier and TypeField and Description
private static final long
Inherited from org.apache.commons.collections4.collection.PredicatedCollection:
predicate

Constructor Summary

AccessConstructor and Description
protected
PredicatedBag(final Bag<E>
the bag to decorate, must not be null
bag
,
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
add(final E
the object to add
object
,
final int
the number of copies to add
count
)

Implements org.apache.commons.collections4.Bag.add.

Adds nCopies copies of the specified object to the Bag.
protected Bag<E>

Returns:

the decorated bag
decorated
()

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

Gets the decorated bag.
public boolean
equals(final Object
object to be compared for equality with this collection
object
)

Overrides java.lang.Object.equals.

Implements java.util.Collection.equals.

Compares the specified object with this collection for equality.
public int
getCount(final Object
the object to search for
object
)

Implements org.apache.commons.collections4.Bag.getCount.

Returns the number of occurrences (cardinality) of the given object currently in the bag.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Collection.hashCode.

Returns the hash code value for this collection.
public static <
the type of the elements in the bag
E
>
PredicatedBag<E>

Returns:

a new predicated Bag
predicatedBag
(final Bag<E>
the bag to decorate, must not be null
bag
,
final Predicate<? super E>
the predicate to use for validation, must not be null
predicate
)

Factory method to create a predicated (validating) bag.

public boolean
remove(final Object
the object to remove
object
,
final int
the number of copies to remove
count
)

Implements org.apache.commons.collections4.Bag.remove.

Removes nCopies copies of the specified object from the Bag.
public Set<E>
uniqueSet()

Implements org.apache.commons.collections4.Bag.uniqueSet.

Returns a Set of unique elements in the Bag.
Inherited from org.apache.commons.collections4.collection.PredicatedCollection:
addaddAllbuildernotNullBuilderpredicatedCollectionvalidate

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

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

Serialization version

Constructor Detail

PredicatedBagback to summary
protected PredicatedBag(final Bag<E> bag, final Predicate<? super E> predicate)

Constructor that wraps (not copies).

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

Parameters
bag:Bag<E>

the bag to decorate, must not be null

predicate:Predicate<? super E>

the predicate to use for validation, must not be null

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

Method Detail

addback to summary
public boolean add(final E object, final int count)

Implements org.apache.commons.collections4.Bag.add.

Doc from org.apache.commons.collections4.Bag.add.

Adds nCopies copies of the specified object to the Bag.

If the object is already in the uniqueSet() then increment its count as reported by getCount(Object). Otherwise add it to the uniqueSet() and report its count as nCopies.

Parameters
object:E

the object to add

count:int

the number of copies to add

Returns:boolean

true if the object was not already in the uniqueSet

Annotations
@Override
decoratedback to summary
protected Bag<E> decorated()

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

Gets the decorated bag.

Returns:Bag<E>

the decorated bag

Annotations
@Override
equalsback to summary
public boolean equals(final Object object)

Overrides java.lang.Object.equals.

Implements java.util.Collection.equals.

Doc from java.util.Collection.equals.

Compares the specified object with this collection for equality.

While the Collection interface adds no stipulations to the general contract for the Object.equals, programmers who implement the Collection interface "directly" (in other words, create a class that is a Collection but is not a Set or a List) must exercise care if they choose to override the Object.equals. It is not necessary to do so, and the simplest course of action is to rely on Object's implementation, but the implementor may wish to implement a "value comparison" in place of the default "reference comparison." (The List and Set interfaces mandate such value comparisons.)

The general contract for the Object.equals method states that equals must be symmetric (in other words, a.equals(b) if and only if b.equals(a)). The contracts for List.equals and Set.equals state that lists are only equal to other lists, and sets to other sets. Thus, a custom equals method for a collection class that implements neither the List nor Set interface must return false when this collection is compared to any list or set. (By the same logic, it is not possible to write a class that correctly implements both the Set and List interfaces.)

Parameters
object:Object

object to be compared for equality with this collection

Returns:boolean

true if the specified object is equal to this collection

Annotations
@Override
getCountback to summary
public int getCount(final Object object)

Implements org.apache.commons.collections4.Bag.getCount.

Doc from org.apache.commons.collections4.Bag.getCount.

Returns the number of occurrences (cardinality) of the given object currently in the bag. If the object does not exist in the bag, return 0.

Parameters
object:Object

the object to search for

Returns:int

the number of occurrences of the object, zero if not found

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Collection.hashCode.

Doc from java.util.Collection.hashCode.

Returns the hash code value for this collection. While the Collection interface adds no stipulations to the general contract for the Object.hashCode method, programmers should take note that any class that overrides the Object.equals method must also override the Object.hashCode method in order to satisfy the general contract for the Object.hashCode method. In particular, c1.equals(c2) implies that c1.hashCode()==c2.hashCode().

Returns:int

the hash code value for this collection

Annotations
@Override
predicatedBagback to summary
public static <E> PredicatedBag<E> predicatedBag(final Bag<E> bag, final Predicate<? super E> predicate)

Factory method to create a predicated (validating) bag.

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

Parameters
<E>
the type of the elements in the bag
bag:Bag<E>

the bag to decorate, must not be null

predicate:Predicate<? super E>

the predicate to use for validation, must not be null

Returns:PredicatedBag<E>

a new predicated Bag

Exceptions
NullPointerException:
if bag or predicate is null
IllegalArgumentException:
if the bag contains invalid elements
Since
4.0
removeback to summary
public boolean remove(final Object object, final int count)

Implements org.apache.commons.collections4.Bag.remove.

Doc from org.apache.commons.collections4.Bag.remove.

Removes nCopies copies of the specified object from the Bag.

If the number of copies to remove is greater than the actual number of copies in the Bag, no error is thrown.

Parameters
object:Object

the object to remove

count:int

the number of copies to remove

Returns:boolean

true if this call changed the collection

Annotations
@Override
uniqueSetback to summary
public Set<E> uniqueSet()

Implements org.apache.commons.collections4.Bag.uniqueSet.

Doc from org.apache.commons.collections4.Bag.uniqueSet.

Returns a Set of unique elements in the Bag.

Uniqueness constraints are the same as those in java.util.Set.

Returns:Set<E>

the Set of unique Bag elements

Annotations
@Override