## org.apache.commons.collections.bag
Class UnmodifiableSortedBag

    java.lang.Object
      org.apache.commons.collections.collection.AbstractCollectionDecorator
          org.apache.commons.collections.bag.AbstractBagDecorator
              org.apache.commons.collections.bag.AbstractSortedBagDecorator
                  org.apache.commons.collections.bag.UnmodifiableSortedBag

All Implemented Interfaces:
  Bag, Collection, Serializable, SortedBag, Unmodifiable

public final class UnmodifiableSortedBag
extends AbstractSortedBagDecorator
implements Unmodifiable, Serializable

Decorates another `SortedBag` to ensure it can't be altered.

This class is Serializable from Commons Collections 3.1.

Since:
  Commons Collections 3.0
Version:
  $Revision: 1.9 $ $Date: 2004/06/02 21:56:19 $
Author:
  Stephen Colebourne
See Also:
  Serialized Form

Field Summary

Fields inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
`collection`

Method Summary
`boolean` `add(Object object)` (Violation) Adds one copy the specified object to the Bag.
`boolean` `add(Object object, int count)` Adds `nCopies` copies of the specified object to the Bag.
`boolean` `addAll(Collection coll)`
`void` `clear()`
`static SortedBag` `decorate(SortedBag bag)` Factory method to create an unmodifiable bag.
`Iterator` `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` `retainAll(Collection coll)` (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.

Methods inherited from class org.apache.commons.collections.bag.AbstractSortedBagDecorator
`comparator, first, getSortedBag, last`

Methods inherited from class org.apache.commons.collections.bag.AbstractBagDecorator
`getBag, getCount`

Methods inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
`contains, containsAll, equals, getCollection, hashCode, isEmpty, 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.collections.Bag
`containsAll, getCount, size`

Methods inherited from interface java.util.Collection
`contains, equals, hashCode, isEmpty, toArray, toArray`

Method Detail

### decorate

    public static SortedBag decorate(SortedBag bag)

  Factory method to create an unmodifiable bag.

  If the bag passed in is already unmodifiable, it is returned.

  Parameters:
    `bag` - the bag to decorate, must not be null
  Returns:
    an unmodifiable SortedBag
  Throws:
    `IllegalArgumentException` - if bag is null

### iterator

    public Iterator 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`
  Overrides:
    `iterator` in class `AbstractCollectionDecorator`

### add

    public boolean add(Object object)

  Description copied from interface: `Bag`
  (Violation) Adds one copy 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`
  Overrides:
    `add` in class `AbstractCollectionDecorator`

### addAll

    public boolean addAll(Collection coll)

  Specified by:
    `addAll` in interface `Collection`
  Overrides:
    `addAll` in class `AbstractCollectionDecorator`

### clear

    public void clear()

  Specified by:
    `clear` in interface `Collection`
  Overrides:
    `clear` in class `AbstractCollectionDecorator`

### 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`
  Overrides:
    `remove` in class `AbstractCollectionDecorator`

### 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`
  Overrides:
    `removeAll` in class `AbstractCollectionDecorator`

### 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 > 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`
  Overrides:
    `retainAll` in class `AbstractCollectionDecorator`

### add

    public boolean add(Object 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`
  Overrides:
    `add` in class `AbstractBagDecorator`

### 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`
  Overrides:
    `remove` in class `AbstractBagDecorator`

### uniqueSet

    public Set 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`
  Overrides:
    `uniqueSet` in class `AbstractBagDecorator`

Copyright © 2001-2005 Apache Software Foundation. All Rights Reserved.