Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
org.apache.commons.collections.bag.AbstractMapBag
Bag
interface to simplify the creation
of subclass implementations.
Subclasses specify a Map implementation to use as the internal storage.
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.
Nested Class Summary | |
protected static class |
|
Constructor Summary | |
| |
|
Method Summary | |
boolean |
|
boolean |
|
boolean |
|
void |
|
boolean |
|
boolean |
|
protected void |
|
protected void |
|
boolean |
|
int |
|
protected Map |
|
int |
|
boolean |
|
Iterator |
|
boolean |
|
boolean |
|
boolean |
|
boolean |
|
int |
|
Object[] |
|
Object[] |
|
String |
|
Set |
|
protected AbstractMapBag()
Constructor needed for subclass serialisation.
protected AbstractMapBag(Map map)
Constructor that assigns the specified Map as the backing store. The map must be empty and non-null.
- Parameters:
map
- the map to assign
public boolean add(Object object)
Adds a new element to the bag, incrementing its count in the underlying map.
- Parameters:
object
- the object to add
- Returns:
true
if the object was not already in theuniqueSet
public boolean add(Object object, int nCopies)
Adds a new element to the bag, incrementing its count in the map.
- Parameters:
object
- the object to search fornCopies
- the number of copies to add
- Returns:
true
if the object was not already in theuniqueSet
public boolean addAll(Collection coll)
Invokesadd(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.
- Specified by:
- containsAll in interface Bag
- Parameters:
coll
- the collection to check against
- Returns:
true
if the Bag contains all the collection
protected void doReadObject(Map map, ObjectInputStream in) throws IOException, ClassNotFoundException
Read the map in using a custom routine.
- Parameters:
map
- the map to usein
- the input stream
protected void doWriteObject(ObjectOutputStream out) throws IOException
Write the map out using a custom routine.
- Parameters:
out
- the output stream
public boolean equals(Object object)
Compares this Bag to another. This Bag equals another Bag if it contains the same number of occurrences of the same elements.
- Parameters:
object
- the Bag to compare to
- Returns:
- true if equal
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
protected Map getMap()
Utility method for implementations to access the map that backs this bag. Not intended for interactive use outside of subclasses.
- Returns:
- the map being used by the Bag
public int hashCode()
Gets a hash code for the Bag compatible with the definition of equals. The hash code is defined as the sum total of a hash code for each element. The per element hash code is defined as(e==null ? 0 : e.hashCode()) ^ noOccurances)
. This hash code is compatible with the Set interface.
- Returns:
- the hash code of the Bag
public boolean isEmpty()
Returns true if the underlying map is empty.
- Returns:
- true if bag is empty
public Iterator iterator()
Gets an iterator over the bag elements. Elements present in the Bag more than once will be returned repeatedly.
- Returns:
- the iterator
public boolean remove(Object object)
Removes all copies of the specified object from the bag.
- Parameters:
object
- the object to remove
- Returns:
- true if the bag changed
public boolean remove(Object object, int nCopies)
Removes a specified number of copies of an object from the bag.
- Parameters:
object
- the object to removenCopies
- the number of copies to remove
- Returns:
- true if the bag changed
public boolean removeAll(Collection coll)
Removes objects from the bag according to their count in the specified collection.
- Parameters:
coll
- the collection to use
- Returns:
- true if the bag changed
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 int size()
Returns the number of elements in this bag.
- Returns:
- current size of the bag
public Object[] toArray()
Returns an array of all of this bag's elements.
- Returns:
- an array of all of this bag's elements
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 String toString()
Implement a toString() method suitable for debugging.
- Returns:
- a debugging toString