Class AbstractMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>,T>
- java.lang.Object
-
- eu.simuline.util.AbstractMultiSet<MAP,T>
-
- Type Parameters:
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 2014
- All Implemented Interfaces:
MultiSet<T>,java.lang.Iterable<T>
- Direct Known Subclasses:
HashMultiSet,TreeMultiSet
public abstract class AbstractMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>,T> extends java.lang.Object implements MultiSet<T>
Represents an abstract MultiSet based on aMap.addAll's implementation seems strange, add seems to be buggy, Problem with overflow of multiplicities.
- Version:
- 1.0
- Author:
- Ernst Reissner
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractMultiSet.MultiplicityImplServes as a wrapper object for a multiplicityAbstractMultiSet.MultiplicityImpl.mult.protected static classAbstractMultiSet.MultiSetIteratorImpl<T>A canonical implementation ofMultiSetIteratordefining also the methods modifying the underlyingMultiSet, namelyAbstractMultiSet.MultiSetIteratorImpl.remove(),AbstractMultiSet.MultiSetIteratorImpl.setMult(int)andAbstractMultiSet.MultiSetIteratorImpl.removeMult(int).-
Nested classes/interfaces inherited from interface eu.simuline.util.MultiSet
MultiSet.Multiplicity
-
-
Constructor Summary
Constructors Constructor Description AbstractMultiSet(MAP t2mult)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(T obj)Addsobjto thisMultiSet.booleanaddAll(MultiSet<? extends T> mvs)Addsmvselementwise to this multi set increasing multiplicities and returns whether this caused a change of the underlying set. **** strange implementation; also: changebooleanaddAll(java.util.Set<? extends T> set)Addssetelementwise to this multi set increasing multiplicities and returns whether this caused a change of the underlying set. **** strange implementation; also: changeintaddWithMult(T obj)Addsobjto thisMultiSetand returns the new multiplicity of this object.intaddWithMult(T obj, int addMult)Increases the multiplicity ofobjin thisMultiSetby the specified valueaddMultand returns the new multiplicity of this object.voidclear()Removes all of the elements from thisMultiSet.booleancontains(java.lang.Object obj)Returns true if thisMultiSetcontains the specified element.booleancontainsAll(java.util.Collection<?> coll)Returns true if thisMultiSetcontains all of the elements in the specified collection with strictly positive multiplicity.booleanequals(java.lang.Object obj)Returnstrueif and only ifobjis also aMultiSetand contains the same elements with the same multiplicities as this one.intgetMaxMult()Returns the maximal multiplicity of an element in this set.private java.util.Map.Entry<T,MultiSet.Multiplicity>getMaxObjWithMult()Returns a Map.Entry representing an element in thisMultiSetwith maximal multiplicity together with this multiplicity, except if this set is empty.intgetMultiplicity(java.lang.Object obj)Returns the multiplicity with which the given object occurs within this set.MultiSet.MultiplicitygetMultiplicityObj(java.lang.Object obj)Returns the multiplicity object of the given object in this set ornull.TgetObjWithMaxMult()Returns one of the elements in this multiple set with maximal multiplicity.java.util.Set<java.util.Map.Entry<T,MultiSet.Multiplicity>>getSetWithMults()Returns a Set view of the mapping from the element of thisMultiSetto the according multiplicities.inthashCode()booleanisEmpty()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.booleanremove(java.lang.Object obj)Removes all instances of the specified element from thisMultiSet, if it is present with nontrivial multiplicity.booleanremoveAll(java.util.Collection<?> coll)Removes all thisMultiSet's elements that are also contained in the specified collection.intremoveWithMult(java.lang.Object obj)Decrements the multiplicity ofobjin thisMultiSetif it is present and returns the old multiplicity ofobj; If this is0returns without altering thisMultiSet.intremoveWithMult(java.lang.Object obj, int removeMult)Decreases the multiplicity ofobjin thisMultiSetby the specified valueremoveMultif possible and returns the old multiplicity ofobj.booleanretainAll(java.util.Collection<?> coll)Retains only the elements in thisMultiSetthat are contained in the specified collection.intsetMultiplicity(T obj, int newMult)Sets the multiplicity ofobjto the value specified bymult.intsize()Returns the number of pairwise different elements in thisMultiSet.intsizeWithMult()Returns the number of elements in thisMultiSetcounted with multiplicities.java.lang.Object[]toArray()Returns an array containing all of the elements in thisMultiSetexactly once, ignoring its multiplicity.T[]toArray(T[] arr)Returns an array containing all of the elements in thisMultiSet; the runtime type of the returned array is that of the specified array.
-
-
-
Field Detail
-
obj2mult
protected final MAP extends java.util.Map<T,MultiSet.Multiplicity> obj2mult
Maps objects to its multiplicities. The keys are objects whereas the corresponding values are strictly positiveIntegers representing the corresponding multiplicities. If an object is not within this set, the corresponding value isnull. **** maybe even: object not in keyset. In the key set nonullvalues may occur.
-
-
Constructor Detail
-
AbstractMultiSet
public AbstractMultiSet(MAP t2mult)
-
-
Method Detail
-
size
public final int size()
Returns the number of pairwise different elements in thisMultiSet. If thisMultiSetcontains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.- Specified by:
sizein interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Returns:
- the number of elements in this
MultiSeteach multiple element counted as a single one. - See Also:
sizeWithMult()
-
sizeWithMult
public final int sizeWithMult()
Returns the number of elements in thisMultiSetcounted with multiplicities. If thisMultiSetcontains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.- Specified by:
sizeWithMultin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Returns:
- the number of elements in this
MultiSetcounted with multiplicities, provided this does not exceedInteger.MAX_VALUE; otherwise justInteger.MAX_VALUE. - See Also:
size()
-
isEmpty
public final boolean isEmpty()
Returns whether this multiple set contains no element.- Specified by:
isEmptyin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Returns:
- whether this multiple set contains no element.
-
getMaxObjWithMult
private java.util.Map.Entry<T,MultiSet.Multiplicity> getMaxObjWithMult()
Returns a Map.Entry representing an element in thisMultiSetwith maximal multiplicity together with this multiplicity, except if this set is empty. For empty sets,nullis returned.- Returns:
- if this
MultiSetis not empty, aMap.Entryobjectemis returned:em.getKey()is an element of thisMultiSetandem.getValue()is aMultiplicitywrapping its multiplicitym = em.getValue().get(). This multiplicity is maximal but if there is more than one such maximal multiplicity, it is not specified whichMap.Entryis returned.Note that
em.getKey()may never benull - if this
MultiSetis empty,nullis returned.
- if this
- See Also:
getObjWithMaxMult(),getMaxMult()
-
getObjWithMaxMult
public final T getObjWithMaxMult()
Returns one of the elements in this multiple set with maximal multiplicity. The return value isnullif and only if this set is empty.- Specified by:
getObjWithMaxMultin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Returns:
- a
Object o != nullwith maximal multiplicity ornullif this multiple set is empty. - See Also:
isEmpty()
-
getMaxMult
public final int getMaxMult()
Returns the maximal multiplicity of an element in this set. In particular for empty sets returns0.- Specified by:
getMaxMultin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Returns:
- a non-negative
intvalue which is the maximal mutliplicity of an element in this set. In particular this is0if and only if this set is empty.
-
getMultiplicity
public final int getMultiplicity(java.lang.Object obj)
Returns the multiplicity with which the given object occurs within this set.- Specified by:
getMultiplicityin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
obj- anObjectand not null.- Returns:
- a non-negative
intvalue which is the mutliplicity of the given element in this set. In particular this is0if and only ifobjis an instance which is not in this set. - Throws:
java.lang.NullPointerException- forobj==null.- See Also:
setMultiplicity(Object, int),getMultiplicityObj(Object)
-
getMultiplicityObj
public final MultiSet.Multiplicity getMultiplicityObj(java.lang.Object obj)
Returns the multiplicity object of the given object in this set ornull.- Specified by:
getMultiplicityObjin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
obj- anObjectand not null.- Returns:
- If
objis an instance which is in this set, a multiplicity object wrapping the multiplicity is returned. Ifobjis an instance which is not in this set,nullis returned. - Throws:
java.lang.NullPointerException- forobj==null.- See Also:
getMultiplicity(Object)
-
contains
public final boolean contains(java.lang.Object obj)
Returns true if thisMultiSetcontains the specified element. More formally, returns true if and only if thisMultiSetcontains at least one element e such that (o==null ? e==null : o.equals(e)).- Specified by:
containsin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
obj- element (notnull) whose presence in thisMultiSetis to be tested.- Returns:
- true if this
MultiSetcontains the specified element. - Throws:
java.lang.NullPointerException- forobj==null.
-
iterator
public final MultiSetIterator<T> iterator()
Returns an iterator over the elements in this collection which emits each element exactly once, without regarding its multiplicity. For certain implementations, the iterator returned does not allow modifications of the underlying (multi-)set.- Specified by:
iteratorin interfacejava.lang.Iterable<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Specified by:
iteratorin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Returns:
- an Iterator over the elements in this collection considering each element exactly once ignoring its multiplicity.
-
toArray
public final java.lang.Object[] toArray()
Returns an array containing all of the elements in thisMultiSetexactly 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 thisMultiSetis backed by an array). The caller is thus free to modify the returned array.- Specified by:
toArrayin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Returns:
- an array containing all of the elements in this collection
- See Also:
iterator()
-
toArray
public final T[] toArray(T[] arr)
Returns an array containing all of the elements in thisMultiSet; the runtime type of the returned array is that of the specified array. If theMultiSetfits 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 thisMultiSet.If this
MultiSetfits in the specified array with room to spare (i.e., the array has more elements than thisMultiSet), the elementin the array immediately following the end of theMultiSetis set to null. This is useful in determining the length of thisMultiSetbecause thisMultiSetdoes 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().
- Specified by:
toArrayin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
arr- the array into which the elements of thisMultiSetare to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.- Returns:
- an array containing each elements of this
MultiSetexactly once. - Throws:
java.lang.ArrayStoreException- the runtime type of the specified array is not a supertype of the runtime type of every element in thisMultiSet.java.lang.NullPointerException- if the specified array is null.
-
addWithMult
public final int addWithMult(T obj)
Addsobjto thisMultiSetand returns the new multiplicity of this object. In other words, increments the multiplicity ofobjby one.- Specified by:
addWithMultin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
obj- aObject. Note that this object may not benull.- Returns:
- a strictly positive
intvalue: the new multiplicity ofobj. - Throws:
java.lang.NullPointerException- if the specified element is null.java.lang.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
addWithMult
public final int addWithMult(T obj, int addMult)
Increases the multiplicity ofobjin thisMultiSetby the specified valueaddMultand returns the new multiplicity of this object.- Specified by:
addWithMultin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
obj- anObjectinstance.addMult- a non-negative integer specifying the multiplicity with whichobjis to be added.- Returns:
- a non-negative
intvalue: the new multiplicity ofobj. - Throws:
java.lang.IllegalArgumentException- foraddMult < 0.java.lang.NullPointerException- forobj==nullprovidedaddMult ≥ 0.java.lang.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
add
public final boolean add(T obj)
Addsobjto thisMultiSet. In other words, increments the multiplicity ofobjby one. Returns true if thisMultiSetinterpreted as a set changed as a result of the call. (Returns false if thisMultiSetalready contains the specified element (with nontrivial multiplicity).- Specified by:
addin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
obj- element the multiplicity of which in thisMultiSetis to be increased by one. Note that this may not benull.- Returns:
- true if and only if
the multiplicity of the specified element
was
0before the call of this method. - Throws:
java.lang.NullPointerException- if the specified element isnull.java.lang.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
removeWithMult
public final int removeWithMult(java.lang.Object obj)
Decrements the multiplicity ofobjin thisMultiSetif it is present and returns the old multiplicity ofobj; If this is0returns without altering thisMultiSet.- Specified by:
removeWithMultin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
obj- aObject. Note that this object may not benull.- Returns:
- a non-negative
intvalue: the old multiplicity ofobjbefore a potential modification of thisMultiSet. - Throws:
java.lang.NullPointerException- if the specified element is null.java.lang.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
removeWithMult
public final int removeWithMult(java.lang.Object obj, int removeMult)Decreases the multiplicity ofobjin thisMultiSetby the specified valueremoveMultif possible and returns the old multiplicity ofobj.- Specified by:
removeWithMultin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
obj- anObjectinstance.removeMult- a non-negative integer specifying the multiplicity with whichobjis to be removed.- Returns:
- a non-negative
intvalue: the old multiplicity ofobjbefore a potential modification of thisMultiSet. - Throws:
java.lang.NullPointerException- forobj == null.java.lang.IllegalArgumentException- forremoveMult < 0and also ifremoveMult - obj.getMultiplicity() < 0.java.lang.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
remove
public final boolean remove(java.lang.Object obj)
Removes all instances of the specified element from thisMultiSet, if it is present with nontrivial multiplicity. More formally, immediately after having (successively) invokeds.remove(o), the conditions.contains(o) == falseis satisfied. Returns true if thisMultiSetcontained the specified element (or equivalently, if (the underlying set of) thisMultiSetchanged as a result of the call).- Specified by:
removein interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
obj- element the multiplicity of which in thisMultiSetis to be increased by one.- Returns:
- true if and only if this
MultiSetchanged as a result of the call. - Throws:
java.lang.NullPointerException- if the specified element isnull.java.lang.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
setMultiplicity
public final int setMultiplicity(T obj, int newMult)
Sets the multiplicity ofobjto the value specified bymult.- Specified by:
setMultiplicityin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
obj- anObjectinstance.newMult- a non-negativeintvalue.- Returns:
- the old multiplicity of
objas a non-negativeintvalue. - Throws:
java.lang.IllegalArgumentException- if eitherobj == nullormult ≤ 0.java.lang.UnsupportedOperationException- if thisMultiSetdoes not support this method.- See Also:
getMultiplicity(Object)
-
containsAll
public final boolean containsAll(java.util.Collection<?> coll)
Returns true if thisMultiSetcontains all of the elements in the specified collection with strictly positive multiplicity.- Specified by:
containsAllin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
coll- collection to be checked for containment in thisMultiSet.- Returns:
- true if this
MultiSetcontains all of the elements in the specified collection. - Throws:
java.lang.NullPointerException- if the specified collection contains one or more null elements.java.lang.NullPointerException- if the specified collection is null.- See Also:
contains(Object)
-
addAll
public final boolean addAll(MultiSet<? extends T> mvs)
Addsmvselementwise to this multi set increasing multiplicities and returns whether this caused a change of the underlying set. **** strange implementation; also: change- Specified by:
addAllin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
mvs- aMultiSetobject.- Returns:
- returns whether adding changed this
MultiSetinterpreted as a set. - Throws:
java.lang.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
addAll
public final boolean addAll(java.util.Set<? extends T> set)
Addssetelementwise to this multi set increasing multiplicities and returns whether this caused a change of the underlying set. **** strange implementation; also: change- Specified by:
addAllin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
set- aSetobject.- Returns:
- returns whether adding changed this
MultiSetinterpreted as a set. - Throws:
java.lang.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
removeAll
public final boolean removeAll(java.util.Collection<?> coll)
Removes all thisMultiSet's elements that are also contained in the specified collection. After this call returns, thisMultiSetwill contain no elements in common with the specified collection.- Specified by:
removeAllin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
coll- elements to be removed from thisMultiSet.- Returns:
- true if this
MultiSetchanged as a result of the call. - Throws:
java.lang.NullPointerException- if the specified collection isnull.java.lang.UnsupportedOperationException- if thisMultiSetdoes not support this method.- See Also:
remove(Object),contains(Object)
-
retainAll
public final boolean retainAll(java.util.Collection<?> coll)
Retains only the elements in thisMultiSetthat are contained in the specified collection. In other words, removes from thisMultiSetall of its elements that are not contained in the specified collection.- Specified by:
retainAllin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Parameters:
coll- elements to be retained in thisMultiSet.- Returns:
- true if this
MultiSetchanged as a result of the call. - Throws:
java.lang.NullPointerException- if the specified collection is null.java.lang.UnsupportedOperationException- if thisMultiSetdoes not support this method.- See Also:
remove(Object),contains(Object)
-
clear
public final void clear()
Removes all of the elements from thisMultiSet. ThisMultiSetwill be empty after this method returns.- Specified by:
clearin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Throws:
java.lang.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
getSetWithMults
public final java.util.Set<java.util.Map.Entry<T,MultiSet.Multiplicity>> getSetWithMults()
Returns a Set view of the mapping from the element of thisMultiSetto the according multiplicities. The set is backed by theMultiSet, so changes to the map are reflected in the set, and vice-versa. If theMultiSetis 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 theMultiSet, via theIterator.remove(),Set.remove(Object),Set.removeAll(Collection),Set.retainAll(Collection)andclear()operations. It does not support the methodsadd(Object)orSet.addAll(Collection).- Specified by:
getSetWithMultsin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>
-
equals
public final boolean equals(java.lang.Object obj)
Returnstrueif and only ifobjis also aMultiSetand contains the same elements with the same multiplicities as this one.- Specified by:
equalsin interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- anObject, possiblynull.- Returns:
- a
trueif and only ifobjis also aMultiSetand contains the same elements with the same multiplicities as this one.
-
hashCode
public final int hashCode()
- Specified by:
hashCodein interfaceMultiSet<MAP extends java.util.Map<T,MultiSet.Multiplicity>>- Overrides:
hashCodein classjava.lang.Object
-
-