Class UnmodifiableBag
- java.lang.Object
-
- org.apache.commons.collections.collection.AbstractCollectionDecorator
-
- org.apache.commons.collections.bag.AbstractBagDecorator
-
- org.apache.commons.collections.bag.UnmodifiableBag
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable
,java.util.Collection
,Bag
,Unmodifiable
public final class UnmodifiableBag extends AbstractBagDecorator implements Unmodifiable, java.io.Serializable
Decorates anotherBag
to ensure it can't be altered.This class is Serializable from Commons Collections 3.1.
- Since:
- Commons Collections 3.0
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static long
serialVersionUID
Serialization version-
Fields inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
collection
-
-
Constructor Summary
Constructors Modifier Constructor Description private
UnmodifiableBag(Bag bag)
Constructor that wraps (not copies).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(java.lang.Object object)
(Violation) Adds one copy the specified object to the Bag.boolean
add(java.lang.Object object, int count)
AddsnCopies
copies of the specified object to the Bag.boolean
addAll(java.util.Collection coll)
void
clear()
static Bag
decorate(Bag bag)
Factory method to create an unmodifiable bag.java.util.Iterator
iterator()
Returns anIterator
over the entire set of members, including copies due to cardinality.private void
readObject(java.io.ObjectInputStream in)
Read the collection in using a custom routine.boolean
remove(java.lang.Object object)
(Violation) Removes all occurrences of the given object from the bag.boolean
remove(java.lang.Object object, int count)
RemovesnCopies
copies of the specified object from the Bag.boolean
removeAll(java.util.Collection coll)
(Violation) Remove all elements represented in the given collection, respecting cardinality.boolean
retainAll(java.util.Collection coll)
(Violation) Remove any members of the bag that are not in the given collection, respecting cardinality.java.util.Set
uniqueSet()
Returns aSet
of unique elements in the Bag.private void
writeObject(java.io.ObjectOutputStream out)
Write the collection out using a custom routine.-
Methods inherited from class org.apache.commons.collections.bag.AbstractBagDecorator
getBag, getCount
-
Methods inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
contains, containsAll, equals, getCollection, hashCode, isEmpty, size, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.collections.Bag
containsAll, size
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Serialization version- See Also:
- Constant Field Values
-
-
Constructor Detail
-
UnmodifiableBag
private UnmodifiableBag(Bag bag)
Constructor that wraps (not copies).- Parameters:
bag
- the bag to decorate, must not be null- Throws:
java.lang.IllegalArgumentException
- if bag is null
-
-
Method Detail
-
decorate
public static Bag decorate(Bag bag)
Factory method to create an unmodifiable bag.If the bag passed in is already unmodifiable, it is returned.
- Parameters:
bag
- the bag to decorate, must not be null- Returns:
- an unmodifiable Bag
- Throws:
java.lang.IllegalArgumentException
- if bag is null
-
writeObject
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
Write the collection out using a custom routine.- Parameters:
out
- the output stream- Throws:
java.io.IOException
-
readObject
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
Read the collection in using a custom routine.- Parameters:
in
- the input stream- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
iterator
public java.util.Iterator iterator()
Description copied from interface:Bag
Returns anIterator
over the entire set of members, including copies due to cardinality. This iterator is fail-fast and will not tolerate concurrent modifications.- Specified by:
iterator
in interfaceBag
- Specified by:
iterator
in interfacejava.util.Collection
- Specified by:
iterator
in interfacejava.lang.Iterable
- Overrides:
iterator
in classAbstractCollectionDecorator
- Returns:
- iterator over all elements in the Bag
-
add
public boolean add(java.lang.Object object)
Description copied from interface:Bag
(Violation) Adds one copy the specified object to the Bag.If the object is already in the
Bag.uniqueSet()
then increment its count as reported byBag.getCount(Object)
. Otherwise add it to theBag.uniqueSet()
and report its count as 1.Since this method always increases the size of the bag, according to the
Collection.add(Object)
contract, it should always returntrue
. Since it sometimes returnsfalse
, this method violates the contract.- Specified by:
add
in interfaceBag
- Specified by:
add
in interfacejava.util.Collection
- Overrides:
add
in classAbstractCollectionDecorator
- Parameters:
object
- the object to add- Returns:
true
if the object was not already in theuniqueSet
-
addAll
public boolean addAll(java.util.Collection coll)
- Specified by:
addAll
in interfacejava.util.Collection
- Overrides:
addAll
in classAbstractCollectionDecorator
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Collection
- Overrides:
clear
in classAbstractCollectionDecorator
-
remove
public boolean remove(java.lang.Object object)
Description copied from interface:Bag
(Violation) Removes all occurrences of the given object from the bag.This will also remove the object from the
Bag.uniqueSet()
.According to the
Collection.remove(Object)
method, this method should only remove the first occurrence of the given object, not all occurrences.- Specified by:
remove
in interfaceBag
- Specified by:
remove
in interfacejava.util.Collection
- Overrides:
remove
in classAbstractCollectionDecorator
- Returns:
true
if this call changed the collection
-
removeAll
public boolean removeAll(java.util.Collection coll)
Description copied from interface:Bag
(Violation) Remove all elements represented in the given collection, respecting cardinality. That is, if the given collectioncoll
containsn
copies of a given object, the bag will haven
fewer copies, assuming the bag had at leastn
copies to begin with.The
Collection.removeAll(Collection)
method specifies that cardinality should not be respected; this method should remove all occurrences of every object contained in the given collection.- Specified by:
removeAll
in interfaceBag
- Specified by:
removeAll
in interfacejava.util.Collection
- Overrides:
removeAll
in classAbstractCollectionDecorator
- Parameters:
coll
- the collection to remove- Returns:
true
if this call changed the collection
-
retainAll
public boolean retainAll(java.util.Collection coll)
Description copied from interface:Bag
(Violation) Remove any members of the bag that are not in the given collection, respecting cardinality. That is, if the given collectioncoll
containsn
copies of a given object and the bag hasm > n
copies, then deletem - n
copies from the bag. In addition, ife
is an object in the bag but!coll.contains(e)
, then removee
and any of its copies.The
Collection.retainAll(Collection)
method specifies that cardinality should not be respected; this method should keep all occurrences of every object contained in the given collection.- Specified by:
retainAll
in interfaceBag
- Specified by:
retainAll
in interfacejava.util.Collection
- Overrides:
retainAll
in classAbstractCollectionDecorator
- Parameters:
coll
- the collection to retain- Returns:
true
if this call changed the collection
-
add
public boolean add(java.lang.Object object, int count)
Description copied from interface:Bag
AddsnCopies
copies of the specified object to the Bag.If the object is already in the
Bag.uniqueSet()
then increment its count as reported byBag.getCount(Object)
. Otherwise add it to theBag.uniqueSet()
and report its count asnCopies
.- Specified by:
add
in interfaceBag
- Overrides:
add
in classAbstractBagDecorator
- Parameters:
object
- the object to addcount
- the number of copies to add- Returns:
true
if the object was not already in theuniqueSet
-
remove
public boolean remove(java.lang.Object object, int count)
Description copied from interface:Bag
RemovesnCopies
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.
- Specified by:
remove
in interfaceBag
- Overrides:
remove
in classAbstractBagDecorator
- Parameters:
object
- the object to removecount
- the number of copies to remove- Returns:
true
if this call changed the collection
-
uniqueSet
public java.util.Set uniqueSet()
Description copied from interface:Bag
Returns aSet
of unique elements in the Bag.Uniqueness constraints are the same as those in
Set
.- Specified by:
uniqueSet
in interfaceBag
- Overrides:
uniqueSet
in classAbstractBagDecorator
- Returns:
- the Set of unique Bag elements
-
-