Class CollectionSortedBag<E>
- java.lang.Object
-
- org.apache.commons.collections4.collection.AbstractCollectionDecorator<E>
-
- org.apache.commons.collections4.bag.AbstractBagDecorator<E>
-
- org.apache.commons.collections4.bag.AbstractSortedBagDecorator<E>
-
- org.apache.commons.collections4.bag.CollectionSortedBag<E>
-
- Type Parameters:
E
- the type of elements in this bag
- All Implemented Interfaces:
Serializable
,Iterable<E>
,Collection<E>
,Bag<E>
,SortedBag<E>
public final class CollectionSortedBag<E> extends AbstractSortedBagDecorator<E>
Decorates anotherSortedBag
to comply with the Collection contract.- Since:
- 4.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description CollectionSortedBag(SortedBag<E> bag)
Constructor that wraps (not copies).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(E object)
(Violation) Adds one copy of the specified object to the Bag.boolean
add(E object, int count)
AddsnCopies
copies of the specified object to the Bag.boolean
addAll(Collection<? extends E> coll)
static <E> SortedBag<E>
collectionSortedBag(SortedBag<E> bag)
Factory method to create a sorted bag that complies to the Collection contract.boolean
containsAll(Collection<?> coll)
(Violation) Returnstrue
if the bag contains all elements in the given collection, respecting cardinality.boolean
remove(Object object)
(Violation) Removes all occurrences of the given object from the bag.boolean
removeAll(Collection<?> coll)
(Violation) Remove all elements represented in the given collection, respecting cardinality.boolean
retainAll(Collection<?> coll)
(Violation) Remove any members of the bag that are not in the given collection, respecting cardinality.-
Methods inherited from class org.apache.commons.collections4.bag.AbstractSortedBagDecorator
comparator, decorated, first, last
-
Methods inherited from class org.apache.commons.collections4.bag.AbstractBagDecorator
equals, getCount, hashCode, remove, uniqueSet
-
Methods inherited from class org.apache.commons.collections4.collection.AbstractCollectionDecorator
clear, contains, isEmpty, iterator, setCollection, 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.collections4.Bag
getCount, iterator, remove, size, uniqueSet
-
Methods inherited from interface java.util.Collection
clear, contains, equals, hashCode, isEmpty, parallelStream, removeIf, spliterator, stream, toArray, toArray, toArray
-
-
-
-
Constructor Detail
-
CollectionSortedBag
public CollectionSortedBag(SortedBag<E> bag)
Constructor that wraps (not copies).- Parameters:
bag
- the sorted bag to decorate, must not be null- Throws:
NullPointerException
- if bag is null
-
-
Method Detail
-
collectionSortedBag
public static <E> SortedBag<E> collectionSortedBag(SortedBag<E> bag)
Factory method to create a sorted bag that complies to the Collection contract.- Type Parameters:
E
- the type of the elements in the bag- Parameters:
bag
- the sorted bag to decorate, must not be null- Returns:
- a SortedBag that complies to the Collection contract
- Throws:
NullPointerException
- if bag is null
-
containsAll
public boolean containsAll(Collection<?> coll)
Description copied from interface:Bag
(Violation) Returnstrue
if the bag contains all elements in the given collection, respecting cardinality. That is, if the given collectioncoll
containsn
copies of a given object, callingBag.getCount(Object)
on that object must be>= n
for alln
incoll
.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.- Specified by:
containsAll
in interfaceBag<E>
- Specified by:
containsAll
in interfaceCollection<E>
- Overrides:
containsAll
in classAbstractCollectionDecorator<E>
- Parameters:
coll
- the collection to check against- Returns:
true
if the Bag contains all the collection
-
add
public boolean add(E object)
Description copied from interface:Bag
(Violation) Adds one copy 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 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<E>
- Specified by:
add
in interfaceCollection<E>
- Overrides:
add
in classAbstractCollectionDecorator<E>
- Parameters:
object
- the object to add- Returns:
true
if the object was not already in theuniqueSet
-
addAll
public boolean addAll(Collection<? extends E> coll)
- Specified by:
addAll
in interfaceCollection<E>
- Overrides:
addAll
in classAbstractCollectionDecorator<E>
-
remove
public boolean remove(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<E>
- Specified by:
remove
in interfaceCollection<E>
- Overrides:
remove
in classAbstractCollectionDecorator<E>
- Parameters:
object
- the object to remove- Returns:
true
if this call changed the collection
-
removeAll
public boolean removeAll(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<E>
- Specified by:
removeAll
in interfaceCollection<E>
- Overrides:
removeAll
in classAbstractCollectionDecorator<E>
- Parameters:
coll
- the collection to remove- Returns:
true
if this call changed the collection
-
retainAll
public boolean retainAll(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<E>
- Specified by:
retainAll
in interfaceCollection<E>
- Overrides:
retainAll
in classAbstractCollectionDecorator<E>
- Parameters:
coll
- the collection to retain- Returns:
true
if this call changed the collection
-
add
public boolean add(E 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
.
-
-