public final class UnmodifiableBag extends AbstractBagDecorator implements Unmodifiable, java.io.Serializable
Bag
to ensure it can't be altered.
This class is Serializable from Commons Collections 3.1.
Modifier and Type | Method and 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)
Adds
nCopies 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 an
Iterator over the entire set of members,
including copies due to cardinality. |
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)
Removes
nCopies 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 a
Set of unique elements in the Bag. |
getCount
contains, containsAll, equals, hashCode, isEmpty, size, toArray, toArray, toString
containsAll, size
public static Bag decorate(Bag bag)
If the bag passed in is already unmodifiable, it is returned.
bag
- the bag to decorate, must not be nulljava.lang.IllegalArgumentException
- if bag is nullpublic java.util.Iterator iterator()
Bag
Iterator
over the entire set of members,
including copies due to cardinality. This iterator is fail-fast
and will not tolerate concurrent modifications.iterator
in interface java.lang.Iterable
iterator
in interface java.util.Collection
iterator
in interface Bag
iterator
in class AbstractCollectionDecorator
public boolean add(java.lang.Object object)
Bag
If the object is already in the Bag.uniqueSet()
then increment its
count as reported by Bag.getCount(Object)
. Otherwise add it to the
Bag.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 return true
. Since it sometimes returns
false
, this method violates the contract.
add
in interface java.util.Collection
add
in interface Bag
add
in class AbstractCollectionDecorator
object
- the object to addtrue
if the object was not already in the uniqueSet
public boolean addAll(java.util.Collection coll)
addAll
in interface java.util.Collection
addAll
in class AbstractCollectionDecorator
public void clear()
clear
in interface java.util.Collection
clear
in class AbstractCollectionDecorator
public boolean remove(java.lang.Object object)
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.
remove
in interface java.util.Collection
remove
in interface Bag
remove
in class AbstractCollectionDecorator
true
if this call changed the collectionpublic boolean removeAll(java.util.Collection coll)
Bag
coll
contains n
copies of a given object,
the bag will have n
fewer copies, assuming the bag
had at least n
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.
removeAll
in interface java.util.Collection
removeAll
in interface Bag
removeAll
in class AbstractCollectionDecorator
coll
- the collection to removetrue
if this call changed the collectionpublic boolean retainAll(java.util.Collection coll)
Bag
coll
contains n
copies of a
given object and the bag has m > n
copies, then
delete m - n
copies from the bag. In addition, if
e
is an object in the bag but
!coll.contains(e)
, then remove e
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.
retainAll
in interface java.util.Collection
retainAll
in interface Bag
retainAll
in class AbstractCollectionDecorator
coll
- the collection to retaintrue
if this call changed the collectionpublic boolean add(java.lang.Object object, int count)
Bag
nCopies
copies of the specified object to the Bag.
If the object is already in the Bag.uniqueSet()
then increment its
count as reported by Bag.getCount(Object)
. Otherwise add it to the
Bag.uniqueSet()
and report its count as nCopies
.
add
in interface Bag
add
in class AbstractBagDecorator
object
- the object to addcount
- the number of copies to addtrue
if the object was not already in the uniqueSet
public boolean remove(java.lang.Object object, int count)
Bag
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.
remove
in interface Bag
remove
in class AbstractBagDecorator
object
- the object to removecount
- the number of copies to removetrue
if this call changed the collectionpublic java.util.Set uniqueSet()
Bag
Set
of unique elements in the Bag.
Uniqueness constraints are the same as those in Set
.
uniqueSet
in interface Bag
uniqueSet
in class AbstractBagDecorator
"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"