E
- the type held in the multisetpublic abstract class AbstractMapMultiSet<E> extends AbstractMultiSet<E>
MultiSet
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 multiset elements to a number; the number represents the number of occurrences of that element in the multiset.
Modifier and Type | Class and Description |
---|---|
protected static class |
AbstractMapMultiSet.EntrySetIterator<E>
Inner class EntrySetIterator.
|
private static class |
AbstractMapMultiSet.MapBasedMultiSetIterator<E>
Inner class iterator for the MultiSet.
|
protected static class |
AbstractMapMultiSet.MultiSetEntry<E>
Inner class MultiSetEntry.
|
protected static class |
AbstractMapMultiSet.MutableInteger
Mutable integer class for storing the data.
|
protected static class |
AbstractMapMultiSet.UniqueSetIterator<E>
Inner class UniqueSetIterator.
|
AbstractMultiSet.AbstractEntry<E>, AbstractMultiSet.EntrySet<E>, AbstractMultiSet.UniqueSet<E>
MultiSet.Entry<E>
Modifier and Type | Field and Description |
---|---|
private java.util.Map<E,AbstractMapMultiSet.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 multiset
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractMapMultiSet()
Constructor needed for subclass serialisation.
|
protected |
AbstractMapMultiSet(java.util.Map<E,AbstractMapMultiSet.MutableInteger> map)
Constructor that assigns the specified Map as the backing store.
|
Modifier and Type | Method and Description |
---|---|
int |
add(E object,
int occurrences)
Adds a number of occurrences of the specified object to the MultiSet.
|
void |
clear()
Clears the multiset by clearing the underlying map.
|
boolean |
contains(java.lang.Object object)
Determines if the multiset contains the given element by checking if the
underlying map contains the element as a key.
|
protected java.util.Iterator<MultiSet.Entry<E>> |
createEntrySetIterator()
Creates an entry set iterator.
|
protected java.util.Iterator<E> |
createUniqueSetIterator()
Creates a unique set iterator.
|
protected void |
doReadObject(java.io.ObjectInputStream in)
Read the multiset in using a custom routine.
|
protected void |
doWriteObject(java.io.ObjectOutputStream out)
Write the multiset out using a custom routine.
|
boolean |
equals(java.lang.Object object)
Compares this MultiSet to another object.
|
int |
getCount(java.lang.Object object)
Returns the number of occurrence of the given element in this multiset by
looking up its count in the underlying map.
|
protected java.util.Map<E,AbstractMapMultiSet.MutableInteger> |
getMap()
Utility method for implementations to access the map that backs this multiset.
|
int |
hashCode()
Gets a hash code for the MultiSet 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 multiset elements.
|
int |
remove(java.lang.Object object,
int occurrences)
Removes a number of occurrences of the specified object from the MultiSet.
|
protected void |
setMap(java.util.Map<E,AbstractMapMultiSet.MutableInteger> map)
Sets the map being wrapped.
|
int |
size()
Returns the number of elements in this multiset.
|
java.lang.Object[] |
toArray()
Returns an array of all of this multiset's elements.
|
<T> T[] |
toArray(T[] array)
Returns an array of all of this multiset's elements.
|
protected int |
uniqueElements()
Returns the number of unique elements in this multiset.
|
add, createEntrySet, createUniqueSet, entrySet, remove, removeAll, setCount, toString, uniqueSet
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
containsAll, retainAll
private transient java.util.Map<E,AbstractMapMultiSet.MutableInteger> map
private transient int size
private transient int modCount
protected AbstractMapMultiSet()
protected AbstractMapMultiSet(java.util.Map<E,AbstractMapMultiSet.MutableInteger> map)
map
- the map to assignprotected java.util.Map<E,AbstractMapMultiSet.MutableInteger> getMap()
protected void setMap(java.util.Map<E,AbstractMapMultiSet.MutableInteger> map)
NOTE: this method should only be used during deserialization
map
- the map to wrappublic int size()
public boolean isEmpty()
public int getCount(java.lang.Object object)
public boolean contains(java.lang.Object object)
contains
in interface java.util.Collection<E>
contains
in class AbstractMultiSet<E>
object
- the object to search forpublic java.util.Iterator<E> iterator()
public int add(E object, int occurrences)
MultiSet
If the object is already in the MultiSet.uniqueSet()
then increment its
count as reported by MultiSet.getCount(Object)
. Otherwise add it to the
MultiSet.uniqueSet()
and report its count as occurrences
.
add
in interface MultiSet<E>
add
in class AbstractMultiSet<E>
object
- the object to addoccurrences
- the number of occurrences to add, may be zero,
in which case no change is made to the multisetpublic void clear()
clear
in interface java.util.Collection<E>
clear
in class AbstractMultiSet<E>
public int remove(java.lang.Object object, int occurrences)
MultiSet
If the number of occurrences to remove is greater than the actual number of occurrences in the multiset, the object will be removed from the multiset.
remove
in interface MultiSet<E>
remove
in class AbstractMultiSet<E>
object
- the object to removeoccurrences
- the number of occurrences to remove, may be zero,
in which case no change is made to the multisetprotected java.util.Iterator<E> createUniqueSetIterator()
AbstractMultiSet
createUniqueSetIterator
in class AbstractMultiSet<E>
protected int uniqueElements()
AbstractMultiSet
uniqueElements
in class AbstractMultiSet<E>
protected java.util.Iterator<MultiSet.Entry<E>> createEntrySetIterator()
AbstractMultiSet
createEntrySetIterator
in class AbstractMultiSet<E>
protected void doWriteObject(java.io.ObjectOutputStream out) throws java.io.IOException
doWriteObject
in class AbstractMultiSet<E>
out
- the output streamjava.io.IOException
- any of the usual I/O related exceptionsprotected void doReadObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
doReadObject
in class AbstractMultiSet<E>
in
- 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 java.lang.Object[] toArray()
public <T> T[] toArray(T[] array)
toArray
in interface java.util.Collection<E>
toArray
in class java.util.AbstractCollection<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 boolean equals(java.lang.Object object)
MultiSet
This MultiSet equals another object if it is also a MultiSet that contains the same number of occurrences of the same elements.
public int hashCode()
MultiSet
(e==null ? 0 : e.hashCode()) ^ noOccurances)
.