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

public Class SynchronizedBag<E>

extends SynchronizedCollection<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.SynchronizedSortedBag
Type Parameters
<E>
the type of elements in this bag
Imports
java.util.Set, org.apache.commons.collections4.Bag, org.apache.commons.collections4.collection.SynchronizedCollection

Decorates another Bag to synchronize its behaviour for a multi-threaded environment.

Methods are synchronized, then forwarded to the decorated bag. Iterators must be separately synchronized around the loop.

This class is Serializable from Commons Collections 3.1.

Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
SynchronizedBag.SynchronizedBagSet

Synchronized Set for the Bag class.

Field Summary

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

Constructor Summary

AccessConstructor and Description
protected
SynchronizedBag(final Bag<E>
the bag to decorate, must not be null
bag
)

Constructor that wraps (not copies).

protected
SynchronizedBag(final Bag<E>
the bag to decorate, must not be null
bag
,
final Object
the lock to use, must not be null
lock
)

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.
public boolean
equals(final Object
object to be compared for equality with this collection
object
)

Overrides org.apache.commons.collections4.collection.SynchronizedCollection.equals.

Implements java.util.Collection.equals.

Compares the specified object with this collection for equality.
protected Bag<E>

Returns:

the decorated bag
getBag
()

Gets the bag being decorated.

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 org.apache.commons.collections4.collection.SynchronizedCollection.hashCode.

Implements java.util.Collection.hashCode.

Returns the hash code value for this collection.
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 static <
the type of the elements in the bag
E
>
SynchronizedBag<E>

Returns:

a new synchronized Bag
synchronizedBag
(final Bag<E>
the bag to decorate, must not be null
bag
)

Factory method to create a synchronized 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.SynchronizedCollection:
addaddAllclearcontainscontainsAlldecoratedisEmptyiteratorremoveremoveAllremoveIfretainAllsizesynchronizedCollectiontoArraytoArraytoString

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

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

Serialization version

Constructor Detail

SynchronizedBagback to summary
protected SynchronizedBag(final Bag<E> bag)

Constructor that wraps (not copies).

Parameters
bag:Bag<E>

the bag to decorate, must not be null

Exceptions
NullPointerException:
if bag is null
SynchronizedBagback to summary
protected SynchronizedBag(final Bag<E> bag, final Object lock)

Constructor that wraps (not copies).

Parameters
bag:Bag<E>

the bag to decorate, must not be null

lock:Object

the lock to use, must not be null

Exceptions
NullPointerException:
if bag or lock is null

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
equalsback to summary
public boolean equals(final Object object)

Overrides org.apache.commons.collections4.collection.SynchronizedCollection.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
getBagback to summary
protected Bag<E> getBag()

Gets the bag being decorated.

Returns:Bag<E>

the decorated bag

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 org.apache.commons.collections4.collection.SynchronizedCollection.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
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
synchronizedBagback to summary
public static <E> SynchronizedBag<E> synchronizedBag(final Bag<E> bag)

Factory method to create a synchronized bag.

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

the bag to decorate, must not be null

Returns:SynchronizedBag<E>

a new synchronized Bag

Exceptions
NullPointerException:
if bag is null
Since
4.0
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
org.apache.commons.collections4.bag back to summary

pack-priv Class SynchronizedBag.SynchronizedBagSet

extends SynchronizedCollection<E>
implements Set<E>
Class Inheritance
All Implemented Interfaces
java.util.Set, java.util.Collection, java.lang.Iterable

Synchronized Set for the Bag class.

Field Summary

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

Constructor Summary

AccessConstructor and Description
pack-priv
SynchronizedBagSet(final Set<E>
the set to decorate
set
,
final Object
the lock to use, shared with the bag
lock
)

Constructor.

Method Summary

Inherited from org.apache.commons.collections4.collection.SynchronizedCollection:
addaddAllclearcontainscontainsAlldecoratedequalshashCodeisEmptyiteratorremoveremoveAllremoveIfretainAllsizesynchronizedCollectiontoArraytoArraytoString

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

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

Serialization version

Constructor Detail

SynchronizedBagSetback to summary
pack-priv SynchronizedBagSet(final Set<E> set, final Object lock)

Constructor.

Parameters
set:Set<E>

the set to decorate

lock:Object

the lock to use, shared with the bag