Class CollectionBag<E>
- Type Parameters:
E- the type of elements in this bag
- All Implemented Interfaces:
Serializable,Iterable<E>,Collection<E>,Bag<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.
- Since:
- 4.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean(Change) Adds one copy of the specified object to the Bag.boolean(Change) Addscountcopies of the specified object to the Bag.booleanaddAll(Collection<? extends E> coll) static <E> Bag<E> collectionBag(Bag<E> bag) Factory method to create a bag that complies to the Collection contract.booleancontainsAll(Collection<?> coll) (Change) Returnstrueif the bag contains all elements in the given collection, not respecting cardinality.boolean(Change) Removes the first occurrence of the given object from the bag.booleanremoveAll(Collection<?> coll) (Change) Remove all elements represented in the given collection, not respecting cardinality.booleanretainAll(Collection<?> coll) (Change) Remove any members of the bag that are not in the given collection, not respecting cardinality.Methods inherited from class org.apache.commons.collections4.bag.AbstractBagDecorator
decorated, equals, getCount, hashCode, remove, uniqueSetMethods inherited from class org.apache.commons.collections4.collection.AbstractCollectionDecorator
clear, contains, isEmpty, iterator, removeIf, setCollection, size, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
clear, contains, isEmpty, parallelStream, removeIf, spliterator, stream, toArray, toArray
-
Constructor Details
-
CollectionBag
Constructor that wraps (not copies).- Parameters:
bag- the bag to decorate, must not be null- Throws:
NullPointerException- if bag is null
-
-
Method Details
-
collectionBag
Factory method to create a bag that complies to the Collection contract.- Type Parameters:
E- the type of the elements in the bag- Parameters:
bag- the bag to decorate, must not be null- Returns:
- a Bag that complies to the Collection contract
- Throws:
NullPointerException- if bag is null
-
add
(Change) Adds one copy of the specified object to the Bag.Since this method always increases the size of the bag, it will always return
true.- Specified by:
addin interfaceBag<E>- Specified by:
addin interfaceCollection<E>- Overrides:
addin classAbstractCollectionDecorator<E>- Parameters:
object- the object to add- Returns:
true, always- Throws:
ClassCastException- if the class of the specified element prevents it from being added to this collection
-
add
(Change) Addscountcopies of the specified object to the Bag.Since this method always increases the size of the bag, it will always return
true.- Specified by:
addin interfaceBag<E>- Overrides:
addin classAbstractBagDecorator<E>- Parameters:
object- the object to addcount- the number of copies to add- Returns:
true, always- Throws:
ClassCastException- if the class of the specified element prevents it from being added to this collection
-
addAll
- Specified by:
addAllin interfaceCollection<E>- Overrides:
addAllin classAbstractCollectionDecorator<E>
-
containsAll
(Change) Returnstrueif the bag contains all elements in the given collection, not respecting cardinality. That is, if the given collectioncollcontains at least one of every object contained in this object.- Specified by:
containsAllin interfaceBag<E>- Specified by:
containsAllin interfaceCollection<E>- Overrides:
containsAllin classAbstractCollectionDecorator<E>- Parameters:
coll- the collection to check against- Returns:
trueif the Bag contains at least one of every object in the collection
-
remove
(Change) Removes the first occurrence of the given object from the bag.This will also remove the object from the
AbstractBagDecorator.uniqueSet()if the bag contains no occurrence anymore of the object after this operation.- Specified by:
removein interfaceBag<E>- Specified by:
removein interfaceCollection<E>- Overrides:
removein classAbstractCollectionDecorator<E>- Parameters:
object- the object to remove- Returns:
trueif this call changed the collection
-
removeAll
(Change) Remove all elements represented in the given collection, not respecting cardinality. That is, remove all occurrences of every object contained in the given collection.- Specified by:
removeAllin interfaceBag<E>- Specified by:
removeAllin interfaceCollection<E>- Overrides:
removeAllin classAbstractCollectionDecorator<E>- Parameters:
coll- the collection to remove- Returns:
trueif this call changed the collection
-
retainAll
(Change) Remove any members of the bag that are not in the given collection, not respecting cardinality. That is, any object in the given collectioncollwill 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 forcollthat provides a fast (for example O(1)) implementation ofCollection.contains(Object).- Specified by:
retainAllin interfaceBag<E>- Specified by:
retainAllin interfaceCollection<E>- Overrides:
retainAllin classAbstractCollectionDecorator<E>- Parameters:
coll- the collection to retain- Returns:
trueif this call changed the collection
-