public abstract class AbstractMapBag<E> extends Objectimplements 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 |
|---|---|
protected static class |
AbstractMapBag
Mutable integer class for storing the data.
|
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractMapBag()
Constructor needed for subclass serialisation.
|
protected |
AbstractMapBag(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(Collection
Invokes
add(Object) for each element in the given collection.
|
void |
clear()
Clears the bag by clearing the underlying map.
|
boolean |
contains(Object
Determines if the bag contains the given element by checking if the underlying map contains the element as a key.
|
boolean |
containsAll(Collection
Determines if the bag contains the given elements.
|
protected void |
doReadObject(Map
Read the map in using a custom routine.
|
protected void |
doWriteObject(ObjectOutputStream
Write the map out using a custom routine.
|
boolean |
equals(Object
Compares this Bag to another.
|
int |
getCount(Object
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 |
iterator()
Gets an iterator over the bag elements.
|
boolean |
remove(Object
Removes all copies of the specified object from the bag.
|
boolean |
remove(Object
Removes a specified number of copies of an object from the bag.
|
boolean |
removeAll(Collection
Removes objects from the bag according to their count in the specified collection.
|
boolean |
retainAll(Collection
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.
|
<T> T[] |
toArray(T[] array)
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.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, waitparallelStream, removeIf, spliterator, streamprotected AbstractMapBag()
protected AbstractMapBag(Map<E ,AbstractMapBag .MutableInteger > map)
map - the map to assign
protected Map<E ,AbstractMapBag .MutableInteger > getMap()
public int size()
public boolean isEmpty()
isEmpty in interface
Collection<E>
public int getCount(Objectobject)
public boolean contains(Objectobject)
contains in interface
Collection<E>
object - the object to search for
public boolean containsAll(Collection<?> coll)
containsAll in interface
Collection<E>
containsAll in interface
Bag<E>
coll - the collection to check against
true if the Bag contains all the collection
public Iterator<E> iterator()
public boolean add(E object)
public boolean add(E object, int nCopies)
public boolean addAll(Collection<? extends E> coll)
add(Object) for each element in the given collection.
addAll in interface
Collection<E>
coll - the collection to add
true if this call changed the bag
public void clear()
public boolean remove(Objectobject)
public boolean remove(Objectobject, int nCopies)
public boolean removeAll(Collection<?> coll)
public boolean retainAll(Collection<?> coll)
public Object[] toArray()
toArray in interface
Collection<E>
public <T> T[] toArray(T[] array)
toArray in interface
Collection<E>
T - the type of the array elements
array - the array to populate
ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of the elements in this list
NullPointerException - if the specified array is null
protected void doWriteObject(ObjectOutputStreamout) throws IOException
out - the output stream
IOException - any of the usual I/O related exceptions
protected void doReadObject(Map<E ,AbstractMapBag .MutableInteger > map, ObjectInputStream in) throws IOException , ClassNotFoundException
map - the map to use
in - the input stream
IOException - any of the usual I/O related exceptions
ClassNotFoundException - if the stream contains an object which class can not be loaded
ClassCastException - if the stream does not contain the correct objects
public boolean equals(Objectobject)
equals in interface
Collection<E>
equals in class
Object
object - the Bag to compare to
public int hashCode()
(e==null ? 0 : e.hashCode()) ^ noOccurances). This hash code is compatible with the Set interface.
hashCode in interface
Collection<E>
hashCode in class
Object