com.google.common.collect
Class ImmutableMultiset<E>

java.lang.Object
  extended by com.google.common.collect.ImmutableCollection<E>
      extended by com.google.common.collect.ImmutableMultiset<E>
All Implemented Interfaces:
Multiset<E>, Serializable, Iterable<E>, Collection<E>

@GwtCompatible(serializable=true,
               emulated=true)
public class ImmutableMultiset<E>
extends ImmutableCollection<E>
implements Multiset<E>

An immutable hash-based multiset. Does not permit null elements.

Its iterator orders elements according to the first appearance of the element among the items passed to the factory method or builder. When the multiset contains multiple instances of an element, those instances are consecutive in the iteration order.

Since:
2 (imported from Google Collections Library)
Author:
Jared Levy
See Also:
Serialized Form

Nested Class Summary
static class ImmutableMultiset.Builder<E>
          A builder for creating immutable multiset instances, especially public static final multisets ("constant multisets").
 
Nested classes/interfaces inherited from interface com.google.common.collect.Multiset
Multiset.Entry<E>
 
Method Summary
 int add(E element, int occurrences)
          Guaranteed to throw an exception and leave the collection unmodified.
static
<E> ImmutableMultiset.Builder<E>
builder()
          Returns a new builder.
 boolean contains(Object element)
          Returns true if this collection contains the specified element.
static
<E> ImmutableMultiset<E>
copyOf(E[] elements)
          Returns an immutable multiset containing the given elements.
static
<E> ImmutableMultiset<E>
copyOf(Iterable<? extends E> elements)
          Returns an immutable multiset containing the given elements.
static
<E> ImmutableMultiset<E>
copyOf(Iterator<? extends E> elements)
          Returns an immutable multiset containing the given elements.
 int count(Object element)
          Returns the number of occurrences of an element in this multiset (the count of the element).
 Set<E> elementSet()
          Returns the set of distinct elements contained in this multiset.
 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.
 boolean equals(Object object)
          Indicates whether some other object is "equal to" this one.
 int hashCode()
          Returns a hash code value for the object.
 UnmodifiableIterator<E> iterator()
          Returns an unmodifiable iterator across the elements in this collection.
static
<E> ImmutableMultiset<E>
of()
          Returns the empty immutable multiset.
static
<E> ImmutableMultiset<E>
of(E element)
          Returns an immutable multiset containing a single element.
static
<E> ImmutableMultiset<E>
of(E[] elements)
          Deprecated. use copyOf(Object[]). This method is scheduled for deletion in January 2012.
static
<E> ImmutableMultiset<E>
of(E e1, E e2)
          Returns an immutable multiset containing the given elements, in order.
static
<E> ImmutableMultiset<E>
of(E e1, E e2, E e3)
          Returns an immutable multiset containing the given elements, in order.
static
<E> ImmutableMultiset<E>
of(E e1, E e2, E e3, E e4)
          Returns an immutable multiset containing the given elements, in order.
static
<E> ImmutableMultiset<E>
of(E e1, E e2, E e3, E e4, E e5)
          Returns an immutable multiset containing the given elements, in order.
static
<E> ImmutableMultiset<E>
of(E e1, E e2, E e3, E e4, E e5, E e6, E... others)
          Returns an immutable multiset containing the given elements, in order.
 int remove(Object element, int occurrences)
          Guaranteed to throw an exception and leave the collection unmodified.
 int setCount(E element, int count)
          Guaranteed to throw an exception and leave the collection unmodified.
 boolean setCount(E element, int oldCount, int newCount)
          Guaranteed to throw an exception and leave the collection unmodified.
 int size()
          Returns the number of elements in this collection.
 String toString()
          Returns a string representation of the object.
 
Methods inherited from class com.google.common.collect.ImmutableCollection
add, addAll, asList, clear, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.google.common.collect.Multiset
add, containsAll, remove, removeAll, retainAll
 
Methods inherited from interface java.util.Collection
addAll, clear, isEmpty, toArray, toArray
 

Method Detail

of

public static <E> ImmutableMultiset<E> of()
Returns the empty immutable multiset.


of

public static <E> ImmutableMultiset<E> of(E element)
Returns an immutable multiset containing a single element.

Throws:
NullPointerException - if element is null
Since:
6 (source-compatible since release 2)

of

public static <E> ImmutableMultiset<E> of(E e1,
                                          E e2)
Returns an immutable multiset containing the given elements, in order.

Throws:
NullPointerException - if any element is null
Since:
6 (source-compatible since release 2)

of

public static <E> ImmutableMultiset<E> of(E e1,
                                          E e2,
                                          E e3)
Returns an immutable multiset containing the given elements, in order.

Throws:
NullPointerException - if any element is null
Since:
6 (source-compatible since release 2)

of

public static <E> ImmutableMultiset<E> of(E e1,
                                          E e2,
                                          E e3,
                                          E e4)
Returns an immutable multiset containing the given elements, in order.

Throws:
NullPointerException - if any element is null
Since:
6 (source-compatible since release 2)

of

public static <E> ImmutableMultiset<E> of(E e1,
                                          E e2,
                                          E e3,
                                          E e4,
                                          E e5)
Returns an immutable multiset containing the given elements, in order.

Throws:
NullPointerException - if any element is null
Since:
6 (source-compatible since release 2)

of

public static <E> ImmutableMultiset<E> of(E e1,
                                          E e2,
                                          E e3,
                                          E e4,
                                          E e5,
                                          E e6,
                                          E... others)
Returns an immutable multiset containing the given elements, in order.

Throws:
NullPointerException - if any element is null
Since:
6 (source-compatible since release 2)

of

@Deprecated
public static <E> ImmutableMultiset<E> of(E[] elements)
Deprecated. use copyOf(Object[]). This method is scheduled for deletion in January 2012.

Returns an immutable multiset containing the given elements.

The multiset is ordered by the first occurrence of each element. For example, ImmutableMultiset.of(2, 3, 1, 3) yields a multiset with elements in the order 2, 3, 3, 1.

Throws:
NullPointerException - if any of elements is null
Since:
2 (changed from varargs in release 6)

copyOf

public static <E> ImmutableMultiset<E> copyOf(E[] elements)
Returns an immutable multiset containing the given elements.

The multiset is ordered by the first occurrence of each element. For example, ImmutableMultiset.copyOf([2, 3, 1, 3]) yields a multiset with elements in the order 2, 3, 3, 1.

Throws:
NullPointerException - if any of elements is null
Since:
6

copyOf

public static <E> ImmutableMultiset<E> copyOf(Iterable<? extends E> elements)
Returns an immutable multiset containing the given elements.

The multiset is ordered by the first occurrence of each element. For example, ImmutableMultiset.copyOf(Arrays.asList(2, 3, 1, 3)) yields a multiset with elements in the order 2, 3, 3, 1.

Despite the method name, this method attempts to avoid actually copying the data when it is safe to do so. The exact circumstances under which a copy will or will not be performed are undocumented and subject to change.

Note: Despite what the method name suggests, if elements is an ImmutableMultiset, no copy will actually be performed, and the given multiset itself will be returned.

Throws:
NullPointerException - if any of elements is null

copyOf

public static <E> ImmutableMultiset<E> copyOf(Iterator<? extends E> elements)
Returns an immutable multiset containing the given elements.

The multiset is ordered by the first occurrence of each element. For example, ImmutableMultiset.copyOf(Arrays.asList(2, 3, 1, 3).iterator()) yields a multiset with elements in the order 2, 3, 3, 1.

Throws:
NullPointerException - if any of elements is null

count

public int count(@Nullable
                 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>
Parameters:
element - the element to count occurrences of
Returns:
the number of occurrences of the element in this multiset; possibly zero but never negative

iterator

public UnmodifiableIterator<E> iterator()
Description copied from class: ImmutableCollection
Returns an unmodifiable iterator across the elements in this collection.

Specified by:
iterator in interface Multiset<E>
Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in class ImmutableCollection<E>
Returns:
an Iterator over the elements in this collection

size

public int size()
Description copied from interface: java.util.Collection
Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Specified by:
size in interface Collection<E>
Returns:
the number of elements in this collection

contains

public boolean contains(@Nullable
                        Object element)
Description copied from interface: java.util.Collection
Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).

Specified by:
contains in interface Multiset<E>
Specified by:
contains in interface Collection<E>
Overrides:
contains in class ImmutableCollection<E>
Parameters:
element - element whose presence in this collection is to be tested
Returns:
true if this collection contains the specified element

add

public int add(E element,
               int occurrences)
Guaranteed to throw an exception and leave the collection unmodified.

Specified by:
add in interface Multiset<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:
UnsupportedOperationException - always

remove

public int remove(Object element,
                  int occurrences)
Guaranteed to throw an exception and leave the collection unmodified.

Specified by:
remove in interface Multiset<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
Throws:
UnsupportedOperationException - always

setCount

public int setCount(E element,
                    int count)
Guaranteed to throw an exception and leave the collection unmodified.

Specified by:
setCount in interface Multiset<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
Throws:
UnsupportedOperationException - always

setCount

public boolean setCount(E element,
                        int oldCount,
                        int newCount)
Guaranteed to throw an exception and leave the collection unmodified.

Specified by:
setCount in interface Multiset<E>
Parameters:
element - the element to conditionally set the count of; may be null only if explicitly allowed by the implementation
oldCount - the expected present count of the element in this multiset
newCount - the desired count of the element in this multiset
Returns:
true if the condition for modification was met. This implies that the multiset was indeed modified, unless oldCount == newCount.
Throws:
UnsupportedOperationException - always

equals

public boolean equals(@Nullable
                      Object object)
Description copied from class: java.lang.Object
Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Specified by:
equals in interface Multiset<E>
Specified by:
equals in interface Collection<E>
Overrides:
equals in class Object
Parameters:
object - the reference object with which to compare.
Returns:
true if this object is the same as the obj argument; false otherwise.
See Also:
Object.hashCode(), Hashtable

hashCode

public int hashCode()
Description copied from class: java.lang.Object
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

The general contract of hashCode is:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

Specified by:
hashCode in interface Multiset<E>
Specified by:
hashCode in interface Collection<E>
Overrides:
hashCode in class Object
Returns:
a hash code value for this object.
See Also:
Object.equals(java.lang.Object), Hashtable

toString

public String toString()
Description copied from class: java.lang.Object
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Specified by:
toString in interface Multiset<E>
Overrides:
toString in class ImmutableCollection<E>
Returns:
a string representation of the object.

elementSet

public Set<E> elementSet()
Description copied from interface: Multiset
Returns the set of distinct elements contained in this multiset. The element set is backed by the same data as the multiset, so any change to either is immediately reflected in the other. The order of the elements in the element set is unspecified.

If the element set supports any removal operations, these necessarily cause all occurrences of the removed element(s) to be removed from the multiset. Implementations are not expected to support the add operations, although this is possible.

A common use for the element set is to find the number of distinct elements in the multiset: elementSet().size().

Specified by:
elementSet in interface Multiset<E>
Returns:
a view of the set of distinct elements in this multiset

entrySet

public Set<Multiset.Entry<E>> entrySet()
Description copied from interface: Multiset
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.

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

builder

public static <E> ImmutableMultiset.Builder<E> builder()
Returns a new builder. The generated builder is equivalent to the builder created by the ImmutableMultiset.Builder constructor.