Top Description Inners Fields Constructors Methods
com.google.common.collect

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

extends AbstractMultiset<E extends Enum<E>>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@GwtCompatible
emulated:true
@J2ktIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, com.google.common.collect.CollectPreconditions.checkNonnegative, .CollectPreconditions.checkRemove

Multiset implementation specialized for enum elements, supporting all single-element operations in O(1).

See the Guava User Guide article on Multiset.

Author
Jared Levy
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv abstract class

Field Summary

Modifier and TypeField and Description
private transient int[]
private transient int
private transient E[]
private static final long
private transient long
private transient Class<E>

Constructor Summary

AccessConstructor and Description
private
EnumMultiset(Class<E> type)

Creates an empty EnumMultiset.

Method Summary

Modifier and TypeMethod and Description
public int
add(E
the element to add occurrences of; may be null only if explicitly allowed by the implementation
element
,
int
the number of occurrences of the element to add. May be zero, in which case no change will be made.
occurrences
)

Overrides com.google.common.collect.AbstractMultiset.add.

Implements com.google.common.collect.Multiset.add.

Adds a number of occurrences of an element to this multiset.
private void
checkIsE(Object element)

Returns element cast to E, if it actually is a nonnull E.

public void
clear()

Implements abstract com.google.common.collect.AbstractMultiset.clear.

Implements java.util.Collection.clear.

Removes all of the elements from this collection (optional operation).
public int
count(Object
the element to count occurrences of
element
)

Implements com.google.common.collect.Multiset.count.

Returns the number of occurrences of an element in this multiset (the count of the element).
public static <E extends Enum<E>> EnumMultiset<E>
create(Class<E> type)

Creates an empty EnumMultiset.

public static <E extends Enum<E>> EnumMultiset<E>
create(Iterable<E>
the elements that the multiset should contain
elements
)

Creates a new EnumMultiset containing the specified elements.

public static <E extends Enum<E>> EnumMultiset<E>
create(Iterable<E> elements, Class<E> type)

Returns a new EnumMultiset instance containing the given elements.

pack-priv int
pack-priv Iterator<E>
pack-priv Iterator<Multiset.Entry<E>>
public void
forEachEntry(ObjIntConsumer<? super E> action)

Overrides default com.google.common.collect.Multiset.forEachEntry.

Runs the specified action for each distinct element in this multiset, and the number of occurrences of that element.
private boolean
public Iterator<E>
iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements com.google.common.collect.Multiset.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this collection.
private void
public int
remove(Object
the element to conditionally remove occurrences of
element
,
int
the number of occurrences of the element to remove. May be zero, in which case no change will be made.
occurrences
)

Overrides com.google.common.collect.AbstractMultiset.remove.

Implements com.google.common.collect.Multiset.remove.

Removes a number of occurrences of the specified element from this multiset.
public int
setCount(E
the element to add or remove occurrences of; may be null only if explicitly allowed by the implementation
element
,
int
the desired count of the element in this multiset
count
)

Overrides com.google.common.collect.AbstractMultiset.setCount.

Implements com.google.common.collect.Multiset.setCount.

Adds or removes the necessary occurrences of an element such that the element attains the desired count.
public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements com.google.common.collect.Multiset.size, java.util.Collection.size.

Returns the total number of all occurrences of all elements in this multiset.
private void
Inherited from com.google.common.collect.AbstractMultiset:
addaddAllcontainscreateElementSetcreateEntrySetelementSetentrySetequalshashCodeisEmptyremoveremoveAllretainAllsetCounttoString

Field Detail

countsback to summary
private transient int[] counts
distinctElementsback to summary
private transient int distinctElements
enumConstantsback to summary
private transient E[] enumConstants
serialVersionUIDback to summary
private static final long serialVersionUID
Annotations
@GwtIncompatible
sizeback to summary
private transient long size
typeback to summary
private transient Class<E> type

Constructor Detail

EnumMultisetback to summary
private EnumMultiset(Class<E> type)

Creates an empty EnumMultiset.

Method Detail

addback to summary
public int add(E element, int occurrences)

Overrides com.google.common.collect.AbstractMultiset.add.

Implements com.google.common.collect.Multiset.add.

Doc from com.google.common.collect.Multiset.add.

Adds a number of occurrences of an element to this multiset. Note that if occurrences == 1, this method has the identical effect to 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.

Parameters
element:E

the element to add occurrences of; may be null only if explicitly allowed by the implementation

occurrences:int

the number of occurrences of the element to add. May be zero, in which case no change will be made.

Returns:int

the count of the element before the operation; possibly zero

Annotations
@CanIgnoreReturnValue
@Override
checkIsEback to summary
private void checkIsE(Object element)

Returns element cast to E, if it actually is a nonnull E. Otherwise, throws either a NullPointerException or a ClassCastException as appropriate.

clearback to summary
public void clear()

Implements abstract com.google.common.collect.AbstractMultiset.clear.

Implements java.util.Collection.clear.

Doc from java.util.Collection.clear.

Removes all of the elements from this collection (optional operation). The collection will be empty after this method returns.

Annotations
@Override
countback to summary
public int count(Object element)

Implements com.google.common.collect.Multiset.count.

Doc from com.google.common.collect.Multiset.count.

Returns the number of occurrences of an element in this multiset (the count of the element). Note that for an Object#equals-based multiset, this gives the same result as Collections#frequency (which would presumably perform more poorly).

Note

the utility method Iterables#frequency generalizes this operation; it correctly delegates to this method when dealing with a multiset, but it can also accept any other iterable type.

Parameters
element:Object

the element to count occurrences of

Returns:int

the number of occurrences of the element in this multiset; possibly zero but never negative

Annotations
@Override
createback to summary
public static <E extends Enum<E>> EnumMultiset<E> create(Class<E> type)

Creates an empty EnumMultiset.

createback to summary
public static <E extends Enum<E>> EnumMultiset<E> create(Iterable<E> elements)

Creates a new EnumMultiset containing the specified elements.

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

Parameters
elements:Iterable<E>

the elements that the multiset should contain

Exceptions
IllegalArgumentException:
if elements is empty
createback to summary
public static <E extends Enum<E>> EnumMultiset<E> create(Iterable<E> elements, Class<E> type)

Returns a new EnumMultiset instance containing the given elements. Unlike EnumMultiset#create(Iterable), this method does not produce an exception on an empty iterable.

Since
14.0
distinctElementsback to summary
pack-priv int distinctElements()

Implements abstract com.google.common.collect.AbstractMultiset.distinctElements.

Annotations
@Override
elementIteratorback to summary
pack-priv Iterator<E> elementIterator()

Implements abstract com.google.common.collect.AbstractMultiset.elementIterator.

Annotations
@Override
entryIteratorback to summary
pack-priv Iterator<Multiset.Entry<E>> entryIterator()

Implements abstract com.google.common.collect.AbstractMultiset.entryIterator.

Annotations
@Override
forEachEntryback to summary
public void forEachEntry(ObjIntConsumer<? super E> action)

Overrides default com.google.common.collect.Multiset.forEachEntry.

Doc from com.google.common.collect.Multiset.forEachEntry.

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 entrySet() either explicitly or with entrySet().forEach(action).

Annotations
@Override
isActuallyEback to summary
private boolean isActuallyE(Object o)
iteratorback to summary
public Iterator<E> iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements com.google.common.collect.Multiset.iterator, java.util.Collection.iterator.

Doc from com.google.common.collect.Multiset.iterator.

Returns an iterator over the elements in this collection. There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).

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

Returns:Iterator<E>

Doc from java.util.Collection.iterator.

an Iterator over the elements in this collection

Annotations
@Override
readObjectback to summary
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
Annotations
@GwtIncompatible
Serial data
the Class<E> for the enum type, the number of distinct elements, the first element, its count, the second element, its count, and so on
removeback to summary
public int remove(Object element, int occurrences)

Overrides com.google.common.collect.AbstractMultiset.remove.

Implements com.google.common.collect.Multiset.remove.

Doc from com.google.common.collect.Multiset.remove.

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).

Parameters
element:Object

the element to conditionally remove occurrences of

occurrences:int

the number of occurrences of the element to remove. May be zero, in which case no change will be made.

Returns:int

the count of the element before the operation; possibly zero

Annotations
@CanIgnoreReturnValue
@Override
setCountback to summary
public int setCount(E element, int count)

Overrides com.google.common.collect.AbstractMultiset.setCount.

Implements com.google.common.collect.Multiset.setCount.

Doc from com.google.common.collect.Multiset.setCount.

Adds or removes the necessary occurrences of an element such that the element attains the desired count.

Parameters
element:E

the element to add or remove occurrences of; may be null only if explicitly allowed by the implementation

count:int

the desired count of the element in this multiset

Returns:int

the count of the element before the operation; possibly zero

Annotations
@CanIgnoreReturnValue
@Override
sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements com.google.common.collect.Multiset.size, java.util.Collection.size.

Doc from com.google.common.collect.Multiset.size.

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().

Returns:int

Doc from java.util.Collection.size.

the number of elements in this collection

Annotations
@Override
writeObjectback to summary
private void writeObject(ObjectOutputStream stream) throws IOException
Annotations
@GwtIncompatible
com.google.common.collect back to summary

pack-priv abstract Class EnumMultiset.Itr<T>

extends Object
implements Iterator<T>
Class Inheritance
All Implemented Interfaces
java.util.Iterator

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv int

Constructor Summary

AccessConstructor and Description
pack-priv
Itr()

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.
public T
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.
pack-priv abstract T
output(int index)

public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

indexback to summary
pack-priv int index
toRemoveback to summary
pack-priv int toRemove

Constructor Detail

Itrback to summary
pack-priv Itr()

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

Annotations
@Override
nextback to summary
public T next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:T

the next element in the iteration

Annotations
@Override
outputback to summary
pack-priv abstract T output(int index)
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

Annotations
@Override