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

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by com.google.common.collect.AbstractMultiset<E>
          extended by com.google.common.collect.LinkedHashMultiset<E>
All Implemented Interfaces:
Multiset<E>, Serializable, Iterable<E>, Collection<E>

public final class LinkedHashMultiset<E>
extends AbstractMultiset<E>

A Multiset implementation with predictable iteration order. Its iterator orders elements according to when the first occurrence of the element was added. When the multiset contains multiple instances of an element, those instances are consecutive in the iteration order. If all occurrences of an element are removed, after which that element is added to the multiset, the element will appear at the end of the iteration.

Author:
Kevin Bourrillion, Jared Levy
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.google.common.collect.Multiset
Multiset.Entry<E>
 
Constructor Summary
LinkedHashMultiset()
          Constructs a new empty LinkedHashMultiset using the default initial capacity (16 distinct elements) and load factor (0.75).
LinkedHashMultiset(int distinctElements)
          Constructs a new empty LinkedHashMultiset with the specified expected number of distinct elements and the default load factor (0.75).
LinkedHashMultiset(Iterable<? extends E> elements)
          Constructs a new LinkedHashMultiset containing the specified elements.
 
Method Summary
 boolean add(E element, int occurrences)
          Adds a number of occurrences of an element to this multiset.
protected  Map<E,AtomicInteger> backingMap()
           
 int count(Object element)
          Returns the number of occurrences of an element in this multiset (the count of the element).
protected  Set<E> createElementSet()
          Creates a new instance of this multiset's element set, which will be returned by AbstractMultiset.elementSet.
 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.
 Iterator<E> iterator()
          
protected  void readObjectNoData()
          Don't allow default serialization.
 int remove(Object element, int occurrences)
          Conditionally removes a number of occurrences of an element from this multiset, provided that at least this many occurrences are present.
 int removeAllOccurrences(Object element)
          Removes all occurrences of the specified element from this multiset.
protected  void setBackingMap(Map<E,AtomicInteger> backingMap)
          Used during deserialization only.
 int size()
          
 
Methods inherited from class com.google.common.collect.AbstractMultiset
add, addAll, clear, contains, containsAll, elementSet, equals, hashCode, isEmpty, remove, removeAll, retainAll, 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

LinkedHashMultiset

public LinkedHashMultiset()
Constructs a new empty LinkedHashMultiset using the default initial capacity (16 distinct elements) and load factor (0.75).


LinkedHashMultiset

public LinkedHashMultiset(int distinctElements)
Constructs a new empty LinkedHashMultiset with the specified expected number of distinct elements and the default load factor (0.75).

Parameters:
distinctElements - the expected number of distinct elements
Throws:
IllegalArgumentException - if distinctElements is negative

LinkedHashMultiset

public LinkedHashMultiset(Iterable<? extends E> elements)
Constructs a new LinkedHashMultiset containing the specified elements.

Parameters:
elements - the elements that the multiset should contain
Method Detail

backingMap

protected Map<E,AtomicInteger> backingMap()

setBackingMap

protected void setBackingMap(Map<E,AtomicInteger> backingMap)
Used during deserialization only. The backing map must be empty.


entrySet

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

Invoking Multiset.Entry.getCount() on an entry in the returned set always returns the current count of that element in the multiset, as opposed to the count at the time the entry was retrieved.

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

size

public int size()
Description copied from class: AbstractMultiset

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

Specified by:
size in interface Collection<E>
Overrides:
size in class AbstractMultiset<E>

iterator

public Iterator<E> iterator()
Description copied from class: AbstractMultiset

This implementation usually invokes methods of the Multiset.entrySet() iterator. However, 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>
Overrides:
iterator in class AbstractMultiset<E>

count

public int count(@Nullable
                 Object element)
Description copied from class: AbstractMultiset
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.

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

Specified by:
count in interface Multiset<E>
Overrides:
count in class AbstractMultiset<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 boolean add(@Nullable
                   E element,
                   int occurrences)
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.

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

Specified by:
add in interface Multiset<E>
Overrides:
add in class AbstractMultiset<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 this element to add. May be zero, in which case no change will be made.
Returns:
the previous count of this element before the operation; possibly zero - TODO: make this the actual behavior!
Throws:
IllegalArgumentException - if the call would result in more than Integer.MAX_VALUE occurrences of element in this multiset.

remove

public int remove(@Nullable
                  Object element,
                  int occurrences)
Description copied from class: AbstractMultiset
Conditionally removes a number of occurrences of an element from this multiset, provided that at least this many occurrences are present. If the count of the element is less than occurrences, no change is made. Note that if occurrences == 1, this is functionally equivalent to the call remove(element).

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

Specified by:
remove in interface Multiset<E>
Overrides:
remove in class AbstractMultiset<E>
Parameters:
element - the element to conditionally remove occurrences of
occurrences - the number of occurrences of this element to remove. May be zero, in which case no change will be made.
Returns:
true if the condition for modification was met. Unless occurrences is zero, this implies that the multiset was indeed modified.

removeAllOccurrences

public int removeAllOccurrences(@Nullable
                                Object element)
Description copied from class: AbstractMultiset
Removes all occurrences of the specified element from this multiset. This method complements Multiset.remove(Object), which removes only one occurrence at a time. TODO: Nuke this. Use setCount(e, 0).

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

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

createElementSet

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

Overrides:
createElementSet in class AbstractMultiset<E>

readObjectNoData

protected void readObjectNoData()
                         throws InvalidObjectException
Don't allow default serialization.

Throws:
InvalidObjectException