Class AbstractMapMultiSet<E>

  • All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, MultiSet<E>
    Direct Known Subclasses:
    HashMultiSet

    public abstract class AbstractMapMultiSet<E>
    extends AbstractMultiSet<E>
    Abstract implementation of the 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.

    Since:
    4.1
    Version:
    $Id: AbstractMapMultiSet.java 1715563 2015-11-21 20:13:35Z tn $
    • Constructor Detail

      • AbstractMapMultiSet

        protected AbstractMapMultiSet()
        Constructor needed for subclass serialisation.
      • AbstractMapMultiSet

        protected AbstractMapMultiSet​(java.util.Map<E,​AbstractMapMultiSet.MutableInteger> map)
        Constructor that assigns the specified Map as the backing store. The map must be empty and non-null.
        Parameters:
        map - the map to assign
    • Method Detail

      • getMap

        protected java.util.Map<E,​AbstractMapMultiSet.MutableInteger> getMap()
        Utility method for implementations to access the map that backs this multiset. Not intended for interactive use outside of subclasses.
        Returns:
        the map being used by the MultiSet
      • setMap

        protected void setMap​(java.util.Map<E,​AbstractMapMultiSet.MutableInteger> map)
        Sets the map being wrapped.

        NOTE: this method should only be used during deserialization

        Parameters:
        map - the map to wrap
      • size

        public int size()
        Returns the number of elements in this multiset.
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in interface MultiSet<E>
        Overrides:
        size in class AbstractMultiSet<E>
        Returns:
        current size of the multiset
      • isEmpty

        public boolean isEmpty()
        Returns true if the underlying map is empty.
        Specified by:
        isEmpty in interface java.util.Collection<E>
        Overrides:
        isEmpty in class java.util.AbstractCollection<E>
        Returns:
        true if multiset is empty
      • getCount

        public 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.
        Specified by:
        getCount in interface MultiSet<E>
        Overrides:
        getCount in class AbstractMultiSet<E>
        Parameters:
        object - the object to search for
        Returns:
        the number of occurrences of the object, zero if not found
      • contains

        public 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.
        Specified by:
        contains in interface java.util.Collection<E>
        Overrides:
        contains in class AbstractMultiSet<E>
        Parameters:
        object - the object to search for
        Returns:
        true if the multiset contains the given element
      • iterator

        public java.util.Iterator<E> iterator()
        Gets an iterator over the multiset elements. Elements present in the MultiSet more than once will be returned repeatedly.
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in interface MultiSet<E>
        Overrides:
        iterator in class AbstractMultiSet<E>
        Returns:
        the iterator
      • add

        public int add​(E object,
                       int occurrences)
        Description copied from interface: MultiSet
        Adds a number of occurrences of the specified object to the 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.

        Specified by:
        add in interface MultiSet<E>
        Overrides:
        add in class AbstractMultiSet<E>
        Parameters:
        object - the object to add
        occurrences - the number of occurrences to add, may be zero, in which case no change is made to the multiset
        Returns:
        the number of occurrences of the object in the multiset before this operation; possibly zero
      • clear

        public void clear()
        Clears the multiset by clearing the underlying map.
        Specified by:
        clear in interface java.util.Collection<E>
        Overrides:
        clear in class AbstractMultiSet<E>
      • remove

        public int remove​(java.lang.Object object,
                          int occurrences)
        Description copied from interface: MultiSet
        Removes a number of occurrences of the specified object from the 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.

        Specified by:
        remove in interface MultiSet<E>
        Overrides:
        remove in class AbstractMultiSet<E>
        Parameters:
        object - the object to remove
        occurrences - the number of occurrences to remove, may be zero, in which case no change is made to the multiset
        Returns:
        the number of occurrences of the object in the multiset before the operation; possibly zero
      • createUniqueSetIterator

        protected java.util.Iterator<E> createUniqueSetIterator()
        Description copied from class: AbstractMultiSet
        Creates a unique set iterator. Subclasses can override this to return iterators with different properties.
        Overrides:
        createUniqueSetIterator in class AbstractMultiSet<E>
        Returns:
        the uniqueSet iterator
      • uniqueElements

        protected int uniqueElements()
        Description copied from class: AbstractMultiSet
        Returns the number of unique elements in this multiset.
        Specified by:
        uniqueElements in class AbstractMultiSet<E>
        Returns:
        the number of unique elements
      • createEntrySetIterator

        protected java.util.Iterator<MultiSet.Entry<E>> createEntrySetIterator()
        Description copied from class: AbstractMultiSet
        Creates an entry set iterator. Subclasses can override this to return iterators with different properties.
        Specified by:
        createEntrySetIterator in class AbstractMultiSet<E>
        Returns:
        the entrySet iterator
      • doWriteObject

        protected void doWriteObject​(java.io.ObjectOutputStream out)
                              throws java.io.IOException
        Write the multiset out using a custom routine.
        Overrides:
        doWriteObject in class AbstractMultiSet<E>
        Parameters:
        out - the output stream
        Throws:
        java.io.IOException - any of the usual I/O related exceptions
      • doReadObject

        protected void doReadObject​(java.io.ObjectInputStream in)
                             throws java.io.IOException,
                                    java.lang.ClassNotFoundException
        Read the multiset in using a custom routine.
        Overrides:
        doReadObject in class AbstractMultiSet<E>
        Parameters:
        in - the input stream
        Throws:
        java.io.IOException - any of the usual I/O related exceptions
        java.lang.ClassNotFoundException - if the stream contains an object which class can not be loaded
        java.lang.ClassCastException - if the stream does not contain the correct objects
      • toArray

        public java.lang.Object[] toArray()
        Returns an array of all of this multiset's elements.
        Specified by:
        toArray in interface java.util.Collection<E>
        Overrides:
        toArray in class java.util.AbstractCollection<E>
        Returns:
        an array of all of this multiset's elements
      • toArray

        public <T> T[] toArray​(T[] array)
        Returns an array of all of this multiset's elements. If the input array has more elements than are in the multiset, trailing elements will be set to null.
        Specified by:
        toArray in interface java.util.Collection<E>
        Overrides:
        toArray in class java.util.AbstractCollection<E>
        Type Parameters:
        T - the type of the array elements
        Parameters:
        array - the array to populate
        Returns:
        an array of all of this multiset's elements
        Throws:
        java.lang.ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of the elements in this list
        java.lang.NullPointerException - if the specified array is null
      • equals

        public boolean equals​(java.lang.Object object)
        Description copied from interface: MultiSet
        Compares this MultiSet to another object.

        This MultiSet equals another object if it is also a MultiSet that contains the same number of occurrences of the same elements.

        Specified by:
        equals in interface java.util.Collection<E>
        Specified by:
        equals in interface MultiSet<E>
        Overrides:
        equals in class AbstractMultiSet<E>
        Parameters:
        object - the object to compare to
        Returns:
        true if equal
      • hashCode

        public int hashCode()
        Description copied from interface: MultiSet
        Gets a hash code for the MultiSet compatible with the definition of equals. The hash code is defined as the sum total of a hash code for each element. The per element hash code is defined as (e==null ? 0 : e.hashCode()) ^ noOccurances).
        Specified by:
        hashCode in interface java.util.Collection<E>
        Specified by:
        hashCode in interface MultiSet<E>
        Overrides:
        hashCode in class AbstractMultiSet<E>
        Returns:
        the hash code of the MultiSet