Class CollectionsExt.AbstractImmutableMultiSet<C extends MultiSet<E>,E>
- java.lang.Object
-
- eu.simuline.util.CollectionsExt.AbstractImmutableMultiSet<C,E>
-
- Type Parameters:
C- the class extendingMultiSetwith elements in E.E- the class of the elements of this collection.
- Direct Known Subclasses:
CollectionsExt.ImmutableMultiSet,CollectionsExt.ImmutableSortedMultiSet
- Enclosing class:
- CollectionsExt<E>
abstract static class CollectionsExt.AbstractImmutableMultiSet<C extends MultiSet<E>,E> extends Object implements MultiSet<E>
A MultiSet which prevents being modified by throwing an exception for the modifying methods.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface eu.simuline.util.MultiSet
MultiSet.Multiplicity
-
-
Field Summary
Fields Modifier and Type Field Description private Set<CollectionsExt.Modification>modsThe set of allowed modifications.private static longserialVersionUID
-
Constructor Summary
Constructors Modifier Constructor Description privateAbstractImmutableMultiSet()(package private)AbstractImmutableMultiSet(Set<CollectionsExt.Modification> mods)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanadd(E obj)Addsobjto thisMultiSet.booleanaddAll(MultiSet<? extends E> mvs)Addsmvselementwise to this multi set taking multiplicities into account and returns whether this caused a change of the underlying set. **** strange implementation; also: changebooleanaddAll(Set<? extends E> set)Addssetelementwise to this multi set increasing multiplicities and returns whether this caused a change of the underlying set. **** strange implementation; also: changeintaddWithMult(E obj)Addsobjto thisMultiSetand returns the new multiplicity of this object.intaddWithMult(E obj, int addMult)Increases the multiplicity ofobjin thisMultiSetby the specified valueaddMultand returns the new multiplicity of this object.Set<CollectionsExt.Modification>allowedModifications()voidclear()Removes all of the elements from thisMultiSet.booleancontains(Object obj)Returns true if thisMultiSetcontains the specified element.booleancontainsAll(Collection<?> coll)Returns true if thisMultiSetcontains all of the elements in the specified collection with strictly positive multiplicity.booleanequals(Object obj)Returnstrueif and only ifobjis also aMultiSetand contains the same elements with the same multiplicities as this one.Map<E,MultiSet.Multiplicity>getMap()Returns a view of the underlying map of thisMultiSetas a map mapping each entry to its multiplicity.intgetMaxMult()Returns the maximal multiplicity of an element in this set.intgetMultiplicity(Object obj)Returns the multiplicity with which the given object occurs within this set.MultiSet.MultiplicitygetMultiplicityObj(Object obj)Returns the multiplicity object of the given object in this set ornull.ObjectgetObjWithMaxMult()Returns one of the elements in this multiple set with maximal multiplicity.Set<E>getSet()Returns a view of the underlying set of thisMultiSet.Set<Map.Entry<E,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<E>iterator()Returns an iterator over the elements in this collection which emits each element exactly once, without regarding its multiplicity.booleanremove(Object obj)Removes all instances of the specified element from thisMultiSet, if it is present with nontrivial multiplicity.booleanremoveAll(Collection<?> coll)Removes all thisMultiSet's elements that are also contained in the specified collection.intremoveWithMult(Object obj)Decrements the multiplicity ofobjin thisMultiSetif it is present and returns the old multiplicity ofobj; If this is0returns without altering thisMultiSet.intremoveWithMult(Object obj, int removeMult)Decreases the multiplicity ofobjin thisMultiSetby the specified valueremoveMultif possible and returns the old multiplicity ofobj.booleanretainAll(Collection<?> coll)Retains only the elements in thisMultiSetthat are contained in the specified collection.intsetMultiplicity(E 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.Object[]toArray()Returns an array containing all of the elements in thisMultiSetexactly once, ignoring its multiplicity.E[]toArray(E[] arr)Returns an array containing all of the elements in thisMultiSet; the runtime type of the returned array is that of the specified array.StringtoString()abstract Cunrestricted()Returns the underlying multiset without the restrictions imposed by thisCollectionsExt.ImmutableCollection.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
mods
private final Set<CollectionsExt.Modification> mods
The set of allowed modifications.
-
-
Constructor Detail
-
AbstractImmutableMultiSet
private AbstractImmutableMultiSet()
-
AbstractImmutableMultiSet
AbstractImmutableMultiSet(Set<CollectionsExt.Modification> mods)
-
-
Method Detail
-
allowedModifications
public final Set<CollectionsExt.Modification> allowedModifications()
-
unrestricted
public abstract C unrestricted()
Returns the underlying multiset without the restrictions imposed by thisCollectionsExt.ImmutableCollection. Note that the result may still throwUnsupportedOperationExceptions depending on the implementation.
-
size
public int size()
Description copied from interface:MultiSetReturns the number of pairwise different elements in thisMultiSet. If thisMultiSetcontains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
-
sizeWithMult
public int sizeWithMult()
Description copied from interface:MultiSetReturns the number of elements in thisMultiSetcounted with multiplicities. If thisMultiSetcontains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.- Specified by:
sizeWithMultin interfaceMultiSet<C extends MultiSet<E>>- Returns:
- the number of elements in this
MultiSetcounted with multiplicities, provided this does not exceedInteger.MAX_VALUE; otherwise justInteger.MAX_VALUE. - See Also:
MultiSet.size()
-
isEmpty
public boolean isEmpty()
Description copied from interface:MultiSetReturns whether this multiple set contains no element.
-
getObjWithMaxMult
public Object getObjWithMaxMult()
Description copied from interface:MultiSetReturns 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<C extends MultiSet<E>>- Returns:
- a
Object o != nullwith maximal multiplicity ornullif this multiple set is empty. - See Also:
MultiSet.isEmpty()
-
getMaxMult
public int getMaxMult()
Description copied from interface:MultiSetReturns the maximal multiplicity of an element in this set. In particular for empty sets returns0.- Specified by:
getMaxMultin interfaceMultiSet<C extends MultiSet<E>>- 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 int getMultiplicity(Object obj)
Description copied from interface:MultiSetReturns the multiplicity with which the given object occurs within this set.- Specified by:
getMultiplicityin interfaceMultiSet<C extends MultiSet<E>>- 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. - See Also:
MultiSet.setMultiplicity(Object, int),MultiSet.getMultiplicityObj(Object)
-
contains
public boolean contains(Object obj)
Description copied from interface:MultiSetReturns 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)).
-
toArray
public Object[] toArray()
Description copied from interface:MultiSetReturns 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.
-
toArray
public E[] toArray(E[] arr)
Description copied from interface:MultiSetReturns 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<C extends MultiSet<E>>- 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.
-
getMultiplicityObj
public MultiSet.Multiplicity getMultiplicityObj(Object obj)
Description copied from interface:MultiSetReturns the multiplicity object of the given object in this set ornull.- Specified by:
getMultiplicityObjin interfaceMultiSet<C extends MultiSet<E>>- 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. - See Also:
MultiSet.getMultiplicity(Object)
-
iterator
public MultiSetIterator<E> iterator()
Description copied from interface:MultiSetReturns 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.
-
addWithMult
public int addWithMult(E obj)
Description copied from interface:MultiSetAddsobjto thisMultiSetand returns the new multiplicity of this object. In other words, increments the multiplicity ofobjby one. This is a special case ofMultiSet.addWithMult(Object obj, int addMult)withaddMult==1.- Specified by:
addWithMultin interfaceMultiSet<C extends MultiSet<E>>- Parameters:
obj- aObject. Note that this object may not benull.- Returns:
- a strictly positive
intvalue: the new multiplicity ofobj.
-
addWithMult
public int addWithMult(E obj, int addMult)
Description copied from interface:MultiSetIncreases the multiplicity ofobjin thisMultiSetby the specified valueaddMultand returns the new multiplicity of this object. This generalizesMultiSet.addWithMult(Object obj)withaddMult==1.- Specified by:
addWithMultin interfaceMultiSet<C extends MultiSet<E>>- 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.
-
add
public boolean add(E obj)
Description copied from interface:MultiSetAddsobjto 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).
-
removeWithMult
public int removeWithMult(Object obj)
Description copied from interface:MultiSetDecrements the multiplicity ofobjin thisMultiSetif it is present and returns the old multiplicity ofobj; If this is0returns without altering thisMultiSet.- Specified by:
removeWithMultin interfaceMultiSet<C extends MultiSet<E>>- Parameters:
obj- aObject. Note that this object may not benull.- Returns:
- a non-negative
intvalue: the old multiplicity ofobjbefore a potential modification of thisMultiSet.
-
removeWithMult
public int removeWithMult(Object obj, int removeMult)
Description copied from interface:MultiSetDecreases the multiplicity ofobjin thisMultiSetby the specified valueremoveMultif possible and returns the old multiplicity ofobj.- Specified by:
removeWithMultin interfaceMultiSet<C extends MultiSet<E>>- 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.
-
remove
public boolean remove(Object obj)
Description copied from interface:MultiSetRemoves 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).
-
setMultiplicity
public int setMultiplicity(E obj, int newMult)
Description copied from interface:MultiSetSets the multiplicity ofobjto the value specified bymult.- Specified by:
setMultiplicityin interfaceMultiSet<C extends MultiSet<E>>- Parameters:
obj- anObjectinstance.newMult- a non-negativeintvalue.- Returns:
- the old multiplicity of
objas a non-negativeintvalue. - See Also:
MultiSet.getMultiplicity(Object)
-
containsAll
public boolean containsAll(Collection<?> coll)
Description copied from interface:MultiSetReturns true if thisMultiSetcontains all of the elements in the specified collection with strictly positive multiplicity.- Specified by:
containsAllin interfaceMultiSet<C extends MultiSet<E>>- Parameters:
coll- collection to be checked for containment in thisMultiSet.- Returns:
- true if this
MultiSetcontains all of the elements in the specified collection. - See Also:
MultiSet.contains(Object)
-
addAll
public boolean addAll(MultiSet<? extends E> mvs)
Description copied from interface:MultiSetAddsmvselementwise to this multi set taking multiplicities into account and returns whether this caused a change of the underlying set. **** strange implementation; also: change
-
addAll
public boolean addAll(Set<? extends E> set)
Description copied from interface:MultiSetAddssetelementwise to this multi set increasing multiplicities and returns whether this caused a change of the underlying set. **** strange implementation; also: change
-
removeAll
public boolean removeAll(Collection<?> coll)
Description copied from interface:MultiSetRemoves 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<C extends MultiSet<E>>- Parameters:
coll- elements to be removed from thisMultiSet.- Returns:
- true if this
MultiSetchanged as a result of the call. - See Also:
MultiSet.remove(Object),MultiSet.contains(Object)
-
retainAll
public boolean retainAll(Collection<?> coll)
Description copied from interface:MultiSetRetains 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<C extends MultiSet<E>>- Parameters:
coll- elements to be retained in thisMultiSet.- Returns:
- true if this
MultiSetchanged as a result of the call. - See Also:
MultiSet.remove(Object),MultiSet.contains(Object)
-
clear
public void clear()
Description copied from interface:MultiSetRemoves all of the elements from thisMultiSet. ThisMultiSetwill be empty after this method returns.
-
getSet
public Set<E> getSet()
Description copied from interface:MultiSetReturns a view of the underlying set of thisMultiSet. For certain implementations, this set is immutable to prevent implicit modification of thisMultiSet.
-
getMap
public Map<E,MultiSet.Multiplicity> getMap()
Description copied from interface:MultiSetReturns a view of the underlying map of thisMultiSetas a map mapping each entry to its multiplicity.
-
getSetWithMults
public Set<Map.Entry<E,MultiSet.Multiplicity>> getSetWithMults()
Description copied from interface:MultiSetReturns 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)andMultiSet.clear()operations. It does not support the methodsMultiSet.add(Object)orSet.addAll(Collection).- Specified by:
getSetWithMultsin interfaceMultiSet<C extends MultiSet<E>>
-
toString
public String toString()
-
equals
public boolean equals(Object obj)
Description copied from interface:MultiSetReturnstrueif and only ifobjis also aMultiSetand contains the same elements with the same multiplicities as this one.
-
-