|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.common.collect.ImmutableCollection<E> com.google.common.collect.ImmutableMultiset<E> com.google.common.collect.ImmutableSortedMultiset<E>
@Beta @GwtIncompatible(value="hasn\'t been tested yet") public abstract class ImmutableSortedMultiset<E>
An immutable SortedMultiset
that stores its elements in a sorted array. Some instances
are ordered by an explicit comparator, while others follow the natural sort ordering of their
elements. Either way, null elements are not supported.
Unlike Multisets.unmodifiableSortedMultiset(com.google.common.collect.SortedMultiset
, which is a view of a separate
collection that can still change, an instance of ImmutableSortedMultiset
contains its
own private data and will never change. This class is convenient for public static
final
multisets ("constant multisets") and also lets you easily make a "defensive copy" of a
set provided to your class by a caller.
The multisets returned by the headMultiset(E, com.google.common.collect.BoundType)
, tailMultiset(E, com.google.common.collect.BoundType)
, and
subMultiset(E, com.google.common.collect.BoundType, E, com.google.common.collect.BoundType)
methods share the same array as the original multiset, preventing that
array from being garbage collected. If this is a concern, the data may be copied into a
correctly-sized array by calling copyOfSorted(com.google.common.collect.SortedMultiset
.
Note on element equivalence: The ImmutableMultiset.contains(Object)
,
ImmutableMultiset.containsAll(Collection)
, and ImmutableMultiset.equals(Object)
implementations must check whether
a provided object is equivalent to an element in the collection. Unlike most collections, an
ImmutableSortedMultiset
doesn't use Object.equals(java.lang.Object)
to determine if two elements
are equivalent. Instead, with an explicit comparator, the following relation determines whether
elements x
and y
are equivalent:
{(x, y) | comparator.compare(x, y) == 0}
With natural ordering of elements, the following relation determines whether two elements are
equivalent:
{(x, y) | x.compareTo(y) == 0}
Warning: Like most multisets, an ImmutableSortedMultiset
will not function
correctly if an element is modified after being placed in the multiset. For this reason, and to
avoid general confusion, it is strongly recommended to place only immutable objects into this
collection.
Note: Although this class is not final, it cannot be subclassed as it has no public or protected constructors. Thus, instances of this type are guaranteed to be immutable.
See the Guava User Guide article on immutable collections.
Nested Class Summary | |
---|---|
static class |
ImmutableSortedMultiset.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 | ||
---|---|---|
static
|
builder()
Deprecated. Use naturalOrder() , which offers better type-safety. |
|
Comparator<? super E> |
comparator()
Returns the comparator that orders this multiset, or Ordering.natural() if the natural ordering of the elements is used. |
|
static
|
copyOf(Comparator<? super E> comparator,
Iterable<? extends E> elements)
Returns an immutable sorted multiset containing the given elements sorted by the given Comparator . |
|
static
|
copyOf(Comparator<? super E> comparator,
Iterator<? extends E> elements)
Returns an immutable sorted multiset containing the given elements sorted by the given Comparator . |
|
static
|
copyOf(E[] elements)
Returns an immutable sorted multiset containing the given elements sorted by their natural ordering. |
|
static
|
copyOf(Iterable<? extends E> elements)
Returns an immutable sorted multiset containing the given elements sorted by their natural ordering. |
|
static
|
copyOf(Iterator<? extends E> elements)
Returns an immutable sorted multiset containing the given elements sorted by their natural ordering. |
|
static
|
copyOfSorted(SortedMultiset<E> sortedMultiset)
Returns an immutable sorted multiset containing the elements of a sorted multiset, sorted by the same Comparator . |
|
ImmutableSortedMultiset<E> |
descendingMultiset()
Returns a descending view of this multiset. |
|
abstract ImmutableSortedSet<E> |
elementSet()
Returns a SortedSet view of the distinct elements in this multiset. |
|
abstract ImmutableSortedMultiset<E> |
headMultiset(E upperBound,
BoundType boundType)
Returns a view of this multiset restricted to the elements less than upperBound , optionally including upperBound itself. |
|
static
|
naturalOrder()
Returns a builder that creates immutable sorted multisets whose elements are ordered by their natural ordering. |
|
static
|
of()
Returns the empty immutable sorted multiset. |
|
static
|
of(E element)
Returns an immutable sorted multiset containing a single element. |
|
static
|
of(E e1,
E e2)
Returns an immutable sorted multiset containing the given elements sorted by their natural ordering. |
|
static
|
of(E e1,
E e2,
E e3)
Returns an immutable sorted multiset containing the given elements sorted by their natural ordering. |
|
static
|
of(E e1,
E e2,
E e3,
E e4)
Returns an immutable sorted multiset containing the given elements sorted by their natural ordering. |
|
static
|
of(E e1,
E e2,
E e3,
E e4,
E e5)
Returns an immutable sorted multiset containing the given elements sorted by their natural ordering. |
|
static
|
of(E e1,
E e2,
E e3,
E e4,
E e5,
E e6,
E... remaining)
Returns an immutable sorted multiset containing the given elements sorted by their natural ordering. |
|
static
|
orderedBy(Comparator<E> comparator)
Returns a builder that creates immutable sorted multisets with an explicit comparator. |
|
Multiset.Entry<E> |
pollFirstEntry()
Returns and removes the entry associated with the lowest element in this multiset, or returns null if this multiset is empty. |
|
Multiset.Entry<E> |
pollLastEntry()
Returns and removes the entry associated with the greatest element in this multiset, or returns null if this multiset is empty. |
|
static
|
reverseOrder()
Returns a builder that creates immutable sorted multisets whose elements are ordered by the reverse of their natural ordering. |
|
ImmutableSortedMultiset<E> |
subMultiset(E lowerBound,
BoundType lowerBoundType,
E upperBound,
BoundType upperBoundType)
Returns a view of this multiset restricted to the range between lowerBound and upperBound . |
|
abstract ImmutableSortedMultiset<E> |
tailMultiset(E lowerBound,
BoundType boundType)
Returns a view of this multiset restricted to the elements greater than lowerBound , optionally including lowerBound itself. |
Methods inherited from class com.google.common.collect.ImmutableMultiset |
---|
add, contains, containsAll, entrySet, equals, hashCode, iterator, remove, setCount, setCount, toString |
Methods inherited from class com.google.common.collect.ImmutableCollection |
---|
add, addAll, asList, clear, 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.SortedMultiset |
---|
firstEntry, iterator, lastEntry |
Methods inherited from interface com.google.common.collect.Multiset |
---|
add, add, contains, containsAll, count, entrySet, equals, hashCode, remove, remove, removeAll, retainAll, setCount, setCount, toString |
Methods inherited from interface java.util.Collection |
---|
addAll, clear, isEmpty, size, toArray, toArray |
Method Detail |
---|
public static <E> ImmutableSortedMultiset<E> of()
public static <E extends Comparable<? super E>> ImmutableSortedMultiset<E> of(E element)
public static <E extends Comparable<? super E>> ImmutableSortedMultiset<E> of(E e1, E e2)
NullPointerException
- if any element is nullpublic static <E extends Comparable<? super E>> ImmutableSortedMultiset<E> of(E e1, E e2, E e3)
NullPointerException
- if any element is nullpublic static <E extends Comparable<? super E>> ImmutableSortedMultiset<E> of(E e1, E e2, E e3, E e4)
NullPointerException
- if any element is nullpublic static <E extends Comparable<? super E>> ImmutableSortedMultiset<E> of(E e1, E e2, E e3, E e4, E e5)
NullPointerException
- if any element is nullpublic static <E extends Comparable<? super E>> ImmutableSortedMultiset<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E... remaining)
NullPointerException
- if any element is nullpublic static <E extends Comparable<? super E>> ImmutableSortedMultiset<E> copyOf(E[] elements)
NullPointerException
- if any of elements
is nullpublic static <E> ImmutableSortedMultiset<E> copyOf(Iterable<? extends E> elements)
SortedMultiset
that preserves the
comparator, call copyOfSorted(com.google.common.collect.SortedMultiset)
instead. This method iterates over elements
at
most once.
Note that if s
is a multiset<String>
, then ImmutableSortedMultiset.copyOf(s)
returns an ImmutableSortedMultiset<String>
containing each of the strings in s
, while ImmutableSortedMultiset.of(s)
returns an ImmutableSortedMultiset<multiset<String>>
containing one element (the given
multiset itself).
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.
This method is not type-safe, as it may be called on elements that are not mutually comparable.
ClassCastException
- if the elements are not mutually comparable
NullPointerException
- if any of elements
is nullpublic static <E> ImmutableSortedMultiset<E> copyOf(Iterator<? extends E> elements)
This method is not type-safe, as it may be called on elements that are not mutually comparable.
ClassCastException
- if the elements are not mutually comparable
NullPointerException
- if any of elements
is nullpublic static <E> ImmutableSortedMultiset<E> copyOf(Comparator<? super E> comparator, Iterator<? extends E> elements)
Comparator
.
NullPointerException
- if comparator
or any of elements
is nullpublic static <E> ImmutableSortedMultiset<E> copyOf(Comparator<? super E> comparator, Iterable<? extends E> elements)
Comparator
. This method iterates over elements
at most once.
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.
NullPointerException
- if comparator
or any of elements
is nullpublic static <E> ImmutableSortedMultiset<E> copyOfSorted(SortedMultiset<E> sortedMultiset)
Comparator
. That behavior differs from copyOf(Iterable)
, which
always uses the natural ordering of the elements.
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.
This method is safe to use even when sortedMultiset
is a synchronized or concurrent
collection that is currently being modified by another thread.
NullPointerException
- if sortedMultiset
or any of its elements is nullpublic final Comparator<? super E> comparator()
SortedMultiset
Ordering.natural()
if the natural ordering of the elements is used.
comparator
in interface SortedMultiset<E>
public abstract ImmutableSortedSet<E> elementSet()
SortedMultiset
SortedSet
view of the distinct elements in this multiset.
elementSet
in interface Multiset<E>
elementSet
in interface SortedMultiset<E>
public ImmutableSortedMultiset<E> descendingMultiset()
SortedMultiset
descendingMultiset
in interface SortedMultiset<E>
public final Multiset.Entry<E> pollFirstEntry()
null
if this multiset is empty.
This implementation is guaranteed to throw an UnsupportedOperationException
.
pollFirstEntry
in interface SortedMultiset<E>
UnsupportedOperationException
- alwayspublic final Multiset.Entry<E> pollLastEntry()
null
if this multiset is empty.
This implementation is guaranteed to throw an UnsupportedOperationException
.
pollLastEntry
in interface SortedMultiset<E>
UnsupportedOperationException
- alwayspublic abstract ImmutableSortedMultiset<E> headMultiset(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 ImmutableSortedMultiset<E> subMultiset(E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType)
SortedMultiset
lowerBound
and upperBound
. 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.
This method is equivalent to
tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound,
upperBoundType)
.
subMultiset
in interface SortedMultiset<E>
public abstract ImmutableSortedMultiset<E> tailMultiset(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>
public static <E> ImmutableSortedMultiset.Builder<E> orderedBy(Comparator<E> comparator)
SortedMultiset<Integer>
with a Comparator<Number>
, use the ImmutableSortedMultiset.Builder
constructor instead.
NullPointerException
- if comparator
is nullpublic static <E extends Comparable<E>> ImmutableSortedMultiset.Builder<E> reverseOrder()
Note: the type parameter E
extends Comparable<E>
rather than Comparable<? super E>
as a workaround for javac bug 6468354.
public static <E extends Comparable<E>> ImmutableSortedMultiset.Builder<E> naturalOrder()
Ordering.natural()
as the comparator. This
method provides more type-safety than builder()
, as it can be called only for classes
that implement Comparable
.
Note: the type parameter E
extends Comparable<E>
rather than Comparable<? super E>
as a workaround for javac bug 6468354.
@Deprecated public static <E> ImmutableSortedMultiset.Builder<E> builder()
naturalOrder()
, which offers better type-safety.
naturalOrder()
, which offers better
type-safety, instead. This method exists only to hide ImmutableMultiset.builder()
from
consumers of ImmutableSortedMultiset
.
UnsupportedOperationException
- always
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |