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

java.lang.Object
  extended by com.google.common.collect.ImmutableCollection<E>
      extended by com.google.common.collect.ImmutableSet<E>
          extended by com.google.common.collect.ImmutableSortedSet<E>
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Set<E>, SortedSet<E>

@GwtCompatible(serializable=true,
               emulated=true)
public abstract class ImmutableSortedSet<E>
extends ImmutableSet<E>
implements SortedSet<E>

An immutable SortedSet 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 Collections.unmodifiableSortedSet(java.util.SortedSet), which is a view of a separate collection that can still change, an instance of ImmutableSortedSet contains its own private data and will never change. This class is convenient for public static final sets ("constant sets") and also lets you easily make a "defensive copy" of a set provided to your class by a caller.

The sets returned by headSet(E), tailSet(E), and subSet(E, E) methods share the same array as the original set, 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(java.util.SortedSet).

Note on element equivalence: The ImmutableCollection.contains(Object), ImmutableCollection.containsAll(Collection), and ImmutableSet.equals(Object) implementations must check whether a provided object is equivalent to an element in the collection. Unlike most collections, an ImmutableSortedSet 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 sets, an ImmutableSortedSet will not function correctly if an element is modified after being placed in the set. 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.

Since:
2 (imported from Google Collections Library)
Author:
Jared Levy, Louis Wasserman
See Also:
ImmutableSet, Serialized Form

Nested Class Summary
static class ImmutableSortedSet.Builder<E>
          A builder for creating immutable sorted set instances, especially public static final sets ("constant sets"), with a given comparator.
 
Method Summary
static
<E> ImmutableSortedSet.Builder<E>
builder()
          Deprecated. Use naturalOrder(), which offers better type-safety.
 Comparator<? super E> comparator()
          Returns the comparator that orders the elements, which is Ordering.natural() when the natural ordering of the elements is used.
static
<E> ImmutableSortedSet<E>
copyOf(Collection<? extends E> elements)
          Returns an immutable sorted set containing the given elements sorted by their natural ordering.
static
<E> ImmutableSortedSet<E>
copyOf(Comparator<? super E> comparator, Collection<? extends E> elements)
          Returns an immutable sorted set containing the given elements sorted by the given Comparator.
static
<E> ImmutableSortedSet<E>
copyOf(Comparator<? super E> comparator, Iterable<? extends E> elements)
          Returns an immutable sorted set containing the given elements sorted by the given Comparator.
static
<E> ImmutableSortedSet<E>
copyOf(Comparator<? super E> comparator, Iterator<? extends E> elements)
          Returns an immutable sorted set containing the given elements sorted by the given Comparator.
static
<E extends Comparable<? super E>>
ImmutableSortedSet<E>
copyOf(E[] elements)
          Returns an immutable sorted set containing the given elements sorted by their natural ordering.
static
<E> ImmutableSortedSet<E>
copyOf(Iterable<? extends E> elements)
          Returns an immutable sorted set containing the given elements sorted by their natural ordering.
static
<E> ImmutableSortedSet<E>
copyOf(Iterator<? extends E> elements)
          Returns an immutable sorted set containing the given elements sorted by their natural ordering.
static
<E> ImmutableSortedSet<E>
copyOfSorted(SortedSet<E> sortedSet)
          Returns an immutable sorted set containing the elements of a sorted set, sorted by the same Comparator.
 ImmutableSortedSet<E> headSet(E toElement)
          Returns a view of the portion of this set whose elements are strictly less than toElement.
static
<E extends Comparable<E>>
ImmutableSortedSet.Builder<E>
naturalOrder()
          Returns a builder that creates immutable sorted sets whose elements are ordered by their natural ordering.
static
<E> ImmutableSortedSet<E>
of()
          Returns the empty immutable sorted set.
static
<E extends Comparable<? super E>>
ImmutableSortedSet<E>
of(E element)
          Returns an immutable sorted set containing a single element.
static
<E extends Comparable<? super E>>
ImmutableSortedSet<E>
of(E[] elements)
          Deprecated. use copyOf(Comparable[]). This method is scheduled for deletion in October 2011.
static
<E extends Comparable<? super E>>
ImmutableSortedSet<E>
of(E e1, E e2)
          Returns an immutable sorted set containing the given elements sorted by their natural ordering.
static
<E extends Comparable<? super E>>
ImmutableSortedSet<E>
of(E e1, E e2, E e3)
          Returns an immutable sorted set containing the given elements sorted by their natural ordering.
static
<E extends Comparable<? super E>>
ImmutableSortedSet<E>
of(E e1, E e2, E e3, E e4)
          Returns an immutable sorted set containing the given elements sorted by their natural ordering.
static
<E extends Comparable<? super E>>
ImmutableSortedSet<E>
of(E e1, E e2, E e3, E e4, E e5)
          Returns an immutable sorted set containing the given elements sorted by their natural ordering.
static
<E extends Comparable<? super E>>
ImmutableSortedSet<E>
of(E e1, E e2, E e3, E e4, E e5, E e6, E... remaining)
          Returns an immutable sorted set containing the given elements sorted by their natural ordering.
static
<E> ImmutableSortedSet.Builder<E>
orderedBy(Comparator<E> comparator)
          Returns a builder that creates immutable sorted sets with an explicit comparator.
static
<E extends Comparable<E>>
ImmutableSortedSet.Builder<E>
reverseOrder()
          Returns a builder that creates immutable sorted sets whose elements are ordered by the reverse of their natural ordering.
 ImmutableSortedSet<E> subSet(E fromElement, E toElement)
          Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
 ImmutableSortedSet<E> tailSet(E fromElement)
          Returns a view of the portion of this set whose elements are greater than or equal to fromElement.
static
<E> ImmutableSortedSet<E>
withExplicitOrder(E firstElement, E... remainingElementsInOrder)
          Deprecated. If the provided elements are already in their natural order, of() will produce a set with the same elements in the same order. If the elements are arranged according to another Comparator, use orderedBy(Comparator). Otherwise, use ImmutableSet, which maintains insertion order for inputs that are ordered but not sorted. This method is scheduled for deletion in Guava release 10.
static
<E> ImmutableSortedSet<E>
withExplicitOrder(List<E> elements)
          Deprecated. If the provided elements are already in their natural order, copyOf(Iterable) will produce a set with the same elements in the same order. If the elements are arranged according to another Comparator, use orderedBy(Comparator). Otherwise, use ImmutableSet, which maintains insertion order for inputs that are ordered but not sorted. This method is scheduled for deletion in Guava release 10.
 
Methods inherited from class com.google.common.collect.ImmutableSet
equals, hashCode, iterator
 
Methods inherited from class com.google.common.collect.ImmutableCollection
add, addAll, asList, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.SortedSet
first, last
 
Methods inherited from interface java.util.Set
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 

Method Detail

of

public static <E> ImmutableSortedSet<E> of()
Returns the empty immutable sorted set.


of

public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(E element)
Returns an immutable sorted set containing a single element.


of

public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(E e1,
                                                                         E e2)
Returns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according to Comparable.compareTo(T), only the first one specified is included.

Throws:
NullPointerException - if any element is null

of

public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(E e1,
                                                                         E e2,
                                                                         E e3)
Returns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according to Comparable.compareTo(T), only the first one specified is included.

Throws:
NullPointerException - if any element is null

of

public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(E e1,
                                                                         E e2,
                                                                         E e3,
                                                                         E e4)
Returns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according to Comparable.compareTo(T), only the first one specified is included.

Throws:
NullPointerException - if any element is null

of

public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(E e1,
                                                                         E e2,
                                                                         E e3,
                                                                         E e4,
                                                                         E e5)
Returns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according to Comparable.compareTo(T), only the first one specified is included.

Throws:
NullPointerException - if any element is null

of

public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(E e1,
                                                                         E e2,
                                                                         E e3,
                                                                         E e4,
                                                                         E e5,
                                                                         E e6,
                                                                         E... remaining)
Returns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according to Comparable.compareTo(T), only the first one specified is included.

Throws:
NullPointerException - if any element is null
Since:
3 (source-compatible since release 2)

of

@Deprecated
public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(E[] elements)
Deprecated. use copyOf(Comparable[]). This method is scheduled for deletion in October 2011.

Returns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according to Comparable.compareTo(T), only the first one specified is included.

Throws:
NullPointerException - if any of elements is null
Since:
2 (changed from varargs in release 3)

copyOf

public static <E extends Comparable<? super E>> ImmutableSortedSet<E> copyOf(E[] elements)
Returns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according to Comparable.compareTo(T), only the first one specified is included.

Throws:
NullPointerException - if any of elements is null
Since:
3

copyOf

public static <E> ImmutableSortedSet<E> copyOf(Iterable<? extends E> elements)
Returns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according to compareTo(), only the first one specified is included. To create a copy of a SortedSet that preserves the comparator, call copyOfSorted(java.util.SortedSet) instead. This method iterates over elements at most once.

Note that if s is a Set<String>, then ImmutableSortedSet.copyOf(s) returns an ImmutableSortedSet<String> containing each of the strings in s, while ImmutableSortedSet.of(s) returns an ImmutableSortedSet<Set<String>> containing one element (the given set 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.

Throws:
ClassCastException - if the elements are not mutually comparable
NullPointerException - if any of elements is null

copyOf

public static <E> ImmutableSortedSet<E> copyOf(Collection<? extends E> elements)
Returns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according to compareTo(), only the first one specified is included. To create a copy of a SortedSet that preserves the comparator, call copyOfSorted(java.util.SortedSet) instead. This method iterates over elements at most once.

Note that if s is a Set<String>, then ImmutableSortedSet.copyOf(s) returns an ImmutableSortedSet<String> containing each of the strings in s, while ImmutableSortedSet.of(s) returns an ImmutableSortedSet<Set<String>> containing one element (the given set itself).

Note: Despite what the method name suggests, if elements is an ImmutableSortedSet, it may be returned instead of a copy.

This method is not type-safe, as it may be called on elements that are not mutually comparable.

This method is safe to use even when elements is a synchronized or concurrent collection that is currently being modified by another thread.

Throws:
ClassCastException - if the elements are not mutually comparable
NullPointerException - if any of elements is null
Since:
7 (source-compatible since release 2)

copyOf

public static <E> ImmutableSortedSet<E> copyOf(Iterator<? extends E> elements)
Returns an immutable sorted set containing the given elements sorted by their natural ordering. When multiple elements are equivalent according to compareTo(), only the first one specified is included.

This method is not type-safe, as it may be called on elements that are not mutually comparable.

Throws:
ClassCastException - if the elements are not mutually comparable
NullPointerException - if any of elements is null

copyOf

public static <E> ImmutableSortedSet<E> copyOf(Comparator<? super E> comparator,
                                               Iterator<? extends E> elements)
Returns an immutable sorted set containing the given elements sorted by the given Comparator. When multiple elements are equivalent according to compareTo(), only the first one specified is included.

Throws:
NullPointerException - if comparator or any of elements is null

copyOf

public static <E> ImmutableSortedSet<E> copyOf(Comparator<? super E> comparator,
                                               Iterable<? extends E> elements)
Returns an immutable sorted set containing the given elements sorted by the given Comparator. When multiple elements are equivalent according to compare(), only the first one specified is included. 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.

Throws:
NullPointerException - if comparator or any of elements is null

copyOf

public static <E> ImmutableSortedSet<E> copyOf(Comparator<? super E> comparator,
                                               Collection<? extends E> elements)
Returns an immutable sorted set containing the given elements sorted by the given Comparator. When multiple elements are equivalent according to compareTo(), only the first one specified is included.

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 elements is a synchronized or concurrent collection that is currently being modified by another thread.

Throws:
NullPointerException - if comparator or any of elements is null
Since:
7 (source-compatible since release 2)

copyOfSorted

public static <E> ImmutableSortedSet<E> copyOfSorted(SortedSet<E> sortedSet)
Returns an immutable sorted set containing the elements of a sorted set, sorted by the same 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 elements is a synchronized or concurrent collection that is currently being modified by another thread.

Throws:
NullPointerException - if sortedSet or any of its elements is null

withExplicitOrder

@Beta
@Deprecated
public static <E> ImmutableSortedSet<E> withExplicitOrder(List<E> elements)
Deprecated. If the provided elements are already in their natural order, copyOf(Iterable) will produce a set with the same elements in the same order. If the elements are arranged according to another Comparator, use orderedBy(Comparator). Otherwise, use ImmutableSet, which maintains insertion order for inputs that are ordered but not sorted. This method is scheduled for deletion in Guava release 10.

Returns an immutable sorted set containing the elements in the given list in the same order. It is useful when the elements already have the desired order but constructing the appropriate comparator is difficult.

Throws:
NullPointerException - if any of the elements is null
IllegalArgumentException - if elements contains any duplicate values (according to Object.equals(java.lang.Object))
Since:
3

withExplicitOrder

@Beta
@Deprecated
public static <E> ImmutableSortedSet<E> withExplicitOrder(E firstElement,
                                                                          E... remainingElementsInOrder)
Deprecated. If the provided elements are already in their natural order, of() will produce a set with the same elements in the same order. If the elements are arranged according to another Comparator, use orderedBy(Comparator). Otherwise, use ImmutableSet, which maintains insertion order for inputs that are ordered but not sorted. This method is scheduled for deletion in Guava release 10.

Returns an immutable sorted set containing the provided elements in the same order. It is useful when the elements already have the desired order but constructing the appropriate comparator is difficult.

Parameters:
firstElement - the value which should appear first in the generated set
remainingElementsInOrder - the rest of the values in the generated set, in the order they should appear
Throws:
NullPointerException - if any of the elements is null
IllegalArgumentException - if any duplicate values (according to Object.equals(Object)) are present among the method arguments
Since:
3

orderedBy

public static <E> ImmutableSortedSet.Builder<E> orderedBy(Comparator<E> comparator)
Returns a builder that creates immutable sorted sets with an explicit comparator. If the comparator has a more general type than the set being generated, such as creating a SortedSet<Integer> with a Comparator<Number>, use the ImmutableSortedSet.Builder constructor instead.

Throws:
NullPointerException - if comparator is null

reverseOrder

public static <E extends Comparable<E>> ImmutableSortedSet.Builder<E> reverseOrder()
Returns a builder that creates immutable sorted sets whose elements are ordered by the reverse of their natural ordering.

Note: the type parameter E extends Comparable<E> rather than Comparable<? super E> as a workaround for javac bug 6468354.


naturalOrder

public static <E extends Comparable<E>> ImmutableSortedSet.Builder<E> naturalOrder()
Returns a builder that creates immutable sorted sets whose elements are ordered by their natural ordering. The sorted sets use 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.


comparator

public Comparator<? super E> comparator()
Returns the comparator that orders the elements, which is Ordering.natural() when the natural ordering of the elements is used. Note that its behavior is not consistent with SortedSet.comparator(), which returns null to indicate natural ordering.

Specified by:
comparator in interface SortedSet<E>
Returns:
the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements

headSet

public ImmutableSortedSet<E> headSet(E toElement)
Returns a view of the portion of this set whose elements are strictly less than toElement. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.

The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

This method returns a serializable ImmutableSortedSet.

The SortedSet.headSet(E) documentation states that a subset of a subset throws an IllegalArgumentException if passed a toElement greater than an earlier toElement. However, this method doesn't throw an exception in that situation, but instead keeps the original toElement.

Specified by:
headSet in interface SortedSet<E>
Parameters:
toElement - high endpoint (exclusive) of the returned set
Returns:
a view of the portion of this set whose elements are strictly less than toElement

subSet

public ImmutableSortedSet<E> subSet(E fromElement,
                                    E toElement)
Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. (If fromElement and toElement are equal, the returned set is empty.) The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.

The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

This method returns a serializable ImmutableSortedSet.

The SortedSet.subSet(E, E) documentation states that a subset of a subset throws an IllegalArgumentException if passed a fromElement smaller than an earlier fromElement. However, this method doesn't throw an exception in that situation, but instead keeps the original fromElement. Similarly, this method keeps the original toElement, instead of throwing an exception, if passed a toElement greater than an earlier toElement.

Specified by:
subSet in interface SortedSet<E>
Parameters:
fromElement - low endpoint (inclusive) of the returned set
toElement - high endpoint (exclusive) of the returned set
Returns:
a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive

tailSet

public ImmutableSortedSet<E> tailSet(E fromElement)
Returns a view of the portion of this set whose elements are greater than or equal to fromElement. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.

The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

This method returns a serializable ImmutableSortedSet.

The SortedSet.tailSet(E) documentation states that a subset of a subset throws an IllegalArgumentException if passed a fromElement smaller than an earlier fromElement. However, this method doesn't throw an exception in that situation, but instead keeps the original fromElement.

Specified by:
tailSet in interface SortedSet<E>
Parameters:
fromElement - low endpoint (inclusive) of the returned set
Returns:
a view of the portion of this set whose elements are greater than or equal to fromElement

builder

@Deprecated
public static <E> ImmutableSortedSet.Builder<E> builder()
Deprecated. Use naturalOrder(), which offers better type-safety.

Not supported. Use naturalOrder(), which offers better type-safety, instead. This method exists only to hide ImmutableSet.builder() from consumers of ImmutableSortedSet.

Throws:
UnsupportedOperationException - always