org.apache.commons.collections
Class DefaultMapBag
java.lang.Object
org.apache.commons.collections.DefaultMapBag
- Bag, Collection
public abstract class DefaultMapBag
extends java.lang.Object
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.
boolean | add(Object o) - Adds a new element to the bag by incrementing its count in the
underlying map.
|
boolean | add(Object o, int i) - Adds a new element to the bag by incrementing its count in the map.
|
boolean | addAll(Collection c) - Invokes
add(Object) for each element in the given collection.
|
protected int | calcTotalSize() - Actually walks the bag to make sure the count is correct and
resets the running total
|
void | clear() - Clears the bag by clearing the underlying map.
|
boolean | contains(Object o) - Determines if the bag contains the given element by checking if the
underlying map contains the element as a key.
|
boolean | containsAll(Collection c)
|
boolean | containsAll(Bag other) - Returns
true if the bag contains all elements in
the given collection, respecting cardinality.
|
boolean | equals(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(Object o) - Returns the number of occurrence of the given element in this bag
by looking up its count in the underlying map.
|
protected Map | getMap() - Utility method for implementations to access the map that backs
this bag.
|
int | hashCode() - Returns the hash code of the underlying map.
|
boolean | isEmpty() - Returns true if the underlying map is empty.
|
Iterator | iterator()
|
boolean | remove(Object o)
|
boolean | remove(Object o, int i)
|
boolean | removeAll(Collection c)
|
boolean | retainAll(Collection c) - 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.
|
protected void | setMap(Map m) - Utility method for implementations to set the map that backs
this bag.
|
int | size() - Returns the number of elements in this bag.
|
Object[] | toArray() - Returns an array of all of this bag's elements.
|
Object[] | toArray(Object[] a) - 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.
|
DefaultMapBag
public DefaultMapBag()
Constructor. Subclasses should invoke
setMap(Map)
in
their constructors.
add
public boolean add(Object o)
Adds a new element to the bag by incrementing its count in the
underlying map.
- add in interface Bag
add
public boolean add(Object o,
int i)
Adds a new element to the bag by incrementing its count in the map.
- add in interface Bag
addAll
public boolean addAll(Collection c)
Invokes
add(Object)
for each element in the given 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()
Clears the bag by clearing the underlying map.
contains
public boolean contains(Object o)
Determines if the bag contains the given element by checking if the
underlying map contains the element as a key.
- true if the bag contains the given element
containsAll
public boolean containsAll(Collection c)
- containsAll in interface Bag
containsAll
public boolean containsAll(Bag other)
Returns true
if the bag contains all elements in
the given collection, respecting cardinality.
equals
public boolean equals(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.
o
- the object to test for equality
- true if that object equals this bag
getCount
public int getCount(Object o)
Returns the number of occurrence of the given element in this bag
by looking up its count in the underlying map.
- 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()
Returns the hash code of the underlying map.
- the hash code of the underlying map
isEmpty
public boolean isEmpty()
Returns true if the underlying map is empty.
- true if there are no elements in this bag
iterator
public Iterator iterator()
- iterator in interface Bag
remove
public boolean remove(Object o)
- remove in interface Bag
remove
public boolean remove(Object o,
int i)
- remove in interface Bag
removeAll
public boolean removeAll(Collection c)
- removeAll in interface Bag
retainAll
public boolean retainAll(Collection c)
Remove any members of the bag that are not in the given
bag, respecting cardinality.
- retainAll in interface Bag
- 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.
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()
Returns the number of elements in this bag.
- size in interface Bag
- the number of elements in this bag
toArray
public Object[] toArray()
Returns an array of all of this bag's elements.
- an array of all of this bag's elements
toArray
public Object[] toArray(Object[] a)
Returns an array of all of this bag's elements.
a
- the array to populate
- an array of all of this bag's elements
toString
public String toString()
Implement a toString() method suitable for debugging
uniqueSet
public Set uniqueSet()
Returns an unmodifiable view of the underlying map's key set.
- uniqueSet in interface Bag
- the set of unique elements in this bag
Copyright © 2001-2004 Apache Software Foundation. Documenation generated ${TODAY}.