Class UnmodifiableBag<E>
- java.lang.Object
-
- org.apache.commons.collections4.collection.AbstractCollectionDecorator<E>
-
- org.apache.commons.collections4.bag.AbstractBagDecorator<E>
-
- org.apache.commons.collections4.bag.UnmodifiableBag<E>
-
- Type Parameters:
E- the type of elements in this bag
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<E>,java.util.Collection<E>,Bag<E>,Unmodifiable
public final class UnmodifiableBag<E> extends AbstractBagDecorator<E> implements Unmodifiable
Decorates anotherBagto 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:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(E object)(Violation) Adds one copy of the specified object to the Bag.booleanadd(E object, int count)AddsnCopiescopies of the specified object to the Bag.booleanaddAll(java.util.Collection<? extends E> coll)voidclear()java.util.Iterator<E>iterator()Returns anIteratorover the entire set of members, including copies due to cardinality.booleanremove(java.lang.Object object)(Violation) Removes all occurrences of the given object from the bag.booleanremove(java.lang.Object object, int count)RemovesnCopiescopies of the specified object from the Bag.booleanremoveAll(java.util.Collection<?> coll)(Violation) Remove all elements represented in the given collection, respecting cardinality.booleanremoveIf(java.util.function.Predicate<? super E> filter)booleanretainAll(java.util.Collection<?> coll)(Violation) Remove any members of the bag that are not in the given collection, respecting cardinality.java.util.Set<E>uniqueSet()Returns aSetof unique elements in the Bag.static <E> Bag<E>unmodifiableBag(Bag<? extends E> bag)Factory method to create an unmodifiable bag.-
Methods inherited from class org.apache.commons.collections4.bag.AbstractBagDecorator
equals, getCount, hashCode
-
Methods inherited from class org.apache.commons.collections4.collection.AbstractCollectionDecorator
contains, containsAll, isEmpty, size, toArray, toArray, toString
-
Methods inherited from interface org.apache.commons.collections4.Bag
containsAll, size
-
-
-
-
Method Detail
-
unmodifiableBag
public static <E> Bag<E> unmodifiableBag(Bag<? extends E> bag)
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 Bag
- Throws:
java.lang.NullPointerException- if bag is null- Since:
- 4.0
-
iterator
public java.util.Iterator<E> iterator()
Description copied from interface:BagReturns anIteratorover the entire set of members, including copies due to cardinality. This iterator is fail-fast and will not tolerate concurrent modifications.
-
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.
-
addAll
public boolean addAll(java.util.Collection<? extends E> coll)
- Specified by:
addAllin interfacejava.util.Collection<E>- Overrides:
addAllin classAbstractCollectionDecorator<E>
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Collection<E>- Overrides:
clearin classAbstractCollectionDecorator<E>
-
remove
public boolean remove(java.lang.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.
-
removeIf
public boolean removeIf(java.util.function.Predicate<? super E> filter)
- Specified by:
removeIfin interfacejava.util.Collection<E>- Overrides:
removeIfin classAbstractCollectionDecorator<E>- Since:
- 4.4
-
removeAll
public boolean removeAll(java.util.Collection<?> coll)
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.
-
retainAll
public boolean retainAll(java.util.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 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.
-
add
public boolean add(E object, int count)
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
public boolean remove(java.lang.Object object, int count)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.
-
-