Class AbstractMapBag
- java.lang.Object
-
- org.apache.commons.collections.bag.AbstractMapBag
-
- All Implemented Interfaces:
java.lang.Iterable
,java.util.Collection
,Bag
public abstract class AbstractMapBag extends java.lang.Object implements Bag
Abstract implementation of theBag
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.
- Since:
- Commons Collections 3.0 (previously DefaultMapBag v2.0)
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
AbstractMapBag.BagIterator
Inner class iterator for the Bag.protected static class
AbstractMapBag.MutableInteger
Mutable integer class for storing the data.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map
map
The map to use to store the dataprivate int
modCount
The modification count for fail fast iteratorsprivate int
size
The current total size of the bagprivate java.util.Set
uniqueSet
The modification count for fail fast iterators
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractMapBag()
Constructor needed for subclass serialisation.protected
AbstractMapBag(java.util.Map map)
Constructor that assigns the specified Map as the backing store.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(java.lang.Object object)
Adds a new element to the bag, incrementing its count in the underlying map.boolean
add(java.lang.Object object, int nCopies)
Adds a new element to the bag, incrementing its count in the map.boolean
addAll(java.util.Collection coll)
Invokesadd(Object)
for each element in the given collection.void
clear()
Clears the bag by clearing the underlying map.boolean
contains(java.lang.Object object)
Determines if the bag contains the given element by checking if the underlying map contains the element as a key.boolean
containsAll(java.util.Collection coll)
Determines if the bag contains the given elements.(package private) boolean
containsAll(Bag other)
Returnstrue
if the bag contains all elements in the given collection, respecting cardinality.protected void
doReadObject(java.util.Map map, java.io.ObjectInputStream in)
Read the map in using a custom routine.protected void
doWriteObject(java.io.ObjectOutputStream out)
Write the map out using a custom routine.boolean
equals(java.lang.Object object)
Compares this Bag to another.int
getCount(java.lang.Object object)
Returns the number of occurrence of the given element in this bag by looking up its count in the underlying map.protected java.util.Map
getMap()
Utility method for implementations to access the map that backs this bag.int
hashCode()
Gets a hash code for the Bag compatible with the definition of equals.boolean
isEmpty()
Returns true if the underlying map is empty.java.util.Iterator
iterator()
Gets an iterator over the bag elements.boolean
remove(java.lang.Object object)
Removes all copies of the specified object from the bag.boolean
remove(java.lang.Object object, int nCopies)
Removes a specified number of copies of an object from the bag.boolean
removeAll(java.util.Collection coll)
Removes objects from the bag according to their count in the specified collection.boolean
retainAll(java.util.Collection coll)
Remove any members of the bag that are not in the given bag, respecting cardinality.(package private) 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.java.lang.Object[]
toArray()
Returns an array of all of this bag's elements.java.lang.Object[]
toArray(java.lang.Object[] array)
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
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
-
-
-
Field Detail
-
map
private transient java.util.Map map
The map to use to store the data
-
size
private int size
The current total size of the bag
-
modCount
private transient int modCount
The modification count for fail fast iterators
-
uniqueSet
private transient java.util.Set uniqueSet
The modification count for fail fast iterators
-
-
Constructor Detail
-
AbstractMapBag
protected AbstractMapBag()
Constructor needed for subclass serialisation.
-
AbstractMapBag
protected AbstractMapBag(java.util.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
-
-
Method Detail
-
getMap
protected java.util.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
-
size
public int size()
Returns the number of elements in this bag.
-
isEmpty
public boolean isEmpty()
Returns true if the underlying map is empty.- Specified by:
isEmpty
in interfacejava.util.Collection
- Returns:
- true if bag is empty
-
getCount
public int getCount(java.lang.Object object)
Returns the number of occurrence of the given element in this bag by looking up its count in the underlying map.
-
contains
public boolean contains(java.lang.Object object)
Determines if the bag contains the given element by checking if the underlying map contains the element as a key.- Specified by:
contains
in interfacejava.util.Collection
- Parameters:
object
- the object to search for- Returns:
- true if the bag contains the given element
-
containsAll
public boolean containsAll(java.util.Collection coll)
Determines if the bag contains the given elements.- Specified by:
containsAll
in interfaceBag
- Specified by:
containsAll
in interfacejava.util.Collection
- Parameters:
coll
- the collection to check against- Returns:
true
if the Bag contains all the collection
-
containsAll
boolean containsAll(Bag other)
Returnstrue
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
-
iterator
public java.util.Iterator iterator()
Gets an iterator over the bag elements. Elements present in the Bag more than once will be returned repeatedly.
-
add
public boolean add(java.lang.Object object)
Adds a new element to the bag, incrementing its count in the underlying map.
-
add
public boolean add(java.lang.Object object, int nCopies)
Adds a new element to the bag, incrementing its count in the map.
-
addAll
public boolean addAll(java.util.Collection coll)
Invokesadd(Object)
for each element in the given collection.- Specified by:
addAll
in interfacejava.util.Collection
- Parameters:
coll
- the collection to add- Returns:
true
if this call changed the bag
-
clear
public void clear()
Clears the bag by clearing the underlying map.- Specified by:
clear
in interfacejava.util.Collection
-
remove
public boolean remove(java.lang.Object object)
Removes all copies of the specified object from the bag.
-
remove
public boolean remove(java.lang.Object object, int nCopies)
Removes a specified number of copies of an object from the bag.
-
removeAll
public boolean removeAll(java.util.Collection coll)
Removes objects from the bag according to their count in the specified collection.
-
retainAll
public boolean retainAll(java.util.Collection coll)
Remove any members of the bag that are not in the given bag, respecting cardinality.
-
retainAll
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- See Also:
retainAll(Collection)
-
toArray
public java.lang.Object[] toArray()
Returns an array of all of this bag's elements.- Specified by:
toArray
in interfacejava.util.Collection
- Returns:
- an array of all of this bag's elements
-
toArray
public java.lang.Object[] toArray(java.lang.Object[] array)
Returns an array of all of this bag's elements.- Specified by:
toArray
in interfacejava.util.Collection
- Parameters:
array
- the array to populate- Returns:
- an array of all of this bag's elements
-
uniqueSet
public java.util.Set uniqueSet()
Returns an unmodifiable view of the underlying map's key set.
-
doWriteObject
protected void doWriteObject(java.io.ObjectOutputStream out) throws java.io.IOException
Write the map out using a custom routine.- Parameters:
out
- the output stream- Throws:
java.io.IOException
-
doReadObject
protected void doReadObject(java.util.Map map, java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
Read the map in using a custom routine.- Parameters:
map
- the map to usein
- the input stream- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
equals
public boolean equals(java.lang.Object object)
Compares this Bag to another. This Bag equals another Bag if it contains the same number of occurrences of the same elements.- Specified by:
equals
in interfacejava.util.Collection
- Overrides:
equals
in classjava.lang.Object
- Parameters:
object
- the Bag to compare to- Returns:
- true if equal
-
hashCode
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.- Specified by:
hashCode
in interfacejava.util.Collection
- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- the hash code of the Bag
-
toString
public java.lang.String toString()
Implement a toString() method suitable for debugging.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a debugging toString
-
-