org.apache.commons.collections
Class DefaultMapBag

java.lang.Object
  |
  +--org.apache.commons.collections.DefaultMapBag
All Implemented Interfaces:
Bag, java.util.Collection
Direct Known Subclasses:
HashBag, TreeBag

public abstract class DefaultMapBag
extends java.lang.Object
implements Bag

This class provides 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 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:
2.0
Author:
Chuck Burdick, Michael A. Smith

Constructor Summary
DefaultMapBag()
          Constructor.
 
Method Summary
 boolean add(java.lang.Object o)
          Adds a new element to the bag by incrementing its count in the underlying map.
 boolean add(java.lang.Object o, int i)
          Adds a new element to the bag by incrementing its count in the map.
 boolean addAll(java.util.Collection c)
          Invokes add(Object) for each element in the given collection.
 void clear()
          Clears the bag by clearing the underlying map.
 boolean contains(java.lang.Object o)
          Determines if the bag contains the given element by checking if the underlying map contains the element as a key.
 boolean containsAll(Bag other)
          Returns true if the bag contains all elements in the given collection, respecting cardinality.
 boolean containsAll(java.util.Collection c)
          (Violation) Returns true if the bag contains all elements in the given collection, respecting cardinality.
 boolean equals(java.lang.Object o)
          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 o)
          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.
 java.util.Iterator iterator()
          Returns an Iterator over the entire set of members, including copies due to cardinality.
 boolean remove(java.lang.Object o)
          (Violation) Remove all occurrences of the given object from the bag, and do not represent the object in the uniqueSet().
 boolean remove(java.lang.Object o, int i)
          Remove the given number of occurrences from the bag.
 boolean removeAll(java.util.Collection c)
          (Violation) Remove all elements represented in the given collection, respecting cardinality.
 boolean retainAll(Bag other)
          Remove any members of the bag that are not in the given bag, respecting cardinality.
 boolean retainAll(java.util.Collection c)
          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.
 java.lang.Object[] toArray()
          Returns an array of all of this bag's elements.
 java.lang.Object[] toArray(java.lang.Object[] a)
          Returns an array of all of this bag's elements.
 java.lang.String toString()
          Implement a toString() method suitable for debugging
 java.util.Set uniqueSet()
          Returns an unmodifiable view of the underlying map's key set.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DefaultMapBag

public DefaultMapBag()
Constructor. Subclasses should invoke #setMap(Map) in their constructors.

Method Detail

add

public boolean add(java.lang.Object o)
Adds a new element to the bag by incrementing its count in the underlying map.

Specified by:
add in interface Bag
Returns:
true if the object was not already in the uniqueSet
See Also:
Bag.add(Object)

add

public boolean add(java.lang.Object o,
                   int i)
Adds a new element to the bag by incrementing its count in the map.

Specified by:
add in interface Bag
Returns:
true if the object was not already in the uniqueSet
See Also:
Bag.add(Object, int)

addAll

public boolean addAll(java.util.Collection c)
Invokes add(Object) for each element in the given collection.

Specified by:
addAll in interface java.util.Collection
See Also:
Collection.addAll(Collection)

clear

public void clear()
Clears the bag by clearing the underlying map.

Specified by:
clear in interface java.util.Collection

contains

public boolean contains(java.lang.Object o)
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
Returns:
true if the bag contains the given element

containsAll

public boolean containsAll(java.util.Collection c)
Description copied from interface: Bag
(Violation) Returns true if the bag contains all elements in the given collection, respecting cardinality. That is, if the given collection C contains n copies of a given object, calling Bag.getCount(Object) on that object must be >= n for all n in C.

The Collection.containsAll(Collection) method specifies that cardinality should not be respected; this method should return true if the bag contains at least one of every object contained in the given collection. A future version of this method will comply with that contract.

Specified by:
containsAll in interface Bag

containsAll

public boolean containsAll(Bag other)
Returns true if the bag contains all elements in the given collection, respecting cardinality.

See Also:
containsAll(Collection)

equals

public boolean equals(java.lang.Object o)
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:
o - the object to test for equality
Returns:
true if that object equals this bag

hashCode

public int hashCode()
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()
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()
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

remove

public boolean remove(java.lang.Object o)
Description copied from interface: Bag
(Violation) Remove all occurrences of the given object from the bag, and do not represent the object in 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. A future version of this method will comply with the contract by only removing one occurrence of the given object.

Specified by:
remove in interface Bag
Returns:
true if this call changed the collection
See Also:
Bag.remove(Object, int)

remove

public boolean remove(java.lang.Object o,
                      int i)
Description copied from interface: Bag
Remove the given number of occurrences from the bag. If the bag contains i occurrences or less, the item will be removed from the Bag.uniqueSet().

Specified by:
remove in interface Bag
Returns:
true if this call changed the collection
See Also:
Bag.getCount(Object), Bag.remove(Object)

removeAll

public boolean removeAll(java.util.Collection c)
Description copied from interface: Bag
(Violation) Remove all elements represented in the given collection, respecting cardinality. That is, if the given collection C 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. A future version of this method will comply with that contract.

Specified by:
removeAll in interface Bag
Returns:
true if this call changed the collection

retainAll

public boolean retainAll(java.util.Collection c)
Remove any members of the bag that are not in the given bag, respecting cardinality.

Specified by:
retainAll in interface Bag
Returns:
true if this call changed the collection

retainAll

public boolean retainAll(Bag other)
Remove any members of the bag that are not in the given bag, respecting cardinality.

Returns:
true if this call changed the collection
See Also:
retainAll(Collection)

toArray

public java.lang.Object[] toArray()
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[] a)
Returns an array of all of this bag's elements.

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

getCount

public int getCount(java.lang.Object o)
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
See Also:
Bag.getCount(Object)

uniqueSet

public java.util.Set uniqueSet()
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()
Returns the number of elements in this bag.

Specified by:
size in interface Bag
Returns:
the number of elements in this bag

toString

public java.lang.String toString()
Implement a toString() method suitable for debugging

Overrides:
toString in class java.lang.Object


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