E - the type held in the bag
public final class CollectionBag<E> extends AbstractBagDecorator<E>
Bag to comply with the Collection contract.
By decorating an existing Bag instance with a CollectionBag, it can be safely passed on to methods that require Collection types that are fully compliant with the Collection contract.
The method javadoc highlights the differences compared to the original Bag interface.
Bag,
Serialized Form
| Constructor and Description |
|---|
CollectionBag(Bag
Constructor that wraps (not copies).
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E object)
(Change) Adds one copy of the specified object to the Bag.
|
boolean |
add(E object, int count)
(Change) Adds
count copies of the specified object to the Bag.
|
boolean |
addAll(Collection
|
static <E> Bag |
collectionBag(Bag
Factory method to create a bag that complies to the Collection contract.
|
boolean |
containsAll(Collection
(Change) Returns
true if the bag contains all elements in the given collection,
not respecting cardinality.
|
boolean |
remove(Object
(Change) Removes the first occurrence of the given object from the bag.
|
boolean |
removeAll(Collection
(Change) Remove all elements represented in the given collection,
not respecting cardinality.
|
boolean |
retainAll(Collection
(Change) Remove any members of the bag that are not in the given collection,
not respecting cardinality.
|
decorated, equals, getCount, hashCode, remove, uniqueSetclear, contains, isEmpty, iterator, setCollection, size, toArray, toArray, toStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitclear, contains, isEmpty, parallelStream, removeIf, spliterator, stream, toArray, toArraypublic CollectionBag(Bag<E> bag)
bag - the bag to decorate, must not be null
NullPointerException - if bag is null
public static <E> Bag<E> collectionBag(Bag <E> bag)
E - the type of the elements in the bag
bag - the bag to decorate, must not be null
NullPointerException - if bag is null
public boolean containsAll(Collection<?> coll)
true if the bag contains all elements in the given collection,
not respecting cardinality. That is, if the given collection
coll contains at least one of every object contained in this object.
containsAll in interface
Collection<E>
containsAll in interface
Bag<E>
containsAll in class
AbstractCollectionDecorator<E>
coll - the collection to check against
true if the Bag contains at least one of every object in the collection
public boolean add(E object)
Since this method always increases the size of the bag, it will always return true.
add in interface
Collection<E>
add in interface
Bag<E>
add in class
AbstractCollectionDecorator<E>
object - the object to add
true, always
public boolean addAll(Collection<? extends E> coll)
public boolean remove(Objectobject)
This will also remove the object from the AbstractBagDecorator if the bag contains no occurrence anymore of the object after this operation.
remove in interface
Collection<E>
remove in interface
Bag<E>
remove in class
AbstractCollectionDecorator<E>
object - the object to remove
true if this call changed the collection
public boolean removeAll(Collection<?> coll)
removeAll in interface
Collection<E>
removeAll in interface
Bag<E>
removeAll in class
AbstractCollectionDecorator<E>
coll - the collection to remove
true if this call changed the collection
public boolean retainAll(Collection<?> coll)
coll will be retained in the bag with the same number of copies prior to this operation. All other objects will be completely removed from this bag.
This implementation iterates over the elements of this bag, checking each element in turn to see if it's contained in coll. If it's not contained, it's removed from this bag. As a consequence, it is advised to use a collection type for coll that provides a fast (e.g. O(1)) implementation of Collection.
retainAll in interface
Collection<E>
retainAll in interface
Bag<E>
retainAll in class
AbstractCollectionDecorator<E>
coll - the collection to retain
true if this call changed the collection
public boolean add(E object, int count)
count copies of the specified object to the Bag.
Since this method always increases the size of the bag, it will always return true.