public final class UnmodifiableBag<E> extends AbstractBagDecorator<E> implements Unmodifiable
Bag 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.
| 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(Collection
|
void |
clear()
|
Iterator |
iterator()
Returns an
Iterator over the entire set of members, including copies due to cardinality.
|
boolean |
remove(Object
(Violation) Removes all occurrences of the given object from the bag.
|
boolean |
remove(Object
Removes
nCopies copies of the specified object from the Bag.
|
boolean |
removeAll(Collection
(Violation) Remove all elements represented in the given collection, respecting cardinality.
|
boolean |
retainAll(Collection
(Violation) Remove any members of the bag that are not in the given collection, respecting cardinality.
|
Set |
uniqueSet()
Returns a
Set of unique elements in the Bag.
|
static <E> Bag |
unmodifiableBag(Bag
Factory method to create an unmodifiable bag.
|
decorated, equals, getCount, hashCodecontains, containsAll, isEmpty, setCollection, size, toArray, toArray, toStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitcontainsAll, sizecontains, isEmpty, parallelStream, removeIf, spliterator, stream, toArray, toArraypublic static <E> Bag<E> unmodifiableBag(Bag <? extends E> bag)
If the bag passed in is already unmodifiable, it is returned.
E - the type of the elements in the bag
bag - the bag to decorate, must not be null
NullPointerException - if bag is null
public Iterator<E> iterator()
Bag
Iterator over the entire set of members, including copies due to cardinality. This iterator is fail-fast and will not tolerate concurrent modifications.
public boolean add(E object)
Bag
If the object is already in the Bag then increment its count as reported by Bag. Otherwise add it to the Bag and report its count as 1.
Since this method always increases the size of the bag, according to the Collection contract, it should always return true. Since it sometimes returns false, this method violates the contract.
add in interface
Collection<E>
add in interface
Bag<E>
add in class
AbstractCollectionDecorator<E>
object - the object to add
true if the object was not already in the
uniqueSet
public boolean addAll(Collection<? extends E> coll)
public void clear()
public boolean remove(Objectobject)
Bag
This will also remove the object from the Bag.
According to the Collection method, this method should only remove the first occurrence of the given object, not all occurrences.
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)
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 method specifies that cardinality should not be respected; this method should remove all occurrences of every object contained in the given collection.
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)
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 method specifies that cardinality should not be respected; this method should keep all occurrences of every object contained in the given 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)
Bag
nCopies copies of the specified object to the Bag.
If the object is already in the Bag then increment its count as reported by Bag. Otherwise add it to the Bag and report its count as nCopies.
public boolean remove(Objectobject, int count)
Bag
nCopies copies 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.