Class AbstractMapBasedMultiset<E>

  • All Implemented Interfaces:
    Multiset<E>, java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>
    Direct Known Subclasses:
    EnumMultiset, HashMultiset, LinkedHashMultiset

    @GwtCompatible(emulated=true)
    abstract class AbstractMapBasedMultiset<E>
    extends AbstractMultiset<E>
    implements java.io.Serializable
    Basic implementation of Multiset<E> backed by an instance of Map<E, Count>.

    For serialization to work, the subclass must specify explicit readObject and writeObject methods.

    • Constructor Detail

      • AbstractMapBasedMultiset

        protected AbstractMapBasedMultiset​(java.util.Map<E,​Count> backingMap)
        Standard constructor.
    • Method Detail

      • setBackingMap

        void setBackingMap​(java.util.Map<E,​Count> backingMap)
        Used during deserialization only. The backing map must be empty.
      • entrySet

        public java.util.Set<Multiset.Entry<E>> entrySet()
        Returns a view of the contents of this multiset, grouped into Multiset.Entry instances, each providing an element of the multiset and the count of that element. This set contains exactly one entry for each distinct element in the multiset (thus it always has the same size as the Multiset.elementSet()). The order of the elements in the element set is unspecified.

        The entry set is backed by the same data as the multiset, so any change to either is immediately reflected in the other. However, multiset changes may or may not be reflected in any Entry instances already retrieved from the entry set (this is implementation-dependent). Furthermore, implementations are not required to support modifications to the entry set at all, and the Entry instances themselves don't even have methods for modification. See the specific implementation class for more details on how its entry set handles modifications.

        Invoking Multiset.Entry.getCount() on an entry in the returned set always returns the current count of that element in the multiset, as opposed to the count at the time the entry was retrieved.

        Specified by:
        entrySet in interface Multiset<E>
        Overrides:
        entrySet in class AbstractMultiset<E>
        Returns:
        a set of entries representing the data of this multiset
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<E>
        Overrides:
        clear in class AbstractMultiset<E>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<E>
        Overrides:
        size in class AbstractMultiset<E>
      • iterator

        public java.util.Iterator<E> iterator()
        Description copied from interface: Multiset

        Elements that occur multiple times in the multiset will appear multiple times in this iterator, though not necessarily sequentially.

        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>
      • count

        public int count​(@Nullable
                         java.lang.Object element)
        Description copied from interface: Multiset
        Returns the number of occurrences of an element in this multiset (the count of the element). Note that for an Object.equals(java.lang.Object)-based multiset, this gives the same result as Collections.frequency(java.util.Collection<?>, java.lang.Object) (which would presumably perform more poorly).

        Note: the utility method Iterables.frequency(java.lang.Iterable<?>, java.lang.Object) generalizes this operation; it correctly delegates to this method when dealing with a multiset, but it can also accept any other iterable type.

        Specified by:
        count in interface Multiset<E>
        Overrides:
        count in class AbstractMultiset<E>
        Parameters:
        element - the element to count occurrences of
        Returns:
        the number of occurrences of the element in this multiset; possibly zero but never negative
      • add

        public int add​(@Nullable
                       E element,
                       int occurrences)
        Adds a number of occurrences of an element to this multiset. Note that if occurrences == 1, this method has the identical effect to Multiset.add(Object). This method is functionally equivalent (except in the case of overflow) to the call addAll(Collections.nCopies(element, occurrences)), which would presumably perform much more poorly.
        Specified by:
        add in interface Multiset<E>
        Overrides:
        add in class AbstractMultiset<E>
        Parameters:
        element - the element to add occurrences of; may be null only if explicitly allowed by the implementation
        occurrences - the number of occurrences of the element to add. May be zero, in which case no change will be made.
        Returns:
        the count of the element before the operation; possibly zero
        Throws:
        java.lang.IllegalArgumentException - if the call would result in more than Integer.MAX_VALUE occurrences of element in this multiset.
      • remove

        public int remove​(@Nullable
                          java.lang.Object element,
                          int occurrences)
        Description copied from interface: Multiset
        Removes a number of occurrences of the specified element from this multiset. If the multiset contains fewer than this number of occurrences to begin with, all occurrences will be removed. Note that if occurrences == 1, this is functionally equivalent to the call remove(element).
        Specified by:
        remove in interface Multiset<E>
        Overrides:
        remove in class AbstractMultiset<E>
        Parameters:
        element - the element to conditionally remove occurrences of
        occurrences - the number of occurrences of the element to remove. May be zero, in which case no change will be made.
        Returns:
        the count of the element before the operation; possibly zero
      • setCount

        public int setCount​(@Nullable
                            E element,
                            int count)
        Description copied from interface: Multiset
        Adds or removes the necessary occurrences of an element such that the element attains the desired count.
        Specified by:
        setCount in interface Multiset<E>
        Overrides:
        setCount in class AbstractMultiset<E>
        Parameters:
        element - the element to add or remove occurrences of; may be null only if explicitly allowed by the implementation
        count - the desired count of the element in this multiset
        Returns:
        the count of the element before the operation; possibly zero
      • getAndSet

        private static int getAndSet​(@Nullable
                                     Count i,
                                     int count)
      • readObjectNoData

        @GwtIncompatible
        private void readObjectNoData()
                               throws java.io.ObjectStreamException
        Throws:
        java.io.ObjectStreamException