@GwtCompatible(emulated=true) public final class TreeMultiset<E> extends AbstractSortedMultiset<E> implements java.io.Serializable
Comparator
. In all cases, this implementation uses
Comparable.compareTo(T)
or Comparator.compare(T, T)
instead of Object.equals(java.lang.Object)
to
determine equivalence of instances.
Warning: The comparison must be consistent with equals as explained by the
Comparable
class specification. Otherwise, the resulting multiset will violate the
Collection
contract, which is specified in terms of Object.equals(java.lang.Object)
.
See the Guava User Guide article on
Multiset
.
Modifier and Type | Class and Description |
---|---|
private static class |
TreeMultiset.Aggregate
A function which can be summed across a subtree.
|
private static class |
TreeMultiset.AvlNode<E> |
private static class |
TreeMultiset.Reference<T> |
AbstractMultiset.ElementSet, AbstractMultiset.EntrySet
Multiset.Entry<E>
Modifier and Type | Field and Description |
---|---|
private TreeMultiset.AvlNode<E> |
header |
private GeneralRange<E> |
range |
private TreeMultiset.Reference<TreeMultiset.AvlNode<E>> |
rootReference |
private static long |
serialVersionUID |
comparator
Constructor and Description |
---|
TreeMultiset(java.util.Comparator<? super E> comparator) |
TreeMultiset(TreeMultiset.Reference<TreeMultiset.AvlNode<E>> rootReference,
GeneralRange<E> range,
TreeMultiset.AvlNode<E> endLink) |
Modifier and Type | Method and Description |
---|---|
int |
add(E element,
int occurrences)
Adds a number of occurrences of an element to this multiset.
|
private long |
aggregateAboveRange(TreeMultiset.Aggregate aggr,
TreeMultiset.AvlNode<E> node) |
private long |
aggregateBelowRange(TreeMultiset.Aggregate aggr,
TreeMultiset.AvlNode<E> node) |
private long |
aggregateForEntries(TreeMultiset.Aggregate aggr) |
int |
count(java.lang.Object element)
Returns the number of occurrences of an element in this multiset (the
count of the element).
|
static <E extends java.lang.Comparable> |
create()
Creates a new, empty multiset, sorted according to the elements' natural order.
|
static <E> TreeMultiset<E> |
create(java.util.Comparator<? super E> comparator)
Creates a new, empty multiset, sorted according to the specified comparator.
|
static <E extends java.lang.Comparable> |
create(java.lang.Iterable<? extends E> elements)
Creates an empty multiset containing the given initial elements, sorted according to the
elements' natural order.
|
(package private) java.util.Iterator<Multiset.Entry<E>> |
descendingEntryIterator() |
(package private) int |
distinctElements() |
(package private) static int |
distinctElements(TreeMultiset.AvlNode<?> node) |
(package private) java.util.Iterator<Multiset.Entry<E>> |
entryIterator() |
private TreeMultiset.AvlNode<E> |
firstNode()
Returns the first node in the tree that is in range.
|
SortedMultiset<E> |
headMultiset(E upperBound,
BoundType boundType)
Returns a view of this multiset restricted to the elements less than
upperBound , optionally including upperBound itself. |
private TreeMultiset.AvlNode<E> |
lastNode() |
private void |
readObject(java.io.ObjectInputStream stream) |
int |
remove(java.lang.Object element,
int occurrences)
Removes a number of occurrences of the specified element from this
multiset.
|
int |
setCount(E element,
int count)
Adds or removes the necessary occurrences of an element such that the
element attains the desired count.
|
boolean |
setCount(E element,
int oldCount,
int newCount)
Conditionally sets the count of an element to a new value, as described in
Multiset.setCount(Object, int) , provided that the element has the expected
current count. |
int |
size() |
private static <T> void |
successor(TreeMultiset.AvlNode<T> a,
TreeMultiset.AvlNode<T> b) |
private static <T> void |
successor(TreeMultiset.AvlNode<T> a,
TreeMultiset.AvlNode<T> b,
TreeMultiset.AvlNode<T> c) |
SortedMultiset<E> |
tailMultiset(E lowerBound,
BoundType boundType)
Returns a view of this multiset restricted to the elements greater than
lowerBound , optionally including lowerBound itself. |
private Multiset.Entry<E> |
wrapEntry(TreeMultiset.AvlNode<E> baseEntry) |
private void |
writeObject(java.io.ObjectOutputStream stream) |
comparator, createDescendingMultiset, createElementSet, descendingIterator, descendingMultiset, elementSet, firstEntry, lastEntry, pollFirstEntry, pollLastEntry, subMultiset
add, addAll, clear, contains, createEntrySet, entrySet, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
entrySet, iterator
add, contains, containsAll, equals, hashCode, remove, removeAll, retainAll, toString
private final transient TreeMultiset.Reference<TreeMultiset.AvlNode<E>> rootReference
private final transient GeneralRange<E> range
private final transient TreeMultiset.AvlNode<E> header
@GwtIncompatible(value="not needed in emulated source") private static final long serialVersionUID
TreeMultiset(TreeMultiset.Reference<TreeMultiset.AvlNode<E>> rootReference, GeneralRange<E> range, TreeMultiset.AvlNode<E> endLink)
TreeMultiset(java.util.Comparator<? super E> comparator)
public static <E extends java.lang.Comparable> TreeMultiset<E> create()
Comparable
interface. Furthermore, all
such elements must be mutually comparable: e1.compareTo(e2)
must not throw a
ClassCastException
for any elements e1
and e2
in the multiset. If the
user attempts to add an element to the multiset that violates this constraint (for example,
the user attempts to add a string element to a set whose elements are integers), the
add(Object)
call will throw a ClassCastException
.
The type specification is <E extends Comparable>
, instead of the more specific
<E extends Comparable<? super E>>
, to support classes defined without generics.
public static <E> TreeMultiset<E> create(@Nullable java.util.Comparator<? super E> comparator)
comparator.compare(e1,
e2)
must not throw a ClassCastException
for any elements e1
and e2
in
the multiset. If the user attempts to add an element to the multiset that violates this
constraint, the add(Object)
call will throw a ClassCastException
.comparator
- the comparator that will be used to sort this multiset. A null value indicates that
the elements' natural ordering should be used.public static <E extends java.lang.Comparable> TreeMultiset<E> create(java.lang.Iterable<? extends E> elements)
This implementation is highly efficient when elements
is itself a Multiset
.
The type specification is <E extends Comparable>
, instead of the more specific
<E extends Comparable<? super E>>
, to support classes defined without generics.
private long aggregateForEntries(TreeMultiset.Aggregate aggr)
private long aggregateBelowRange(TreeMultiset.Aggregate aggr, @Nullable TreeMultiset.AvlNode<E> node)
private long aggregateAboveRange(TreeMultiset.Aggregate aggr, @Nullable TreeMultiset.AvlNode<E> node)
public int size()
size
in interface java.util.Collection<E>
size
in class AbstractMultiset<E>
int distinctElements()
distinctElements
in class AbstractMultiset<E>
public int count(@Nullable java.lang.Object element)
Multiset
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.
public int add(@Nullable E element, int occurrences)
Multiset
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.add
in interface Multiset<E>
add
in class AbstractMultiset<E>
element
- the element to add occurrences of; may be null only if
explicitly allowed by the implementationoccurrences
- the number of occurrences of the element to add. May be
zero, in which case no change will be made.public int remove(@Nullable java.lang.Object element, int occurrences)
Multiset
occurrences == 1
, this is functionally equivalent to the call
remove(element)
.remove
in interface Multiset<E>
remove
in class AbstractMultiset<E>
element
- the element to conditionally remove occurrences ofoccurrences
- the number of occurrences of the element to remove. May
be zero, in which case no change will be made.public int setCount(@Nullable E element, int count)
Multiset
setCount
in interface Multiset<E>
setCount
in class AbstractMultiset<E>
element
- the element to add or remove occurrences of; may be null
only if explicitly allowed by the implementationcount
- the desired count of the element in this multisetpublic boolean setCount(@Nullable E element, int oldCount, int newCount)
Multiset
Multiset.setCount(Object, int)
, provided that the element has the expected
current count. If the current count is not oldCount
, no change is
made.setCount
in interface Multiset<E>
setCount
in class AbstractMultiset<E>
element
- the element to conditionally set the count of; may be null
only if explicitly allowed by the implementationoldCount
- the expected present count of the element in this multisetnewCount
- the desired count of the element in this multisettrue
if the condition for modification was met. This
implies that the multiset was indeed modified, unless
oldCount == newCount
.private Multiset.Entry<E> wrapEntry(TreeMultiset.AvlNode<E> baseEntry)
@Nullable private TreeMultiset.AvlNode<E> firstNode()
@Nullable private TreeMultiset.AvlNode<E> lastNode()
java.util.Iterator<Multiset.Entry<E>> entryIterator()
entryIterator
in class AbstractMultiset<E>
java.util.Iterator<Multiset.Entry<E>> descendingEntryIterator()
descendingEntryIterator
in class AbstractSortedMultiset<E>
public SortedMultiset<E> headMultiset(@Nullable E upperBound, BoundType boundType)
SortedMultiset
upperBound
, optionally including upperBound
itself. The
returned multiset is a view of this multiset, so changes to one will be
reflected in the other. The returned multiset supports all operations that
this multiset supports.
The returned multiset will throw an IllegalArgumentException
on
attempts to add elements outside its range.
headMultiset
in interface SortedMultiset<E>
public SortedMultiset<E> tailMultiset(@Nullable E lowerBound, BoundType boundType)
SortedMultiset
lowerBound
, optionally including lowerBound
itself. The
returned multiset is a view of this multiset, so changes to one will be
reflected in the other. The returned multiset supports all operations that
this multiset supports.
The returned multiset will throw an IllegalArgumentException
on
attempts to add elements outside its range.
tailMultiset
in interface SortedMultiset<E>
static int distinctElements(@Nullable TreeMultiset.AvlNode<?> node)
private static <T> void successor(TreeMultiset.AvlNode<T> a, TreeMultiset.AvlNode<T> b)
private static <T> void successor(TreeMultiset.AvlNode<T> a, TreeMultiset.AvlNode<T> b, TreeMultiset.AvlNode<T> c)
@GwtIncompatible(value="java.io.ObjectOutputStream") private void writeObject(java.io.ObjectOutputStream stream) throws java.io.IOException
java.io.IOException
@GwtIncompatible(value="java.io.ObjectInputStream") private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, java.lang.ClassNotFoundException
java.io.IOException
java.lang.ClassNotFoundException