## org.apache.commons.collections
Class DefaultMapBag

    java.lang.Object
      org.apache.commons.collections.DefaultMapBag

All Implemented Interfaces:
  java.lang.Iterable, java.util.Collection, Bag

Direct Known Subclasses:
  HashBag, TreeBag

Deprecated. Moved to bag subpackage as AbstractMapBag. Due to be removed in v4.0.

    public abstract class DefaultMapBag

extends java.lang.Object
implements Bag

A skeletal implementation of the `Bag` interface to minimize the effort required for target implementations. Subclasses need only to call `setMap(Map)` in their constructor (or invoke the Map constructor) specifying a map instance that will be used to store the contents of the bag.

The map will be used to map bag elements to a number; the number represents the number of occurrences of that element in the bag.

Since:
  Commons Collections 2.0
Version:
  $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
Author:
  Chuck Burdick, Michael A. Smith, Stephen Colebourne, Janek Bogucki

Constructor Summary
`` `DefaultMapBag()` Deprecated. No-argument constructor.
`protected` `DefaultMapBag(java.util.Map map)` Deprecated. Constructor that assigns the specified Map as the backing store.

Method Summary
`boolean` `add(java.lang.Object object)` Deprecated. Adds a new element to the bag by incrementing its count in the underlying map.
`boolean` `add(java.lang.Object object, int nCopies)` Deprecated. Adds a new element to the bag by incrementing its count in the map.
`boolean` `addAll(java.util.Collection coll)` Deprecated. Invokes `add(Object)` for each element in the given collection.
`protected int` `calcTotalSize()` Deprecated. Actually walks the bag to make sure the count is correct and resets the running total
`void` `clear()` Deprecated. Clears the bag by clearing the underlying map.
`boolean` `contains(java.lang.Object object)` Deprecated. Determines if the bag contains the given element by checking if the underlying map contains the element as a key.
`boolean` `containsAll(Bag other)` Deprecated. Returns `true` if the bag contains all elements in the given collection, respecting cardinality.
`boolean` `containsAll(java.util.Collection coll)` Deprecated. Determines if the bag contains the given elements.
`boolean` `equals(java.lang.Object object)` Deprecated. Returns true if the given object is not null, has the precise type of this bag, and contains the same number of occurrences of all the same elements.
`int` `getCount(java.lang.Object object)` Deprecated. Returns the number of occurrence of the given element in this bag by looking up its count in the underlying map.
`protected java.util.Map` `getMap()` Deprecated. Utility method for implementations to access the map that backs this bag.
`int` `hashCode()` Deprecated. Returns the hash code of the underlying map.
`boolean` `isEmpty()` Deprecated. Returns true if the underlying map is empty.
`java.util.Iterator` `iterator()` Deprecated. Returns an `Iterator` over the entire set of members, including copies due to cardinality.
`boolean` `remove(java.lang.Object object)` Deprecated. (Violation) Removes all occurrences of the given object from the bag.
`boolean` `remove(java.lang.Object object, int nCopies)` Deprecated. Removes `nCopies` copies of the specified object from the Bag.
`boolean` `removeAll(java.util.Collection coll)` Deprecated. (Violation) Remove all elements represented in the given collection, respecting cardinality.
`boolean` `retainAll(Bag other)` Deprecated. Remove any members of the bag that are not in the given bag, respecting cardinality.
`boolean` `retainAll(java.util.Collection coll)` Deprecated. Remove any members of the bag that are not in the given bag, respecting cardinality.
`protected void` `setMap(java.util.Map map)` Deprecated. Utility method for implementations to set the map that backs this bag.
`int` `size()` Deprecated. Returns the number of elements in this bag.
`java.lang.Object[]` `toArray()` Deprecated. Returns an array of all of this bag's elements.
`java.lang.Object[]` `toArray(java.lang.Object[] array)` Deprecated. Returns an array of all of this bag's elements.
`java.lang.String` `toString()` Deprecated. Implement a toString() method suitable for debugging.
`java.util.Set` `uniqueSet()` Deprecated. Returns an unmodifiable view of the underlying map's key set.

Methods inherited from class java.lang.Object
`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

Constructor Detail

### DefaultMapBag

    public DefaultMapBag()

  Deprecated.
  No-argument constructor. Subclasses should invoke `setMap(Map)` in their constructors.

### DefaultMapBag

    protected DefaultMapBag(java.util.Map map)

  Deprecated.
  Constructor that assigns the specified Map as the backing store. The map must be empty.

  Parameters:
    `map` - the map to assign

Method Detail

### add

    public boolean add(java.lang.Object object)

  Deprecated.
  Adds a new element to the bag by incrementing its count in the underlying map.

  Specified by:
    `add` in interface `java.util.Collection`
  Specified by:
    `add` in interface `Bag`
  Parameters:
    `object` - the object to add
  Returns:
    `true` if the object was not already in the `uniqueSet`

### add

    public boolean add(java.lang.Object object,
                       int nCopies)

  Deprecated.
  Adds a new element to the bag by incrementing its count in the map.

  Specified by:
    `add` in interface `Bag`
  Parameters:
    `object` - the object to search for
    `nCopies` - the number of copies to add
  Returns:
    `true` if the object was not already in the `uniqueSet`

### addAll

    public boolean addAll(java.util.Collection coll)

  Deprecated.
  Invokes `add(Object)` for each element in the given collection.

  Specified by:
    `addAll` in interface `java.util.Collection`
  Parameters:
    `coll` - the collection to add
  Returns:
    `true` if this call changed the bag

### clear

    public void clear()

  Deprecated.
  Clears the bag by clearing the underlying map.

  Specified by:
    `clear` in interface `java.util.Collection`

### contains

    public boolean contains(java.lang.Object object)

  Deprecated.
  Determines if the bag contains the given element by checking if the underlying map contains the element as a key.

  Specified by:
    `contains` in interface `java.util.Collection`
  Parameters:
    `object` - the object to search for
  Returns:
    true if the bag contains the given element

### containsAll

    public boolean containsAll(java.util.Collection coll)

  Deprecated.
  Determines if the bag contains the given elements.

  Specified by:
    `containsAll` in interface `java.util.Collection`
  Specified by:
    `containsAll` in interface `Bag`
  Parameters:
    `coll` - the collection to check against
  Returns:
    `true` if the Bag contains all the collection

### containsAll

    public boolean containsAll(Bag other)

  Deprecated.
  Returns `true` if the bag contains all elements in the given collection, respecting cardinality.

  Parameters:
    `other` - the bag to check against
  Returns:
    `true` if the Bag contains all the collection

### equals

    public boolean equals(java.lang.Object object)

  Deprecated.
  Returns true if the given object is not null, has the precise type of this bag, and contains the same number of occurrences of all the same elements.

  Specified by:
    `equals` in interface `java.util.Collection`
  Overrides:
    `equals` in class `java.lang.Object`
  Parameters:
    `object` - the object to test for equality
  Returns:
    true if that object equals this bag

### hashCode

    public int hashCode()

  Deprecated.
  Returns the hash code of the underlying map.

  Specified by:
    `hashCode` in interface `java.util.Collection`
  Overrides:
    `hashCode` in class `java.lang.Object`
  Returns:
    the hash code of the underlying map

### isEmpty

    public boolean isEmpty()

  Deprecated.
  Returns true if the underlying map is empty.

  Specified by:
    `isEmpty` in interface `java.util.Collection`
  Returns:
    true if there are no elements in this bag

### iterator

    public java.util.Iterator iterator()

  Deprecated.
  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 `java.lang.Iterable`
  Specified by:
    `iterator` in interface `java.util.Collection`
  Specified by:
    `iterator` in interface `Bag`
  Returns:
    iterator over all elements in the Bag

### remove

    public boolean remove(java.lang.Object object)

  Deprecated.
  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 `java.util.Collection`
  Specified by:
    `remove` in interface `Bag`
  Returns:
    `true` if this call changed the collection

### remove

    public boolean remove(java.lang.Object object,
                          int nCopies)

  Deprecated.
  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`
  Parameters:
    `object` - the object to remove
    `nCopies` - the number of copies to remove
  Returns:
    `true` if this call changed the collection

### removeAll

    public boolean removeAll(java.util.Collection coll)

  Deprecated.
  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 `java.util.Collection`
  Specified by:
    `removeAll` in interface `Bag`
  Parameters:
    `coll` - the collection to remove
  Returns:
    `true` if this call changed the collection

### retainAll

    public boolean retainAll(java.util.Collection coll)

  Deprecated.
  Remove any members of the bag that are not in the given bag, respecting cardinality.

  Specified by:
    `retainAll` in interface `java.util.Collection`
  Specified by:
    `retainAll` in interface `Bag`
  Parameters:
    `coll` - the collection to retain
  Returns:
    true if this call changed the collection

### retainAll

    public boolean retainAll(Bag other)

  Deprecated.
  Remove any members of the bag that are not in the given bag, respecting cardinality.

  Parameters:
    `other` - the bag to retain
  Returns:
    `true` if this call changed the collection
  See Also:
    `retainAll(Collection)`

### toArray

    public java.lang.Object[] toArray()

  Deprecated.
  Returns an array of all of this bag's elements.

  Specified by:
    `toArray` in interface `java.util.Collection`
  Returns:
    an array of all of this bag's elements

### toArray

    public java.lang.Object[] toArray(java.lang.Object[] array)

  Deprecated.
  Returns an array of all of this bag's elements.

  Specified by:
    `toArray` in interface `java.util.Collection`
  Parameters:
    `array` - the array to populate
  Returns:
    an array of all of this bag's elements

### getCount

    public int getCount(java.lang.Object object)

  Deprecated.
  Returns the number of occurrence of the given element in this bag by looking up its count in the underlying map.

  Specified by:
    `getCount` in interface `Bag`
  Parameters:
    `object` - the object to search for
  Returns:
    the number of occurrences of the object, zero if not found

### uniqueSet

    public java.util.Set uniqueSet()

  Deprecated.
  Returns an unmodifiable view of the underlying map's key set.

  Specified by:
    `uniqueSet` in interface `Bag`
  Returns:
    the set of unique elements in this bag

### size

    public int size()

  Deprecated.
  Returns the number of elements in this bag.

  Specified by:
    `size` in interface `java.util.Collection`
  Specified by:
    `size` in interface `Bag`
  Returns:
    the number of elements in this bag

### calcTotalSize

    protected int calcTotalSize()

  Deprecated.
  Actually walks the bag to make sure the count is correct and resets the running total

  Returns:
    the current total size

### setMap

    protected void setMap(java.util.Map map)

  Deprecated.
  Utility method for implementations to set the map that backs this bag. Not intended for interactive use outside of subclasses.

### getMap

    protected java.util.Map getMap()

  Deprecated.
  Utility method for implementations to access the map that backs this bag. Not intended for interactive use outside of subclasses.

### toString

    public java.lang.String toString()

  Deprecated.
  Implement a toString() method suitable for debugging.

  Overrides:
    `toString` in class `java.lang.Object`
  Returns:
    a debugging toString

Copyright © 2001-2008 The Apache Software Foundation. All Rights Reserved.