public abstract class AbstractMapBag<E> extends java.lang.Object implements Bag<E>
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.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
AbstractMapBag.BagIterator<E>
Inner class iterator for the Bag.
|
protected static class |
AbstractMapBag.MutableInteger
Mutable integer class for storing the data.
|
Modifier and Type | Field and Description |
---|---|
private java.util.Map<E,AbstractMapBag.MutableInteger> |
map
The map to use to store the data
|
private int |
modCount
The modification count for fail fast iterators
|
private int |
size
The current total size of the bag
|
private java.util.Set<E> |
uniqueSet
Unique view of the elements
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractMapBag()
Constructor needed for subclass serialisation.
|
protected |
AbstractMapBag(java.util.Map<E,AbstractMapBag.MutableInteger> map)
Constructor that assigns the specified Map as the backing store.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object)
Adds a new element to the bag, incrementing its count in the underlying map.
|
boolean |
add(E object,
int nCopies)
Adds a new element to the bag, incrementing its count in the map.
|
boolean |
addAll(java.util.Collection<? extends E> coll)
Invokes
add(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.
|
(package private) boolean |
containsAll(Bag<?> other)
Returns
true if the bag contains all elements in the given
collection, respecting cardinality. |
boolean |
containsAll(java.util.Collection<?> coll)
Determines if the bag contains the given elements.
|
protected void |
doReadObject(java.util.Map<E,AbstractMapBag.MutableInteger> 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<E,AbstractMapBag.MutableInteger> |
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<E> |
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.
|
(package private) boolean |
retainAll(Bag<?> other)
Remove any members of the bag that are not in the given bag, respecting
cardinality.
|
boolean |
retainAll(java.util.Collection<?> coll)
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.
|
<T> T[] |
toArray(T[] 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<E> |
uniqueSet()
Returns an unmodifiable view of the underlying map's key set.
|
private transient java.util.Map<E,AbstractMapBag.MutableInteger> map
private int size
private transient int modCount
private transient java.util.Set<E> uniqueSet
protected AbstractMapBag()
protected AbstractMapBag(java.util.Map<E,AbstractMapBag.MutableInteger> map)
map
- the map to assignprotected java.util.Map<E,AbstractMapBag.MutableInteger> getMap()
public int size()
public boolean isEmpty()
isEmpty
in interface java.util.Collection<E>
public int getCount(java.lang.Object object)
public boolean contains(java.lang.Object object)
contains
in interface java.util.Collection<E>
object
- the object to search forpublic boolean containsAll(java.util.Collection<?> coll)
containsAll
in interface java.util.Collection<E>
containsAll
in interface Bag<E>
coll
- the collection to check againsttrue
if the Bag contains all the collectionboolean containsAll(Bag<?> other)
true
if the bag contains all elements in the given
collection, respecting cardinality.other
- the bag to check againsttrue
if the Bag contains all the collectionpublic java.util.Iterator<E> iterator()
public boolean add(E object)
public boolean add(E object, int nCopies)
public boolean addAll(java.util.Collection<? extends E> coll)
add(Object)
for each element in the given collection.addAll
in interface java.util.Collection<E>
coll
- the collection to addtrue
if this call changed the bagpublic void clear()
clear
in interface java.util.Collection<E>
public boolean remove(java.lang.Object object)
public boolean remove(java.lang.Object object, int nCopies)
public boolean removeAll(java.util.Collection<?> coll)
public boolean retainAll(java.util.Collection<?> coll)
boolean retainAll(Bag<?> other)
other
- the bag to retaintrue
if this call changed the collectionretainAll(Collection)
public java.lang.Object[] toArray()
toArray
in interface java.util.Collection<E>
public <T> T[] toArray(T[] array)
toArray
in interface java.util.Collection<E>
T
- the type of the array elementsarray
- the array to populatejava.lang.ArrayStoreException
- if the runtime type of the specified array is not
a supertype of the runtime type of the elements in this listjava.lang.NullPointerException
- if the specified array is nullpublic java.util.Set<E> uniqueSet()
protected void doWriteObject(java.io.ObjectOutputStream out) throws java.io.IOException
out
- the output streamjava.io.IOException
- any of the usual I/O related exceptionsprotected void doReadObject(java.util.Map<E,AbstractMapBag.MutableInteger> map, java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
map
- the map to usein
- the input streamjava.io.IOException
- any of the usual I/O related exceptionsjava.lang.ClassNotFoundException
- if the stream contains an object which class can not be loadedjava.lang.ClassCastException
- if the stream does not contain the correct objectspublic boolean equals(java.lang.Object object)
equals
in interface java.util.Collection<E>
equals
in class java.lang.Object
object
- the Bag to compare topublic int hashCode()
(e==null ? 0 : e.hashCode()) ^ noOccurances)
. This hash code
is compatible with the Set interface.hashCode
in interface java.util.Collection<E>
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object