Class UnmodifiableSortedBag<E>
- Type Parameters:
E- the type of elements in this bag
- All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Bag<E>, SortedBag<E>, Unmodifiable
SortedBag to ensure it can't be altered.
This class is Serializable from Commons Collections 3.1.
Attempts to modify it will result in an UnsupportedOperationException.
- Since:
- 3.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final longSerialization version -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateUnmodifiableSortedBag(SortedBag<E> bag) Constructor that wraps (not copies). -
Method Summary
Modifier and TypeMethodDescriptionboolean(Violation) Adds one copy of the specified object to the Bag.booleanAddsnCopiescopies of the specified object to the Bag.booleanaddAll(Collection<? extends E> coll) voidclear()iterator()Returns anIteratorover the entire set of members, including copies due to cardinality.private voidRead the collection in using a custom routine.boolean(Violation) Removes all occurrences of the given object from the bag.booleanRemovesnCopiescopies of the specified object from the Bag.booleanremoveAll(Collection<?> coll) (Violation) Remove all elements represented in the given collection, respecting cardinality.booleanbooleanretainAll(Collection<?> coll) (Violation) Remove any members of the bag that are not in the given collection, respecting cardinality.Returns aSetof unique elements in the Bag.static <E> SortedBag<E> unmodifiableSortedBag(SortedBag<E> bag) Factory method to create an unmodifiable bag.private voidWrite the collection out using a custom routine.Methods inherited from class AbstractSortedBagDecorator
comparator, decorated, first, lastMethods inherited from class AbstractBagDecorator
equals, getCount, hashCodeMethods inherited from class AbstractCollectionDecorator
contains, containsAll, isEmpty, setCollection, size, toArray, toArray, toStringMethods inherited from interface Bag
containsAll, getCount, sizeMethods inherited from interface Collection
contains, equals, hashCode, isEmpty, parallelStream, spliterator, stream, toArray, toArray
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerialization version- See Also:
-
-
Constructor Details
-
UnmodifiableSortedBag
Constructor that wraps (not copies).- Parameters:
bag- the bag to decorate, must not be null- Throws:
NullPointerException- if bag is null
-
-
Method Details
-
unmodifiableSortedBag
Factory method to create an unmodifiable bag.If the bag passed in is already unmodifiable, it is returned.
- Type Parameters:
E- the type of the elements in the bag- Parameters:
bag- the bag to decorate, must not be null- Returns:
- an unmodifiable SortedBag
- Throws:
NullPointerException- if bag is null- Since:
- 4.0
-
writeObject
Write the collection out using a custom routine.- Parameters:
out- the output stream- Throws:
IOException- if an error occurs while writing to the stream
-
readObject
Read the collection in using a custom routine.- Parameters:
in- the input stream- Throws:
IOException- if an error occurs while reading from the streamClassNotFoundException- if an object read from the stream can not be loadedClassCastException- if deserialised object has wrong type
-
iterator
-
add
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:
addin interfaceBag<E>- Specified by:
addin interfaceCollection<E>- Overrides:
addin classAbstractCollectionDecorator<E>- Parameters:
object- the object to add- Returns:
trueif the object was not already in theuniqueSet
-
addAll
- Specified by:
addAllin interfaceCollection<E>- Overrides:
addAllin classAbstractCollectionDecorator<E>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractCollectionDecorator<E>
-
remove
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:
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
-
removeIf
- Specified by:
removeIfin interfaceCollection<E>- Overrides:
removeIfin classAbstractCollectionDecorator<E>- Since:
- 4.4
-
removeAll
Description copied from interface:Bag(Violation) Remove all elements represented in the given collection, respecting cardinality. That is, if the given collectioncollcontainsncopies of a given object, the bag will havenfewer copies, assuming the bag had at leastncopies 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:
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
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 collectioncollcontainsncopies of a given object and the bag hasm > ncopies, then deletem - ncopies from the bag. In addition, ifeis an object in the bag but!coll.contains(e), then removeeand 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:
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
-
add
Description copied from interface:BagAddsnCopiescopies 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. -
remove
Description copied from interface:BagRemovesnCopiescopies 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.
-
uniqueSet
Description copied from interface:Bag
-