public abstract class

DefaultMapBag

extends Object
implements Bag
java.lang.Object
   ↳ org.apache.commons.collections.DefaultMapBag
Known Direct Subclasses

This class is deprecated.
Moved to bag subpackage as AbstractMapBag. Due to be removed in v4.0.

Class Overview

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.

Summary

Public Constructors
DefaultMapBag()
No-argument constructor.
Protected Constructors
DefaultMapBag(Map map)
Constructor that assigns the specified Map as the backing store.
Public Methods
boolean add(Object object, int nCopies)
Adds a new element to the bag by incrementing its count in the map.
boolean add(Object object)
Adds a new element to the bag by incrementing its count in the underlying map.
boolean addAll(Collection coll)
Invokes add(Object) for each element in the given collection.
void clear()
Clears the bag by clearing the underlying map.
boolean contains(Object object)
Determines if the bag contains the given element by checking if the underlying map contains the element as a key.
boolean containsAll(Collection coll)
Determines if the bag contains the given elements.
boolean containsAll(Bag other)
Returns true if the bag contains all elements in the given collection, respecting cardinality.
boolean equals(Object object)
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(Object object)
Returns the number of occurrence of the given element in this bag by looking up its count in the underlying map.
int hashCode()
Returns the hash code of the underlying map.
boolean isEmpty()
Returns true if the underlying map is empty.
Iterator iterator()
Returns an Iterator over the entire set of members, including copies due to cardinality.
boolean remove(Object object, int nCopies)
Removes nCopies copies of the specified object from the Bag.
boolean remove(Object object)
(Violation) Removes all occurrences of the given object from the bag.
boolean removeAll(Collection coll)
(Violation) Remove all elements represented in the given collection, respecting cardinality.
boolean retainAll(Collection coll)
Remove any members of the bag that are not in the given bag, respecting cardinality.
boolean retainAll(Bag other)
Remove any members of the bag that are not in the given bag, respecting cardinality.
int size()
Returns the number of elements in this bag.
Object[] toArray(Object[] array)
Returns an array of all of this bag's elements.
Object[] toArray()
Returns an array of all of this bag's elements.
String toString()
Implement a toString() method suitable for debugging.
Set uniqueSet()
Returns an unmodifiable view of the underlying map's key set.
Protected Methods
int calcTotalSize()
Actually walks the bag to make sure the count is correct and resets the running total
Map getMap()
Utility method for implementations to access the map that backs this bag.
void setMap(Map map)
Utility method for implementations to set the map that backs this bag.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.lang.Iterable
From interface java.util.Collection
From interface org.apache.commons.collections.Bag

Public Constructors

public DefaultMapBag ()

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

Protected Constructors

protected DefaultMapBag (Map map)

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

Parameters
map the map to assign

Public Methods

public boolean add (Object object, int nCopies)

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

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

public boolean add (Object object)

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

Parameters
object the object to add
Returns
  • true if the object was not already in the uniqueSet

public boolean addAll (Collection coll)

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

Parameters
coll the collection to add
Returns
  • true if this call changed the bag

public void clear ()

Clears the bag by clearing the underlying map.

public boolean contains (Object object)

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

Parameters
object the object to search for
Returns
  • true if the bag contains the given element

public boolean containsAll (Collection coll)

Determines if the bag contains the given elements.

Parameters
coll the collection to check against
Returns
  • true if the Bag contains all the collection

public boolean containsAll (Bag other)

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

public boolean equals (Object object)

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.

Parameters
object the object to test for equality
Returns
  • true if that object equals this bag

public int getCount (Object object)

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

Parameters
object the object to search for
Returns
  • the number of occurrences of the object, zero if not found

public int hashCode ()

Returns the hash code of the underlying map.

Returns
  • the hash code of the underlying map

public boolean isEmpty ()

Returns true if the underlying map is empty.

Returns
  • true if there are no elements in this bag

public Iterator iterator ()

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.

Returns
  • iterator over all elements in the Bag

public boolean remove (Object object, int nCopies)

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.

Parameters
object the object to remove
nCopies the number of copies to remove
Returns
  • true if this call changed the collection

public boolean remove (Object object)

(Violation) Removes all occurrences of the given object from the bag.

This will also remove the object from the uniqueSet().

According to the remove(Object) method, this method should only remove the first occurrence of the given object, not all occurrences.

Returns
  • true if this call changed the collection

public boolean removeAll (Collection coll)

(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 removeAll(Collection) method specifies that cardinality should not be respected; this method should remove all occurrences of every object contained in the given collection.

Parameters
coll the collection to remove
Returns
  • true if this call changed the collection

public boolean retainAll (Collection coll)

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

Parameters
coll the collection to retain
Returns
  • true if this call changed the collection

public boolean retainAll (Bag other)

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

public int size ()

Returns the number of elements in this bag.

Returns
  • the number of elements in this bag

public Object[] toArray (Object[] array)

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

Parameters
array the array to populate
Returns
  • an array of all of this bag's elements

public Object[] toArray ()

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

Returns
  • an array of all of this bag's elements

public String toString ()

Implement a toString() method suitable for debugging.

Returns
  • a debugging toString

public Set uniqueSet ()

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

Returns
  • the set of unique elements in this bag

Protected Methods

protected int calcTotalSize ()

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

Returns
  • the current total size

protected Map getMap ()

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

protected void setMap (Map map)

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