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

java.lang.Object
  extended by com.google.common.collect.ForwardingObject
      extended by com.google.common.collect.ForwardingCollection<E>
          extended by com.google.common.collect.ForwardingMultiset<E>
All Implemented Interfaces:
Multiset<E>, Serializable, Iterable<E>, Collection<E>
Direct Known Subclasses:
LinkedListMultiset

public abstract class ForwardingMultiset<E>
extends ForwardingCollection<E>
implements Multiset<E>

A multiset which forwards all its method calls to another multiset. Subclasses should override one or more methods to change or add behavior of the backing multiset as desired per the decorator pattern.

Author:
Kevin Bourrillion
See Also:
ForwardingObject, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.google.common.collect.Multiset
Multiset.Entry<E>
 
Constructor Summary
protected ForwardingMultiset(Multiset<E> delegate)
           
 
Method Summary
 boolean add(E element, int occurrences)
          Adds a number of occurrences of the specified element to this multiset.
 int count(Object element)
          Returns the number of occurrences of the specified element in this multiset.
protected  Multiset<E> delegate()
          Returns the backing delegate object.
 Set<E> elementSet()
          Returns a view of the elements of this multiset as a set.
 Set<Multiset.Entry<E>> entrySet()
          Returns the data of this multiset as a set of Entry instances.
 boolean equals(Object obj)
          Compares the specified object with this multiset for equality.
 int hashCode()
          Returns the hash code for this multiset.
 int remove(Object element, int occurrences)
          Removes a number of occurrences of the specified element from this multiset.
 int removeAllOccurrences(Object element)
          Removes all occurrences of the specified element from this multiset.
 
Methods inherited from class com.google.common.collect.ForwardingCollection
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 
Methods inherited from class com.google.common.collect.ForwardingObject
toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 

Constructor Detail

ForwardingMultiset

protected ForwardingMultiset(Multiset<E> delegate)
Method Detail

delegate

protected Multiset<E> delegate()
Description copied from class: ForwardingObject
Returns the backing delegate object. This method should be overridden to specify the correct return type. For example:
  @SuppressWarnings("unchecked")
  @Override protected Foo delegate() {
    return (Foo) super.delegate();
  }
This method should always return the same delegate instance that was passed to the constructor.

Overrides:
delegate in class ForwardingCollection<E>

count

public int count(Object element)
Description copied from interface: Multiset
Returns the number of occurrences of the specified element in this multiset.

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(E element,
                   int occurrences)
Description copied from interface: Multiset
Adds a number of occurrences of the specified element to this multiset.

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 int remove(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.

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)
Description copied from interface: Multiset
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.

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

elementSet

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

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

entrySet

public 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

equals

public boolean equals(Object obj)
Description copied from interface: Multiset
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.

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

hashCode

public int hashCode()
Description copied from interface: Multiset
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.

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