Package org.apache.commons.collections4.bag

# 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:
  `Serializable`, `Iterable<E>`, `Collection<E>`, `Bag<E>`, `Unmodifiable`

public final class UnmodifiableBag<E> extends AbstractBagDecorator<E> implements Unmodifiable
Decorates another `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.

Since:
  3.0
See Also:
  - Serialized Form

- ## Method Summary

  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)`
  Adds `nCopies` copies of the specified object to the Bag.
  `boolean`
  `addAll(Collection<? extends E> coll)`
  `void`
  `clear()`
  `Iterator<E>`
  `iterator()`
  Returns an `Iterator` over the entire set of members, including copies due to cardinality.
  `boolean`
  `remove(Object object)`
  (Violation) Removes all occurrences of the given object from the bag.
  `boolean`
  `remove(Object object, int count)`
  Removes `nCopies` copies of the specified object from the Bag.
  `boolean`
  `removeAll(Collection<?> coll)`
  (Violation) Remove all elements represented in the given collection, respecting cardinality.
  `boolean`
  `removeIf(Predicate<? super E> filter)`
  `boolean`
  `retainAll(Collection<?> coll)`
  (Violation) Remove any members of the bag that are not in the given collection, respecting cardinality.
  `Set<E>`
  `uniqueSet()`
  Returns a `Set` of 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

  `decorated, equals, getCount, hashCode`

  ### Methods inherited from class org.apache.commons.collections4.collection.AbstractCollectionDecorator

  `contains, containsAll, isEmpty, 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

  `containsAll, size`

  ### Methods inherited from interface java.util.Collection

  `contains, isEmpty, parallelStream, spliterator, stream, toArray, toArray`

  ### Methods inherited from interface java.lang.Iterable

  `forEach`

- ## Method Details

  - ### 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:
      `NullPointerException` - if bag is null
    Since:
      4.0

  - ### 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 by `Bag.getCount(Object)`. Otherwise add it to the `Bag.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 return `true`. Since it sometimes returns `false`, this method violates the contract.

    Specified by:
      `add` in interface `Bag<E>`
    Specified by:
      `add` in interface `Collection<E>`
    Overrides:
      `add` in class `AbstractCollectionDecorator<E>`
    Parameters:
      `object` - the object to add
    Returns:
      `true` if the object was not already in the `uniqueSet`

  - ### add

    public boolean add(E object, int count)
    Description copied from interface: `Bag`
    Adds `nCopies` copies of the specified object to the Bag.

    If the object is already in the `Bag.uniqueSet()` then increment its count as reported by `Bag.getCount(Object)`. Otherwise add it to the `Bag.uniqueSet()` and report its count as `nCopies`.

    Specified by:
      `add` in interface `Bag<E>`
    Overrides:
      `add` in class `AbstractBagDecorator<E>`
    Parameters:
      `object` - the object to add
      `count` - the number of copies to add
    Returns:
      `true` if the object was not already in the `uniqueSet`

  - ### addAll

    public boolean addAll(Collection<? extends E> coll)
    Specified by:
      `addAll` in interface `Collection<E>`
    Overrides:
      `addAll` in class `AbstractCollectionDecorator<E>`

  - ### clear

    public void clear()
    Specified by:
      `clear` in interface `Collection<E>`
    Overrides:
      `clear` in class `AbstractCollectionDecorator<E>`

  - ### iterator

    public Iterator<E> iterator()
    Description copied from interface: `Bag`
    Returns an `Iterator` over the entire set of members, including copies due to cardinality. This iterator is fail-fast and will not tolerate concurrent modifications.
    Specified by:
      `iterator` in interface `Bag<E>`
    Specified by:
      `iterator` in interface `Collection<E>`
    Specified by:
      `iterator` in interface `Iterable<E>`
    Overrides:
      `iterator` in class `AbstractCollectionDecorator<E>`
    Returns:
      iterator over all elements in the Bag

  - ### 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 interface `Bag<E>`
    Specified by:
      `remove` in interface `Collection<E>`
    Overrides:
      `remove` in class `AbstractCollectionDecorator<E>`
    Parameters:
      `object` - the object to remove
    Returns:
      `true` if this call changed the collection

  - ### remove

    public boolean remove(Object object, int count)
    Description copied from interface: `Bag`
    Removes `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.

    Specified by:
      `remove` in interface `Bag<E>`
    Overrides:
      `remove` in class `AbstractBagDecorator<E>`
    Parameters:
      `object` - the object to remove
      `count` - the number of copies 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 collection `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.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 interface `Bag<E>`
    Specified by:
      `removeAll` in interface `Collection<E>`
    Overrides:
      `removeAll` in class `AbstractCollectionDecorator<E>`
    Parameters:
      `coll` - the collection to remove
    Returns:
      `true` if this call changed the collection

  - ### removeIf

    public boolean removeIf(Predicate<? super E> filter)
    Specified by:
      `removeIf` in interface `Collection<E>`
    Overrides:
      `removeIf` in class `AbstractCollectionDecorator<E>`
    Since:
      4.4

  - ### 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 collection `coll` contains `n` copies of a given object and the bag has `m &gt; 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.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 interface `Bag<E>`
    Specified by:
      `retainAll` in interface `Collection<E>`
    Overrides:
      `retainAll` in class `AbstractCollectionDecorator<E>`
    Parameters:
      `coll` - the collection to retain
    Returns:
      `true` if this call changed the collection

  - ### uniqueSet

    public Set<E> uniqueSet()
    Description copied from interface: `Bag`
    Returns a `Set` of unique elements in the Bag.

    Uniqueness constraints are the same as those in `Set`.

    Specified by:
      `uniqueSet` in interface `Bag<E>`
    Overrides:
      `uniqueSet` in class `AbstractBagDecorator<E>`
    Returns:
      the Set of unique Bag elements