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

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by com.google.common.collect.AbstractMultiset<E>
All Implemented Interfaces:
Multiset<E>, Iterable<E>, Collection<E>
Direct Known Subclasses:
ConcurrentMultiset, EnumMultiset, HashMultiset, LinkedHashMultiset, TreeMultiset

public abstract class AbstractMultiset<E>
extends AbstractCollection<E>
implements Multiset<E>

This class provides a skeletal implementation of the Multiset interface. A new multiset implementation can be created easily by extending this class and implementing the Multiset.entrySet() method, plus optionally overriding add(Object, int) and remove(Object, int) to enable modifications to the multiset.

The contains(java.lang.Object), containsAll(java.util.Collection), count(java.lang.Object), and size() implementations all iterate across the set returned by Multiset.entrySet(), as do many methods acting on the set returned by elementSet. Override those methods for better performance.

Author:
Kevin Bourrillion

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.google.common.collect.Multiset
Multiset.Entry<E>
 
Constructor Summary
AbstractMultiset()
           
 
Method Summary
 boolean add(E element)
          
 boolean add(E element, int occurrences)
          Adds a number of occurrences of the specified element to this multiset.
 boolean addAll(Collection<? extends E> elementsToAdd)
          
 void clear()
          
 boolean contains(Object element)
          
 boolean containsAll(Collection<?> elements)
          
 int count(Object element)
          Returns the number of occurrences of the specified element in this multiset.
protected  Set<E> createElementSet()
          Creates a new instance of this multiset's element set, which will be returned by elementSet.
 Set<E> elementSet()
          Returns a view of the elements of this multiset as a set.
abstract  Set<Multiset.Entry<E>> entrySet()
          Returns the data of this multiset as a set of Entry instances.
 boolean equals(Object other)
          Compares the specified object with this multiset for equality.
 int hashCode()
          Returns the hash code for this multiset.
 boolean isEmpty()
           
 Iterator<E> iterator()
          
 boolean remove(Object element)
          
 int remove(Object element, int occurrences)
          Removes a number of occurrences of the specified element from this multiset.
 boolean removeAll(Collection<?> elementsToRemove)
          
 int removeAllOccurrences(Object element)
          Removes all occurrences of the specified element from this multiset.
 boolean retainAll(Collection<?> elementsToRetain)
          
 int size()
          
 String toString()
          
 
Methods inherited from class java.util.AbstractCollection
toArray, toArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
toArray, toArray
 

Constructor Detail

AbstractMultiset

public AbstractMultiset()
Method Detail

entrySet

public abstract Set<Multiset.Entry<E>> entrySet()
Description copied from interface: Multiset
Returns the data of this multiset as a set of Entry instances. This set contains precisely one Entry instance for each distinct element of the multiset. The iteration order of this set is implementation-dependent.

Specified by:
entrySet in interface Multiset<E>
Returns:
the entries of this multiset, viewed as a set

size

public int size()

This implementation iterates across Multiset.entrySet() and sums the counts of the entries.

Specified by:
size in interface Collection<E>
Specified by:
size in class AbstractCollection<E>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<E>
Overrides:
isEmpty in class AbstractCollection<E>

contains

public boolean contains(@Nullable
                        Object element)

This implementation checks whether elementSet contains the element.

Specified by:
contains in interface Collection<E>
Overrides:
contains in class AbstractCollection<E>

iterator

public Iterator<E> iterator()

This implementation usually invokes methods of the Multiset.entrySet() iterator. As the only exception, the iterator's remove method sometimes calls the multiset's remove.

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in class AbstractCollection<E>

count

public int count(Object element)
Returns the number of occurrences of the specified element in this multiset.

This implementation iterates across Multiset.entrySet() and sums the count of all entries.

Specified by:
count in interface Multiset<E>
Parameters:
element - the element to look for
Returns:
the nonnegative number of occurrences of the element
See Also:
Iterables.frequency(java.lang.Iterable, java.lang.Object)

add

public boolean add(@Nullable
                   E element)

This implementation calls add(Object, int) with one occurrence.

Specified by:
add in interface Collection<E>
Overrides:
add in class AbstractCollection<E>

add

public boolean add(E element,
                   int occurrences)
Adds a number of occurrences of the specified element to this multiset.

This implementation always throws an UnsupportedOperationException. To support adding elements, override it.

Specified by:
add in interface Multiset<E>
Parameters:
element - the element to add
occurrences - the number of occurrences to add
Returns:
true if the collection changed as a result (this should always be the case unless occurrences is zero)

remove

public boolean remove(Object element)

This implementation calls remove(Object,int) with 1 occurrence.

Specified by:
remove in interface Collection<E>
Overrides:
remove in class AbstractCollection<E>

remove

public int remove(Object element,
                  int occurrences)
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.

This implementation always throws an UnsupportedOperationException. To support removing elements, override it.

Specified by:
remove in interface Multiset<E>
Parameters:
element - the element whose occurrences should be removed
occurrences - the number of occurrences of this element to remove
Returns:
the number of occurrences that were successfully removed (zero if the element was not present)

removeAllOccurrences

public int removeAllOccurrences(Object element)
Removes all occurrences of the specified element from this multiset. This method complements Multiset.remove(java.lang.Object, int), which removes only one occurrence at a time.

This implementation calls remove(Object, int) with Integer.MAX_VALUE occurrences.

Specified by:
removeAllOccurrences in interface Multiset<E>
Parameters:
element - the element whose occurrences should all be removed
Returns:
the number of occurrences successfully removed, possibly zero

containsAll

public boolean containsAll(Collection<?> elements)

This implementation checks whether elementSet contains the elements.

Specified by:
containsAll in interface Collection<E>
Overrides:
containsAll in class AbstractCollection<E>

addAll

public boolean addAll(Collection<? extends E> elementsToAdd)

If the collection being added is a multiset, this implementation iterates over that multiset's entry set to add the appropriate number of occurrences of each of its elements to this multiset. Otherwise, it calls AbstractCollection.addAll(java.util.Collection).

Specified by:
addAll in interface Collection<E>
Overrides:
addAll in class AbstractCollection<E>

removeAll

public boolean removeAll(Collection<?> elementsToRemove)

This implementation iterates over the elements in the collection and calls removeAllOccurrences(java.lang.Object) on each element. In some cases, this approach has better performance than AbstractCollection.removeAll(java.util.Collection).

Specified by:
removeAll in interface Collection<E>
Overrides:
removeAll in class AbstractCollection<E>

retainAll

public boolean retainAll(Collection<?> elementsToRetain)

This implementation iterates over entrySet(), checking each entry's element to see if it's contained in the provided collection. If it's not found, the remove method of the entry set's iterator is invoked. In some cases, this approach has better performance than AbstractCollection.removeAll(java.util.Collection).

Specified by:
retainAll in interface Collection<E>
Overrides:
retainAll in class AbstractCollection<E>

clear

public void clear()

This implementation calls clear on Multiset.entrySet().

Specified by:
clear in interface Collection<E>
Overrides:
clear in class AbstractCollection<E>

elementSet

public Set<E> elementSet()
Returns a view of the elements of this multiset as a set. All remove operations on the returned set "write through" to the underlying multiset, removing all occurrences of the given elements. No add operations are supported. Note that you can use elementSet().size() to find the number of distinct elements in this multiset.

The returned set's methods are implemented by calling Multiset.entrySet() methods.

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

createElementSet

protected Set<E> createElementSet()
Creates a new instance of this multiset's element set, which will be returned by elementSet.


equals

public boolean equals(@Nullable
                      Object other)
Compares the specified object with this multiset for equality. Returns true if the given object is also a multiset and contains equal elements with equal counts.

This implementation returns true if other is a multiset of the same size and if, for each element, the two multisets have the same count.

Specified by:
equals in interface Multiset<E>
Specified by:
equals in interface Collection<E>
Overrides:
equals in class Object

hashCode

public int hashCode()
Returns the hash code for this multiset. This is defined as the sum of
  (element == null ? 0 : element.hashCode()) ^ count(element)
over all elements in the multiset.

This implementation returns the hash code of Multiset.entrySet().

Specified by:
hashCode in interface Multiset<E>
Specified by:
hashCode in interface Collection<E>
Overrides:
hashCode in class Object

toString

public String toString()

This implementation returns the result of invoking toString on Multiset.entrySet().

Overrides:
toString in class AbstractCollection<E>