org.apache.commons.collections
Class AbstractBag

java.lang.Object
  |
  +--org.apache.commons.collections.AbstractBag
All Implemented Interfaces:
Bag, Collection

public abstract class AbstractBag
extends 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.

Author:
Chuck Burdick, Michael A. Smith

Constructor Summary
AbstractBag()
           
 
Method Summary
 boolean add(Object o)
          Add the given object to the bag and keep a count.
 boolean add(Object o, int i)
          Add i copies of the given object to the bag and keep a count.
 boolean addAll(Collection c)
           
protected  int calcTotalSize()
          Actually walks the bag to make sure the count is correct and resets the running total
 void clear()
           
 boolean contains(Object o)
           
 boolean containsAll(Bag other)
          Returns true if the bag contains all elements in the given collection, respecting cardinality.
 boolean containsAll(Collection c)
          Returns true if the bag contains all elements in the given collection, respecting cardinality.
 boolean equals(Object o)
           
 int getCount(Object o)
          Return the number of occurrences (cardinality) of the given object currently in the bag.
protected  Map getMap()
          Utility method for implementations to access the map that backs this bag.
 int hashCode()
           
 boolean isEmpty()
           
 Iterator iterator()
          Returns an Iterator over the entire set of members, including copies due to cardinality.
 boolean remove(Object o)
          Remove all occurrences of the given object from the bag, and do not represent the object in the uniqueSet().
 boolean remove(Object o, int i)
          Remove the given number of occurrences from the bag.
 boolean removeAll(Collection c)
          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(Collection c)
          Remove any members of the bag that are not in the given collection, respecting cardinality.
protected  void setMap(Map m)
          Utility method for implementations to set the map that backs this bag.
 int size()
          Returns the total number of items in the bag across all types.
 Object[] toArray()
           
 Object[] toArray(Object[] a)
           
 Set uniqueSet()
          The Set of unique members that represent all members in the bag.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractBag

public AbstractBag()
Method Detail

add

public boolean add(Object o)
Description copied from interface: Bag
Add the given object to the bag and keep a count. 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.
Specified by:
add in interface Bag
Following copied from interface: org.apache.commons.collections.Bag
Returns:
true if the object was not already in the uniqueSet
See Also:
Bag.getCount(Object)

add

public boolean add(Object o,
                   int i)
Description copied from interface: Bag
Add i copies of the given object to the bag and keep a count.
Specified by:
add in interface Bag
Following copied from interface: org.apache.commons.collections.Bag
Returns:
true if the object was not already in the uniqueSet
See Also:
Bag.add(Object), Bag.getCount(Object)

addAll

public boolean addAll(Collection c)
Specified by:
addAll in interface Collection

calcTotalSize

protected int calcTotalSize()
Actually walks the bag to make sure the count is correct and resets the running total

clear

public void clear()
Specified by:
clear in interface Collection

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection

containsAll

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

containsAll

public boolean containsAll(Collection c)
Description copied from interface: Bag
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.
Specified by:
containsAll in interface Bag

equals

public boolean equals(Object o)
Specified by:
equals in interface Collection
Overrides:
equals in class Object

getCount

public int getCount(Object o)
Description copied from interface: Bag
Return the number of occurrences (cardinality) of the given object currently in the bag. If the object does not exist in the bag, return 0.
Specified by:
getCount in interface Bag

getMap

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

hashCode

public int hashCode()
Specified by:
hashCode in interface Collection
Overrides:
hashCode in class Object

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection

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

remove

public boolean remove(Object o)
Description copied from interface: Bag
Remove all occurrences of the given object from the bag, and do not represent the object in the Bag.uniqueSet().
Specified by:
remove in interface Bag
Following copied from interface: org.apache.commons.collections.Bag
Returns:
true if this call changed the collection
See Also:
Bag.remove(Object, int)

remove

public boolean remove(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
Following copied from interface: org.apache.commons.collections.Bag
Returns:
true if this call changed the collection
See Also:
Bag.getCount(Object), Bag.remove(Object)

removeAll

public boolean removeAll(Collection c)
Description copied from interface: Bag
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.
Specified by:
removeAll in interface Bag
Following copied from interface: org.apache.commons.collections.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)

retainAll

public boolean retainAll(Collection c)
Description copied from interface: Bag
Remove any members of the bag that are not in the given collection, respecting cardinality. That is, if the given collection C 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 !C.contains(e), then remove e and any of its copies.
Specified by:
retainAll in interface Bag
Following copied from interface: org.apache.commons.collections.Bag
Returns:
true if this call changed the collection

setMap

protected void setMap(Map m)
Utility method for implementations to set the map that backs this bag. Not intended for interactive use outside of subclasses.

size

public int size()
Description copied from interface: Bag
Returns the total number of items in the bag across all types.
Specified by:
size in interface Bag

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection

toArray

public Object[] toArray(Object[] a)
Specified by:
toArray in interface Collection

uniqueSet

public Set uniqueSet()
Description copied from interface: Bag
The Set of unique members that represent all members in the bag. Uniqueness constraints are the same as those in Set.
Specified by:
uniqueSet in interface Bag


Copyright © 2001 Apache Software Foundation. Documenation generated April 2 2002.