public final class CollectionBag<E> extends AbstractBagDecorator<E>
Bag
to comply with the Collection contract.Modifier and Type | Field and Description |
---|---|
private static long |
serialVersionUID
Serialization version
|
Constructor and Description |
---|
CollectionBag(Bag<E> bag)
Constructor that wraps (not copies).
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object)
(Violation)
Adds one copy of the specified object to the Bag.
|
boolean |
add(E object,
int count)
Adds
nCopies copies of the specified object to the Bag. |
boolean |
addAll(java.util.Collection<? extends E> coll) |
static <E> Bag<E> |
collectionBag(Bag<E> bag)
Factory method to create a bag that complies to the Collection contract.
|
boolean |
containsAll(java.util.Collection<?> coll)
(Violation)
Returns
true if the bag contains all elements in
the given collection, respecting 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 |
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.
|
private void |
writeObject(java.io.ObjectOutputStream out)
Write the collection out using a custom routine.
|
decorated, getCount, remove, uniqueSet
clear, contains, equals, hashCode, isEmpty, iterator, setCollection, size, toArray, toArray, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
private static final long serialVersionUID
public static <E> Bag<E> collectionBag(Bag<E> bag)
E
- the type of the elements in the bagbag
- the bag to decorate, must not be nulljava.lang.IllegalArgumentException
- if bag is nullprivate void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
out
- the output streamjava.io.IOException
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
in
- the input streamjava.io.IOException
java.lang.ClassNotFoundException
java.lang.ClassCastException
- if deserialised object has wrong typepublic boolean containsAll(java.util.Collection<?> coll)
Bag
true
if the bag contains all elements in
the given collection, respecting cardinality. That is, if the
given collection coll
contains n
copies
of a given object, calling Bag.getCount(Object)
on that object must
be >= n
for all n
in coll
.
The Collection.containsAll(Collection)
method specifies
that cardinality should not be respected; this method should
return true if the bag contains at least one of every object contained
in the given collection.
containsAll
in interface java.util.Collection<E>
containsAll
in interface Bag<E>
containsAll
in class AbstractCollectionDecorator<E>
coll
- the collection to check againsttrue
if the Bag contains all the collectionpublic boolean add(E 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.
public boolean addAll(java.util.Collection<? extends E> coll)
addAll
in interface java.util.Collection<E>
addAll
in class AbstractCollectionDecorator<E>
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.
public 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.
public 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.
public boolean add(E 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
.