Class EnumMultiset<E extends java.lang.Enum<E>>

  • All Implemented Interfaces:
    Multiset<E>, java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>

    @GwtCompatible(emulated=true)
    public final class EnumMultiset<E extends java.lang.Enum<E>>
    extends AbstractMultiset<E>
    implements java.io.Serializable
    Multiset implementation specialized for enum elements, supporting all single-element operations in O(1).

    See the Guava User Guide article on Multiset.

    Since:
    2.0
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private EnumMultiset​(java.lang.Class<E> type)
      Creates an empty EnumMultiset.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int add​(E element, int occurrences)
      Adds a number of occurrences of an element to this multiset.
      (package private) void checkIsE​(java.lang.Object element)
      Returns element cast to E, if it actually is a nonnull E.
      void clear()  
      int count​(java.lang.Object element)
      Returns the number of occurrences of an element in this multiset (the count of the element).
      static <E extends java.lang.Enum<E>>
      EnumMultiset<E>
      create​(java.lang.Class<E> type)
      Creates an empty EnumMultiset.
      static <E extends java.lang.Enum<E>>
      EnumMultiset<E>
      create​(java.lang.Iterable<E> elements)
      Creates a new EnumMultiset containing the specified elements.
      static <E extends java.lang.Enum<E>>
      EnumMultiset<E>
      create​(java.lang.Iterable<E> elements, java.lang.Class<E> type)
      Returns a new EnumMultiset instance containing the given elements.
      (package private) int distinctElements()  
      (package private) java.util.Iterator<E> elementIterator()  
      (package private) java.util.Iterator<Multiset.Entry<E>> entryIterator()  
      void forEachEntry​(java.util.function.ObjIntConsumer<? super E> action)
      Runs the specified action for each distinct element in this multiset, and the number of occurrences of that element.
      private boolean isActuallyE​(java.lang.Object o)  
      java.util.Iterator<E> iterator()
      private void readObject​(java.io.ObjectInputStream stream)  
      int remove​(java.lang.Object element, int occurrences)
      Removes a number of occurrences of the specified element from this multiset.
      int setCount​(E element, int count)
      Adds or removes the necessary occurrences of an element such that the element attains the desired count.
      int size()
      Returns the total number of all occurrences of all elements in this multiset.
      private void writeObject​(java.io.ObjectOutputStream stream)  
      • Methods inherited from class java.util.AbstractCollection

        containsAll, 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, stream, toArray, toArray, toArray
    • Field Detail

      • type

        private transient java.lang.Class<E extends java.lang.Enum<E>> type
      • enumConstants

        private transient E extends java.lang.Enum<E>[] enumConstants
      • counts

        private transient int[] counts
      • distinctElements

        private transient int distinctElements
      • size

        private transient long size
    • Constructor Detail

      • EnumMultiset

        private EnumMultiset​(java.lang.Class<E> type)
        Creates an empty EnumMultiset.
    • Method Detail

      • create

        public static <E extends java.lang.Enum<E>> EnumMultiset<E> create​(java.lang.Class<E> type)
        Creates an empty EnumMultiset.
      • create

        public static <E extends java.lang.Enum<E>> EnumMultiset<E> create​(java.lang.Iterable<E> elements)
        Creates a new EnumMultiset containing the specified elements.

        This implementation is highly efficient when elements is itself a Multiset.

        Parameters:
        elements - the elements that the multiset should contain
        Throws:
        java.lang.IllegalArgumentException - if elements is empty
      • create

        public static <E extends java.lang.Enum<E>> EnumMultiset<E> create​(java.lang.Iterable<E> elements,
                                                                           java.lang.Class<E> type)
        Returns a new EnumMultiset instance containing the given elements. Unlike create(Iterable), this method does not produce an exception on an empty iterable.
        Since:
        14.0
      • isActuallyE

        private boolean isActuallyE​(java.lang.Object o)
      • checkIsE

        void checkIsE​(java.lang.Object element)
        Returns element cast to E, if it actually is a nonnull E. Otherwise, throws either a NullPointerException or a ClassCastException as appropriate.
      • size

        public int size()
        Description copied from interface: Multiset
        Returns the total number of all occurrences of all elements in this multiset.

        Note: this method does not return the number of distinct elements in the multiset, which is given by entrySet().size().

        Specified by:
        size in interface java.util.Collection<E extends java.lang.Enum<E>>
        Specified by:
        size in interface Multiset<E extends java.lang.Enum<E>>
        Specified by:
        size in class java.util.AbstractCollection<E extends java.lang.Enum<E>>
      • count

        public int count​(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 extends java.lang.Enum<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​(E element,
                       int occurrences)
        Description copied from interface: Multiset
        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 extends java.lang.Enum<E>>
        Overrides:
        add in class AbstractMultiset<E extends java.lang.Enum<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
      • remove

        public int remove​(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 extends java.lang.Enum<E>>
        Overrides:
        remove in class AbstractMultiset<E extends java.lang.Enum<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​(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 extends java.lang.Enum<E>>
        Overrides:
        setCount in class AbstractMultiset<E extends java.lang.Enum<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
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<E extends java.lang.Enum<E>>
        Specified by:
        clear in class AbstractMultiset<E extends java.lang.Enum<E>>
      • forEachEntry

        public void forEachEntry​(java.util.function.ObjIntConsumer<? super E> action)
        Description copied from interface: Multiset
        Runs the specified action for each distinct element in this multiset, and the number of occurrences of that element. For some Multiset implementations, this may be more efficient than iterating over the Multiset.entrySet() either explicitly or with entrySet().forEach(action).
        Specified by:
        forEachEntry in interface Multiset<E extends java.lang.Enum<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 extends java.lang.Enum<E>>
        Specified by:
        iterator in interface java.lang.Iterable<E extends java.lang.Enum<E>>
        Specified by:
        iterator in interface Multiset<E extends java.lang.Enum<E>>
        Specified by:
        iterator in class java.util.AbstractCollection<E extends java.lang.Enum<E>>
      • writeObject

        @GwtIncompatible
        private void writeObject​(java.io.ObjectOutputStream stream)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • readObject

        @GwtIncompatible
        private void readObject​(java.io.ObjectInputStream stream)
                         throws java.io.IOException,
                                java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException