Class AbstractMapBag
java.lang.Object
org.apache.commons.collections.bag.AbstractMapBag
- All Implemented Interfaces:
Iterable
,Collection
,Bag
Abstract implementation of the
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.
- 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 ClassesModifier and TypeClassDescription(package private) static class
Inner class iterator for the Bag.protected static class
Mutable integer class for storing the data. -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Constructor needed for subclass serialisation.protected
AbstractMapBag
(Map map) Constructor that assigns the specified Map as the backing store. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds a new element to the bag, incrementing its count in the underlying map.boolean
Adds a new element to the bag, incrementing its count in the map.boolean
addAll
(Collection coll) Invokesadd(Object)
for each element in the given collection.void
clear()
Clears the bag by clearing the underlying map.boolean
Determines if the bag contains the given element by checking if the underlying map contains the element as a key.boolean
containsAll
(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
(Map map, ObjectInputStream in) Read the map in using a custom routine.protected void
Write the map out using a custom routine.boolean
Compares this Bag to another.int
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()
Gets a hash code for the Bag compatible with the definition of equals.boolean
isEmpty()
Returns true if the underlying map is empty.iterator()
Gets an iterator over the bag elements.boolean
Removes all copies of the specified object from the bag.boolean
Removes a specified number of copies of an object from the bag.boolean
removeAll
(Collection coll) Removes objects from the bag according to their count in the specified collection.boolean
retainAll
(Collection coll) Remove any members of the bag that are not in the given bag, respecting cardinality.(package private) boolean
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.Object[]
toArray()
Returns an array of all of this bag's elements.Object[]
Returns an array of all of this bag's elements.toString()
Implement a toString() method suitable for debugging.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
Methods inherited from interface java.util.Collection
parallelStream, removeIf, spliterator, stream
-
Field Details
-
map
The map to use to store the data -
size
private int sizeThe current total size of the bag -
modCount
private transient int modCountThe modification count for fail fast iterators -
uniqueSet
The modification count for fail fast iterators
-
-
Constructor Details
-
AbstractMapBag
protected AbstractMapBag()Constructor needed for subclass serialisation. -
AbstractMapBag
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 Details
-
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.- Specified by:
size
in interfaceBag
- Specified by:
size
in interfaceCollection
- Returns:
- current size of the bag
-
isEmpty
public boolean isEmpty()Returns true if the underlying map is empty.- Specified by:
isEmpty
in interfaceCollection
- Returns:
- true if bag is empty
-
getCount
Returns the number of occurrence of the given element in this bag by looking up its count in the underlying map. -
contains
Determines if the bag contains the given element by checking if the underlying map contains the element as a key.- Specified by:
contains
in interfaceCollection
- Parameters:
object
- the object to search for- Returns:
- true if the bag contains the given element
-
containsAll
Determines if the bag contains the given elements.- Specified by:
containsAll
in interfaceBag
- Specified by:
containsAll
in interfaceCollection
- Parameters:
coll
- the collection to check against- Returns:
true
if the Bag contains all the collection
-
containsAll
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
Gets an iterator over the bag elements. Elements present in the Bag more than once will be returned repeatedly. -
add
Adds a new element to the bag, incrementing its count in the underlying map.- Specified by:
add
in interfaceBag
- Specified by:
add
in interfaceCollection
- Parameters:
object
- the object to add- Returns:
true
if the object was not already in theuniqueSet
-
add
Adds a new element to the bag, incrementing its count in the map. -
addAll
Invokesadd(Object)
for each element in the given collection.- Specified by:
addAll
in interfaceCollection
- 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 interfaceCollection
-
remove
Removes all copies of the specified object from the bag.- Specified by:
remove
in interfaceBag
- Specified by:
remove
in interfaceCollection
- Parameters:
object
- the object to remove- Returns:
- true if the bag changed
-
remove
Removes a specified number of copies of an object from the bag. -
removeAll
Removes objects from the bag according to their count in the specified collection.- Specified by:
removeAll
in interfaceBag
- Specified by:
removeAll
in interfaceCollection
- Parameters:
coll
- the collection to use- Returns:
- true if the bag changed
-
retainAll
Remove any members of the bag that are not in the given bag, respecting cardinality.- Specified by:
retainAll
in interfaceBag
- Specified by:
retainAll
in interfaceCollection
- Parameters:
coll
- the collection to retain- Returns:
- true if this call changed the collection
-
retainAll
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:
-
toArray
Returns an array of all of this bag's elements.- Specified by:
toArray
in interfaceCollection
- Returns:
- an array of all of this bag's elements
-
toArray
Returns an array of all of this bag's elements.- Specified by:
toArray
in interfaceCollection
- Parameters:
array
- the array to populate- Returns:
- an array of all of this bag's elements
-
uniqueSet
Returns an unmodifiable view of the underlying map's key set. -
doWriteObject
Write the map out using a custom routine.- Parameters:
out
- the output stream- Throws:
IOException
-
doReadObject
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- Throws:
IOException
ClassNotFoundException
-
equals
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 interfaceCollection
- Overrides:
equals
in classObject
- 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 interfaceCollection
- Overrides:
hashCode
in classObject
- Returns:
- the hash code of the Bag
-
toString
Implement a toString() method suitable for debugging.
-