Class AbstractMultiSet<T,​M extends java.util.Map<T,​java.lang.Integer>>

  • Type Parameters:
    T - the type of the objects in this multiset.
    All Implemented Interfaces:
    java.lang.Iterable<T>, java.util.Collection<T>, MultiSet<T>, HTMLToString
    Direct Known Subclasses:
    HashMultiSet, TreeMultiSet

    public abstract class AbstractMultiSet<T,​M extends java.util.Map<T,​java.lang.Integer>>
    extends java.util.AbstractCollection<T>
    implements MultiSet<T>
    This class implements a mutliset. The implementation is synchronized.
    Author:
    bfvdonge
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected M map  
      protected int size  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(T par)
      adds one object to the multiset.
      java.lang.Integer add​(T par, java.lang.Integer weight)
      Adds the given object to the multiset, as many times as indicated by the given weight parameter.
      boolean addAll​(java.util.Collection<? extends T> collection)
      Adds the given collection to this multiset.
      java.util.Set<T> baseSet()
      returns an unmodifiable set of unique objects in the multiset.
      void clear()  
      boolean contains​(java.lang.Object o)
      checks whether the number of occurrences of the given object is greater or equal to 1.
      boolean containsAll​(java.util.Collection<?> c)
      Checks whether the number of occurrences of elements in the given collection is at most what is specified in this collection, i.e., this method returns (new MultiSet(c)).isLessOrEqual(this)
      protected boolean containsAtLeast​(T element, int occ)  
      boolean equals​(java.lang.Object o)
      returns true if the multisets are equal, i.e.
      int hashCode()
      returns a hashCode for this multiset.
      boolean isEmpty()  
      boolean isLessOrEqual​(MultiSet<T> multiSet)
      returns true if this multiset is less or equal to the given multiset, i.e.
      java.util.Iterator<T> iterator()
      returns an iterator over the elements of the multiset.
      java.lang.Integer occurrences​(java.lang.Object source)
      returns the number of occurrences of the given object in this multiset.
      boolean remove​(java.lang.Object o)
      removes the given object from this multiset, if it is in there.
      boolean removeAll​(java.util.Collection<?> collection)
      removes the elements in the given multiset from this multiset.
      protected <S extends MultiSet<T>>
      S
      removeAllMultiSet​(AbstractMultiSet<?,​?> mset, S removed)  
      boolean retainAll​(java.util.Collection<?> c)
      Keeps all elements of the given collection in this multiset.
      boolean retainAll​(MultiSet<?> c)
      Keeps all elements of the given collection in this multiset.
      int size()
      returns the size of the multiset, i.e.
      java.lang.String toHTMLString​(boolean includeHTMLTags)  
      java.util.List<T> toList()
      Converts this multiset to a list, such that each element occurs as often as returned by the iterator of the multiset (its number of occurrences).
      java.lang.String toString()
      returns a string representing this multiset.
      • Methods inherited from class java.util.AbstractCollection

        toArray, toArray
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, spliterator, stream, toArray, toArray, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Field Detail

      • map

        protected M extends java.util.Map<T,​java.lang.Integer> map
      • size

        protected int size
    • Constructor Detail

      • AbstractMultiSet

        public AbstractMultiSet()
    • Method Detail

      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Keeps all elements of the given collection in this multiset. Multicities are taken into account, i.e. as many of the same objects are kept as returned by the collections iterator.
        Specified by:
        retainAll in interface java.util.Collection<T>
        Overrides:
        retainAll in class java.util.AbstractCollection<T>
        Returns:
        true if the multiset changed from calling this method.
      • add

        public boolean add​(T par)
        adds one object to the multiset. If the object was not contained before, then it now has one occerrence, otherwise, the number of occurrences is increased.
        Specified by:
        add in interface java.util.Collection<T>
        Overrides:
        add in class java.util.AbstractCollection<T>
        Returns:
        true, since the collection is always modified.
      • retainAll

        public boolean retainAll​(MultiSet<?> c)
        Keeps all elements of the given collection in this multiset. Multiplicities are taken into account.
        Returns:
        true if the multiset changed from calling this method.
      • add

        public java.lang.Integer add​(T par,
                                     java.lang.Integer weight)
        Adds the given object to the multiset, as many times as indicated by the given weight parameter.
        Specified by:
        add in interface MultiSet<T>
        Parameters:
        par - the object to add
        weight - the number of times to add it
        Returns:
        the new number of occurrences of the object (>0)
      • addAll

        public boolean addAll​(java.util.Collection<? extends T> collection)
        Adds the given collection to this multiset. If the given collection is not a multiset, then the implementation is diverted to abstractcollection.
        Specified by:
        addAll in interface java.util.Collection<T>
        Overrides:
        addAll in class java.util.AbstractCollection<T>
        Parameters:
        collection - the collection to add
        Returns:
        true if the multiset changed due to this method call.
      • toList

        public java.util.List<T> toList()
        Converts this multiset to a list, such that each element occurs as often as returned by the iterator of the multiset (its number of occurrences).
        Specified by:
        toList in interface MultiSet<T>
        Returns:
        a list of objects as returned by the iterator
      • isLessOrEqual

        public boolean isLessOrEqual​(MultiSet<T> multiSet)
        returns true if this multiset is less or equal to the given multiset, i.e. all objects in this multiset should be contained in the given set and the number of occurrences in the given set is at least the number of occurrences in this multiset.
        Specified by:
        isLessOrEqual in interface MultiSet<T>
        Parameters:
        multiSet - the multiset to test
        Returns:
        true if the given multiset is less or equal.
      • containsAtLeast

        protected boolean containsAtLeast​(T element,
                                          int occ)
      • equals

        public boolean equals​(java.lang.Object o)
        returns true if the multisets are equal, i.e. if they contain the same objects with the same number of occurrences.
        Specified by:
        equals in interface java.util.Collection<T>
        Overrides:
        equals in class java.lang.Object
      • size

        public int size()
        returns the size of the multiset, i.e. the sum over the multiplicities of all contained objects.
        Specified by:
        size in interface java.util.Collection<T>
        Specified by:
        size in class java.util.AbstractCollection<T>
      • toString

        public java.lang.String toString()
        returns a string representing this multiset. The string contains, between brackets, pairs of objects and their multiplicities.
        Overrides:
        toString in class java.util.AbstractCollection<T>
      • hashCode

        public int hashCode()
        returns a hashCode for this multiset.
        Specified by:
        hashCode in interface java.util.Collection<T>
        Overrides:
        hashCode in class java.lang.Object
      • occurrences

        public java.lang.Integer occurrences​(java.lang.Object source)
        returns the number of occurrences of the given object in this multiset.
        Specified by:
        occurrences in interface MultiSet<T>
        Parameters:
        source - the object to get the occurrences for
        Returns:
        the number of occurrences, 0 if the object does not occur.
      • iterator

        public java.util.Iterator<T> iterator()
        returns an iterator over the elements of the multiset. Note that if an object appears n times in the multiset, it is returned n times by the iterator. For an iterator over unique elements of the multiset, use the toSet() method.
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in class java.util.AbstractCollection<T>
      • baseSet

        public java.util.Set<T> baseSet()
        returns an unmodifiable set of unique objects in the multiset.
        Specified by:
        baseSet in interface MultiSet<T>
        Returns:
        an unmodifiable set of unique objects in the multiset.
      • contains

        public boolean contains​(java.lang.Object o)
        checks whether the number of occurrences of the given object is greater or equal to 1.
        Specified by:
        contains in interface java.util.Collection<T>
        Overrides:
        contains in class java.util.AbstractCollection<T>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Checks whether the number of occurrences of elements in the given collection is at most what is specified in this collection, i.e., this method returns (new MultiSet(c)).isLessOrEqual(this)
        Specified by:
        containsAll in interface java.util.Collection<T>
        Overrides:
        containsAll in class java.util.AbstractCollection<T>
      • remove

        public boolean remove​(java.lang.Object o)
        removes the given object from this multiset, if it is in there. Only one occurrence is removed, i.e. contains(o) can still be true after calling remove(o)
        Specified by:
        remove in interface java.util.Collection<T>
        Overrides:
        remove in class java.util.AbstractCollection<T>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> collection)
        removes the elements in the given multiset from this multiset.
        Specified by:
        removeAll in interface java.util.Collection<T>
        Overrides:
        removeAll in class java.util.AbstractCollection<T>
        Parameters:
        mset - the multiset of elements needing to be removed.
        Returns:
        a new multiset where the occurrences are the occurrences in this multiset, minus the occurrences in the given multiset
      • toHTMLString

        public java.lang.String toHTMLString​(boolean includeHTMLTags)
        Specified by:
        toHTMLString in interface HTMLToString
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<T>
        Overrides:
        clear in class java.util.AbstractCollection<T>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<T>
        Overrides:
        isEmpty in class java.util.AbstractCollection<T>