MAP - the map assigning to each element of this multi-set its multiplicity.T - the class of the elements of this multi-set.
Created: Sun Nov 23 23:32:06 2014public abstract class AbstractMultiSet<MAP extends Map<T,MultiSet.Multiplicity>,T> extends Object implements MultiSet<T>
Map.
addAll's implementation seems strange, add seems to be buggy, Problem with overflow of multiplicities.
| Modifier and Type | Class and Description |
|---|---|
static class |
AbstractMultiSet.MultiplicityImpl
Serves as a wrapper object for a multiplicity
AbstractMultiSet.MultiplicityImpl.mult. |
protected static class |
AbstractMultiSet.MultiSetIteratorImpl<T>
A canonical implementation of
MultiSetIterator
defining also the methods modifying the underlying MultiSet,
namely AbstractMultiSet.MultiSetIteratorImpl.remove(), AbstractMultiSet.MultiSetIteratorImpl.setMult(int)
and AbstractMultiSet.MultiSetIteratorImpl.removeMult(int). |
MultiSet.Multiplicity| Modifier and Type | Field and Description |
|---|---|
protected MAP |
obj2mult
Maps objects to its multiplicities.
|
| Constructor and Description |
|---|
AbstractMultiSet(MAP t2mult) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(T obj)
Adds
obj to this MultiSet. |
boolean |
addAll(MultiSet<? extends T> mvs)
Adds
mvs elementwise to this multi set
increasing multiplicities
and returns whether this caused a change
of the underlying set.
**** strange implementation; also: change |
boolean |
addAll(Set<? extends T> set)
Adds
set elementwise to this multi set
increasing multiplicities
and returns whether this caused a change
of the underlying set.
**** strange implementation; also: change |
int |
addWithMult(T obj)
Adds
obj to this MultiSet
and returns the new multiplicity of this object. |
int |
addWithMult(T obj,
int addMult)
Increases the multiplicity of
obj
in this MultiSet
by the specified value addMult
and returns the new multiplicity of this object. |
void |
clear()
Removes all of the elements from this
MultiSet. |
boolean |
contains(Object obj)
Returns true if this
MultiSet
contains the specified element. |
boolean |
containsAll(Collection<?> coll)
Returns true if this
MultiSet
contains all of the elements in the specified collection
with strictly positive multiplicity. |
boolean |
equals(Object obj)
Returns
true if and only if obj
is also a MultiSet
and contains the same elements with the same multiplicities
as this one. |
int |
getMaxMult()
Returns the maximal multiplicity of an element in this set.
|
private Map.Entry<T,MultiSet.Multiplicity> |
getMaxObjWithMult()
Returns a Map.Entry
representing an element in this
MultiSet
with maximal multiplicity together with this multiplicity,
except if this set is empty. |
int |
getMultiplicity(Object obj)
Returns the multiplicity
with which the given object occurs within this set.
|
MultiSet.Multiplicity |
getMultiplicityObj(Object obj)
Returns the multiplicity object of the given object in this set
or
null. |
T |
getObjWithMaxMult()
Returns one of the elements in this multiple set
with maximal multiplicity.
|
Set<Map.Entry<T,MultiSet.Multiplicity>> |
getSetWithMults()
Returns a Set view of the mapping
from the element of this
MultiSet
to the according multiplicities. |
int |
hashCode() |
boolean |
isEmpty()
Returns whether this multiple set contains no element.
|
MultiSetIterator<T> |
iterator()
Returns an iterator over the elements in this collection
which emits each element exactly once,
without regarding its multiplicity
|
boolean |
remove(Object obj)
Removes all instances of the specified element from this
MultiSet, if it is present with nontrivial multiplicity. |
boolean |
removeAll(Collection<?> coll)
Removes all this
MultiSet's elements
that are also contained in the specified collection. |
int |
removeWithMult(Object obj)
Decrements the multiplicity of
obj
in this MultiSet if it is present and
returns the old multiplicity of obj;
If this is 0 returns
without altering this MultiSet. |
int |
removeWithMult(Object obj,
int removeMult)
Decreases the multiplicity of
obj
in this MultiSet
by the specified value removeMult if possible
and returns the old multiplicity of obj. |
boolean |
retainAll(Collection<?> coll)
Retains only the elements in this
MultiSet
that are contained in the specified collection. |
int |
setMultiplicity(T obj,
int newMult)
Sets the multiplicity of
obj to the value
specified by mult. |
int |
size()
Returns the number of pairwise different elements
in this
MultiSet. |
int |
sizeWithMult()
Returns the number of elements
in this
MultiSet counted with multiplicities. |
Object[] |
toArray()
Returns an array containing all of the elements
in this
MultiSet exactly once, ignoring its multiplicity |
T[] |
toArray(T[] arr)
Returns an array containing all of the elements
in this
MultiSet;
the runtime type of the returned array is that of the specified array. |
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitforEach, spliteratorprotected final MAP extends Map<T,MultiSet.Multiplicity> obj2mult
Integers
representing the corresponding multiplicities.
If an object is not within this set,
the corresponding value is null.
**** maybe even: object not in keyset.
In the key set no null values may occur.public AbstractMultiSet(MAP t2mult)
public final int size()
MultiSet.
If this MultiSet
contains more than Integer.MAX_VALUE elements,
returns Integer.MAX_VALUE.size in interface MultiSet<T>MultiSet
each multiple element counted as a single one.sizeWithMult()public final int sizeWithMult()
MultiSet counted with multiplicities.
If this MultiSet
contains more than Integer.MAX_VALUE elements,
returns Integer.MAX_VALUE.sizeWithMult in interface MultiSet<T>MultiSet
counted with multiplicities,
provided this does not exceed Integer.MAX_VALUE;
otherwise just Integer.MAX_VALUE.size()public final boolean isEmpty()
private Map.Entry<T,MultiSet.Multiplicity> getMaxObjWithMult()
MultiSet
with maximal multiplicity together with this multiplicity,
except if this set is empty.
For empty sets, null is returned.MultiSet is not empty,
a Map.Entry object em is returned:
em.getKey() is an element of this MultiSet
and em.getValue() is a Multiplicity
wrapping its multiplicity m = em.getValue().get().
This multiplicity is maximal
but if there is more than one such maximal multiplicity,
it is not specified which Map.Entry is returned.
Note that em.getKey() may never be null
MultiSet is empty,
null is returned.
getObjWithMaxMult(),
getMaxMult()public final T getObjWithMaxMult()
null
if and only if this set is empty.getObjWithMaxMult in interface MultiSet<T>Object o != null with maximal multiplicity
or null if this multiple set is empty.isEmpty()public final int getMaxMult()
0.getMaxMult in interface MultiSet<T>int value
which is the maximal mutliplicity of an element in this set.
In particular this is 0
if and only if this set is empty.public final int getMultiplicity(Object obj)
getMultiplicity in interface MultiSet<T>obj - an Object and not null.int value
which is the mutliplicity of the given element in this set.
In particular this is 0 if and only if
obj is an instance which is not in this set.NullPointerException - for obj==null.setMultiplicity(Object, int),
getMultiplicityObj(Object)public final MultiSet.Multiplicity getMultiplicityObj(Object obj)
null.getMultiplicityObj in interface MultiSet<T>obj - an Object and not null.obj is an instance which is in this set,
a multiplicity object wrapping the multiplicity is returned.
If obj is an instance which is not in this set,
null is returned.NullPointerException - for obj==null.getMultiplicity(Object)public final boolean contains(Object obj)
MultiSet
contains the specified element.
More formally, returns true if and only if this
MultiSet contains at least one element e
such that (o==null ? e==null : o.equals(e)).contains in interface MultiSet<T>obj - element (not null)
whose presence in this MultiSet is to be tested.MultiSet
contains the specified element.NullPointerException - for obj==null.public final MultiSetIterator<T> iterator()
public final Object[] toArray()
MultiSet exactly once, ignoring its multiplicity.
The returned array will be "safe" in that no references to it
are maintained by this MultiSet.
(In other words, this method must allocate a new array
even if this MultiSet is backed by an array).
The caller is thus free to modify the returned array.
toArray in interface MultiSet<T>iterator()public final T[] toArray(T[] arr)
MultiSet;
the runtime type of the returned array is that of the specified array.
If the MultiSet fits in the specified array,
it is returned therein.
Otherwise, a new array is allocated with the runtime type
of the specified array and the size of this MultiSet.
If this MultiSet fits in the specified array
with room to spare
(i.e., the array has more elements than this MultiSet),
the elementin the array
immediately following the end of the MultiSet
is set to null.
This is useful in determining the length of this
MultiSet because this MultiSet does
not contain any null elements.
Suppose l is a List known to contain only strings. The following code can be used to dump the list into a newly allocated array of String:
String[] x = (String[]) v.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to toArray().
toArray in interface MultiSet<T>arr - the array into which the elements of this MultiSet
are to be stored, if it is big enough;
otherwise, a new array of the same runtime type
is allocated for this purpose.MultiSet
exactly once.ArrayStoreException - the runtime type of the specified array is not a supertype
of the runtime type of every element in this MultiSet.NullPointerException - if the specified array is null.public final int addWithMult(T obj)
obj to this MultiSet
and returns the new multiplicity of this object.
In other words, increments the multiplicity of obj by one.addWithMult in interface MultiSet<T>obj - a Object.
Note that this object may not be null.int value:
the new multiplicity of obj.NullPointerException - if the specified element is null.UnsupportedOperationException - if this MultiSet does not support this method.public final int addWithMult(T obj, int addMult)
obj
in this MultiSet
by the specified value addMult
and returns the new multiplicity of this object.addWithMult in interface MultiSet<T>obj - an Object instance.addMult - a non-negative integer specifying the multiplicity
with which obj is to be added.int value:
the new multiplicity of obj.IllegalArgumentException - for addMult < 0.NullPointerException - for obj==null provided addMult ≥ 0.UnsupportedOperationException - if this MultiSet does not support this method.public final boolean add(T obj)
obj to this MultiSet.
In other words, increments the multiplicity of obj by one.
Returns true if this MultiSet
interpreted as a set changed as a result of the call.
(Returns false if this MultiSet
already contains the specified element (with nontrivial multiplicity).
add in interface MultiSet<T>obj - element the multiplicity of which in this MultiSet
is to be increased by one.
Note that this may not be null.0 before the call of this method.NullPointerException - if the specified element is null.UnsupportedOperationException - if this MultiSet does not support this method.public final int removeWithMult(Object obj)
obj
in this MultiSet if it is present and
returns the old multiplicity of obj;
If this is 0 returns
without altering this MultiSet.removeWithMult in interface MultiSet<T>obj - a Object.
Note that this object may not be null.int value:
the old multiplicity of obj
before a potential modification of this MultiSet.NullPointerException - if the specified element is null.UnsupportedOperationException - if this MultiSet does not support this method.public final int removeWithMult(Object obj, int removeMult)
obj
in this MultiSet
by the specified value removeMult if possible
and returns the old multiplicity of obj.removeWithMult in interface MultiSet<T>obj - an Object instance.removeMult - a non-negative integer specifying the multiplicity
with which obj is to be removed.int value:
the old multiplicity of obj
before a potential modification of this MultiSet.NullPointerException - for obj == null.IllegalArgumentException - for removeMult < 0 and also if
removeMult - obj.getMultiplicity() < 0.UnsupportedOperationException - if this MultiSet does not support this method.public final boolean remove(Object obj)
MultiSet, if it is present with nontrivial multiplicity.
More formally,
immediately after having (successively)
invoked s.remove(o),
the condition s.contains(o) == false is satisfied.
Returns true if this MultiSet contained the specified
element (or equivalently, if (the underlying set of)
this MultiSet changed as a result of the call).remove in interface MultiSet<T>obj - element the multiplicity of which in this MultiSet
is to be increased by one.MultiSet changed
as a result of the call.NullPointerException - if the specified element is null.UnsupportedOperationException - if this MultiSet does not support this method.public final int setMultiplicity(T obj, int newMult)
obj to the value
specified by mult.setMultiplicity in interface MultiSet<T>obj - an Object instance.newMult - a non-negative int value.obj
as a non-negative int value.IllegalArgumentException - if either obj == null or mult ≤ 0.UnsupportedOperationException - if this MultiSet does not support this method.getMultiplicity(Object)public final boolean containsAll(Collection<?> coll)
MultiSet
contains all of the elements in the specified collection
with strictly positive multiplicity.containsAll in interface MultiSet<T>coll - collection to be checked for containment
in this MultiSet.MultiSet
contains all of the elements in the specified collection.NullPointerException - if the specified collection contains one or more null elements.NullPointerException - if the specified collection is null.contains(Object)public final boolean addAll(MultiSet<? extends T> mvs)
mvs elementwise to this multi set
increasing multiplicities
and returns whether this caused a change
of the underlying set.
**** strange implementation; also: changeaddAll in interface MultiSet<T>mvs - a MultiSet object.MultiSet
interpreted as a set.UnsupportedOperationException - if this MultiSet does not support this method.public final boolean addAll(Set<? extends T> set)
set elementwise to this multi set
increasing multiplicities
and returns whether this caused a change
of the underlying set.
**** strange implementation; also: changeaddAll in interface MultiSet<T>set - a Set object.MultiSet
interpreted as a set.UnsupportedOperationException - if this MultiSet does not support this method.public final boolean removeAll(Collection<?> coll)
MultiSet's elements
that are also contained in the specified collection.
After this call returns, this MultiSet
will contain no elements in common with the specified collection.removeAll in interface MultiSet<T>coll - elements to be removed from this MultiSet.MultiSet
changed as a result of the call.NullPointerException - if the specified collection is null.UnsupportedOperationException - if this MultiSet does not support this method.remove(Object),
contains(Object)public final boolean retainAll(Collection<?> coll)
MultiSet
that are contained in the specified collection.
In other words, removes from this MultiSet
all of its elements that are not contained
in the specified collection.retainAll in interface MultiSet<T>coll - elements to be retained in this MultiSet.MultiSet changed
as a result of the call.NullPointerException - if the specified collection is null.UnsupportedOperationException - if this MultiSet does not support this method.remove(Object),
contains(Object)public final void clear()
MultiSet.
This MultiSet will be empty after this method returns.clear in interface MultiSet<T>UnsupportedOperationException - if this MultiSet does not support this method.public final Set<Map.Entry<T,MultiSet.Multiplicity>> getSetWithMults()
MultiSet
to the according multiplicities.
The set is backed by the MultiSet,
so changes to the map are reflected in the set, and vice-versa.
If the MultiSet is modified
while an iteration over the set is in progress
(except through the iterator's own remove operation,
or through the setValue operation on a map entry
returned by the iterator) the results of the iteration are undefined.
The set may support element removal,
which removes the corresponding element from the MultiSet,
via the Iterator.remove(), Set.remove(Object),
Set.removeAll(Collection), Set.retainAll(Collection)
and clear() operations.
It does not support the methods
add(Object) or Set.addAll(Collection).getSetWithMults in interface MultiSet<T>public final boolean equals(Object obj)
true if and only if obj
is also a MultiSet
and contains the same elements with the same multiplicities
as this one.Copyright © 2012–2018 Simuline Organization (l2r). All rights reserved.