001    /*
002     * Copyright (C) 2008 The Guava Authors
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.google.common.collect;
018    
019    import static com.google.common.base.Preconditions.checkArgument;
020    import static com.google.common.base.Preconditions.checkNotNull;
021    
022    import com.google.common.annotations.GwtCompatible;
023    import com.google.common.annotations.GwtIncompatible;
024    
025    import java.io.InvalidObjectException;
026    import java.io.ObjectInputStream;
027    import java.io.Serializable;
028    import java.util.ArrayList;
029    import java.util.Arrays;
030    import java.util.Collection;
031    import java.util.Collections;
032    import java.util.Comparator;
033    import java.util.Iterator;
034    import java.util.List;
035    import java.util.NavigableSet;
036    import java.util.SortedSet;
037    
038    import javax.annotation.Nullable;
039    
040    /**
041     * An immutable {@code SortedSet} that stores its elements in a sorted array.
042     * Some instances are ordered by an explicit comparator, while others follow the
043     * natural sort ordering of their elements. Either way, null elements are not
044     * supported.
045     *
046     * <p>Unlike {@link Collections#unmodifiableSortedSet}, which is a <i>view</i>
047     * of a separate collection that can still change, an instance of {@code
048     * ImmutableSortedSet} contains its own private data and will <i>never</i>
049     * change. This class is convenient for {@code public static final} sets
050     * ("constant sets") and also lets you easily make a "defensive copy" of a set
051     * provided to your class by a caller.
052     *
053     * <p>The sets returned by the {@link #headSet}, {@link #tailSet}, and
054     * {@link #subSet} methods share the same array as the original set, preventing
055     * that array from being garbage collected. If this is a concern, the data may
056     * be copied into a correctly-sized array by calling {@link #copyOfSorted}.
057     *
058     * <p><b>Note on element equivalence:</b> The {@link #contains(Object)},
059     * {@link #containsAll(Collection)}, and {@link #equals(Object)}
060     * implementations must check whether a provided object is equivalent to an
061     * element in the collection. Unlike most collections, an
062     * {@code ImmutableSortedSet} doesn't use {@link Object#equals} to determine if
063     * two elements are equivalent. Instead, with an explicit comparator, the
064     * following relation determines whether elements {@code x} and {@code y} are
065     * equivalent: <pre>   {@code
066     *
067     *   {(x, y) | comparator.compare(x, y) == 0}}</pre>
068     *
069     * With natural ordering of elements, the following relation determines whether
070     * two elements are equivalent: <pre>   {@code
071     *
072     *   {(x, y) | x.compareTo(y) == 0}}</pre>
073     *
074     * <b>Warning:</b> Like most sets, an {@code ImmutableSortedSet} will not
075     * function correctly if an element is modified after being placed in the set.
076     * For this reason, and to avoid general confusion, it is strongly recommended
077     * to place only immutable objects into this collection.
078     *
079     * <p><b>Note:</b> Although this class is not final, it cannot be subclassed as
080     * it has no public or protected constructors. Thus, instances of this type are
081     * guaranteed to be immutable.
082     *
083     * <p>See the Guava User Guide article on <a href=
084     * "http://code.google.com/p/guava-libraries/wiki/ImmutableCollectionsExplained">
085     * immutable collections</a>.
086     *
087     * @see ImmutableSet
088     * @author Jared Levy
089     * @author Louis Wasserman
090     * @since 2.0 (imported from Google Collections Library; implements {@code NavigableSet} since 12.0)
091     */
092    // TODO(benyu): benchmark and optimize all creation paths, which are a mess now
093    @GwtCompatible(serializable = true, emulated = true)
094    @SuppressWarnings("serial") // we're overriding default serialization
095    public abstract class ImmutableSortedSet<E> extends ImmutableSortedSetFauxverideShim<E>
096        implements NavigableSet<E>, SortedIterable<E> {
097    
098      private static final Comparator<Comparable> NATURAL_ORDER =
099          Ordering.natural();
100    
101      private static final ImmutableSortedSet<Comparable> NATURAL_EMPTY_SET =
102          new EmptyImmutableSortedSet<Comparable>(NATURAL_ORDER);
103    
104      @SuppressWarnings("unchecked")
105      private static <E> ImmutableSortedSet<E> emptySet() {
106        return (ImmutableSortedSet<E>) NATURAL_EMPTY_SET;
107      }
108    
109      static <E> ImmutableSortedSet<E> emptySet(
110          Comparator<? super E> comparator) {
111        if (NATURAL_ORDER.equals(comparator)) {
112          return emptySet();
113        } else {
114          return new EmptyImmutableSortedSet<E>(comparator);
115        }
116      }
117    
118      /**
119       * Returns the empty immutable sorted set.
120       */
121      public static <E> ImmutableSortedSet<E> of() {
122        return emptySet();
123      }
124    
125      /**
126       * Returns an immutable sorted set containing a single element.
127       */
128      public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(
129          E element) {
130        return new RegularImmutableSortedSet<E>(
131            ImmutableList.of(element), Ordering.natural());
132      }
133    
134      /**
135       * Returns an immutable sorted set containing the given elements sorted by
136       * their natural ordering. When multiple elements are equivalent according to
137       * {@link Comparable#compareTo}, only the first one specified is included.
138       *
139       * @throws NullPointerException if any element is null
140       */
141      @SuppressWarnings("unchecked")
142      public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(
143          E e1, E e2) {
144        return copyOf(Ordering.natural(), Arrays.asList(e1, e2));
145      }
146    
147      /**
148       * Returns an immutable sorted set containing the given elements sorted by
149       * their natural ordering. When multiple elements are equivalent according to
150       * {@link Comparable#compareTo}, only the first one specified is included.
151       *
152       * @throws NullPointerException if any element is null
153       */
154      @SuppressWarnings("unchecked")
155      public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(
156          E e1, E e2, E e3) {
157        return copyOf(Ordering.natural(), Arrays.asList(e1, e2, e3));
158      }
159    
160      /**
161       * Returns an immutable sorted set containing the given elements sorted by
162       * their natural ordering. When multiple elements are equivalent according to
163       * {@link Comparable#compareTo}, only the first one specified is included.
164       *
165       * @throws NullPointerException if any element is null
166       */
167      @SuppressWarnings("unchecked")
168      public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(
169          E e1, E e2, E e3, E e4) {
170        return copyOf(Ordering.natural(), Arrays.asList(e1, e2, e3, e4));
171      }
172    
173      /**
174       * Returns an immutable sorted set containing the given elements sorted by
175       * their natural ordering. When multiple elements are equivalent according to
176       * {@link Comparable#compareTo}, only the first one specified is included.
177       *
178       * @throws NullPointerException if any element is null
179       */
180      @SuppressWarnings("unchecked")
181      public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(
182          E e1, E e2, E e3, E e4, E e5) {
183        return copyOf(Ordering.natural(), Arrays.asList(e1, e2, e3, e4, e5));
184      }
185    
186      /**
187       * Returns an immutable sorted set containing the given elements sorted by
188       * their natural ordering. When multiple elements are equivalent according to
189       * {@link Comparable#compareTo}, only the first one specified is included.
190       *
191       * @throws NullPointerException if any element is null
192       * @since 3.0 (source-compatible since 2.0)
193       */
194      @SuppressWarnings("unchecked")
195      public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(
196          E e1, E e2, E e3, E e4, E e5, E e6, E... remaining) {
197        int size = remaining.length + 6;
198        List<E> all = new ArrayList<E>(size);
199        Collections.addAll(all, e1, e2, e3, e4, e5, e6);
200        Collections.addAll(all, remaining);
201        return copyOf(Ordering.natural(), all);
202      }
203    
204      // TODO(kevinb): Consider factory methods that reject duplicates
205    
206      /**
207       * Returns an immutable sorted set containing the given elements sorted by
208       * their natural ordering. When multiple elements are equivalent according to
209       * {@link Comparable#compareTo}, only the first one specified is included.
210       *
211       * @throws NullPointerException if any of {@code elements} is null
212       * @since 3.0
213       */
214      public static <E extends Comparable<? super E>> ImmutableSortedSet<E> copyOf(
215          E[] elements) {
216        return copyOf(Ordering.natural(), Arrays.asList(elements));
217      }
218    
219      /**
220       * Returns an immutable sorted set containing the given elements sorted by
221       * their natural ordering. When multiple elements are equivalent according to
222       * {@code compareTo()}, only the first one specified is included. To create a
223       * copy of a {@code SortedSet} that preserves the comparator, call {@link
224       * #copyOfSorted} instead. This method iterates over {@code elements} at most
225       * once.
226    
227       *
228       * <p>Note that if {@code s} is a {@code Set<String>}, then {@code
229       * ImmutableSortedSet.copyOf(s)} returns an {@code ImmutableSortedSet<String>}
230       * containing each of the strings in {@code s}, while {@code
231       * ImmutableSortedSet.of(s)} returns an {@code
232       * ImmutableSortedSet<Set<String>>} containing one element (the given set
233       * itself).
234       *
235       * <p>Despite the method name, this method attempts to avoid actually copying
236       * the data when it is safe to do so. The exact circumstances under which a
237       * copy will or will not be performed are undocumented and subject to change.
238       *
239       * <p>This method is not type-safe, as it may be called on elements that are
240       * not mutually comparable.
241       *
242       * @throws ClassCastException if the elements are not mutually comparable
243       * @throws NullPointerException if any of {@code elements} is null
244       */
245      public static <E> ImmutableSortedSet<E> copyOf(
246          Iterable<? extends E> elements) {
247        // Hack around E not being a subtype of Comparable.
248        // Unsafe, see ImmutableSortedSetFauxverideShim.
249        @SuppressWarnings("unchecked")
250        Ordering<E> naturalOrder = (Ordering<E>) Ordering.<Comparable>natural();
251        return copyOf(naturalOrder, elements);
252      }
253    
254      /**
255       * Returns an immutable sorted set containing the given elements sorted by
256       * their natural ordering. When multiple elements are equivalent according to
257       * {@code compareTo()}, only the first one specified is included. To create a
258       * copy of a {@code SortedSet} that preserves the comparator, call
259       * {@link #copyOfSorted} instead. This method iterates over {@code elements}
260       * at most once.
261       *
262       * <p>Note that if {@code s} is a {@code Set<String>}, then
263       * {@code ImmutableSortedSet.copyOf(s)} returns an
264       * {@code ImmutableSortedSet<String>} containing each of the strings in
265       * {@code s}, while {@code ImmutableSortedSet.of(s)} returns an
266       * {@code ImmutableSortedSet<Set<String>>} containing one element (the given
267       * set itself).
268       *
269       * <p><b>Note:</b> Despite what the method name suggests, if {@code elements}
270       * is an {@code ImmutableSortedSet}, it may be returned instead of a copy.
271       *
272       * <p>This method is not type-safe, as it may be called on elements that are
273       * not mutually comparable.
274       *
275       * <p>This method is safe to use even when {@code elements} is a synchronized
276       * or concurrent collection that is currently being modified by another
277       * thread.
278       *
279       * @throws ClassCastException if the elements are not mutually comparable
280       * @throws NullPointerException if any of {@code elements} is null
281       * @since 7.0 (source-compatible since 2.0)
282       */
283      public static <E> ImmutableSortedSet<E> copyOf(
284          Collection<? extends E> elements) {
285        // Hack around E not being a subtype of Comparable.
286        // Unsafe, see ImmutableSortedSetFauxverideShim.
287        @SuppressWarnings("unchecked")
288        Ordering<E> naturalOrder = (Ordering<E>) Ordering.<Comparable>natural();
289        return copyOf(naturalOrder, elements);
290      }
291    
292      /**
293       * Returns an immutable sorted set containing the given elements sorted by
294       * their natural ordering. When multiple elements are equivalent according to
295       * {@code compareTo()}, only the first one specified is included.
296       *
297       * <p>This method is not type-safe, as it may be called on elements that are
298       * not mutually comparable.
299       *
300       * @throws ClassCastException if the elements are not mutually comparable
301       * @throws NullPointerException if any of {@code elements} is null
302       */
303      public static <E> ImmutableSortedSet<E> copyOf(
304          Iterator<? extends E> elements) {
305        // Hack around E not being a subtype of Comparable.
306        // Unsafe, see ImmutableSortedSetFauxverideShim.
307        @SuppressWarnings("unchecked")
308        Ordering<E> naturalOrder = (Ordering<E>) Ordering.<Comparable>natural();
309        return copyOf(naturalOrder, elements);
310      }
311    
312      /**
313       * Returns an immutable sorted set containing the given elements sorted by
314       * the given {@code Comparator}. When multiple elements are equivalent
315       * according to {@code compareTo()}, only the first one specified is
316       * included.
317       *
318       * @throws NullPointerException if {@code comparator} or any of
319       *     {@code elements} is null
320       */
321      public static <E> ImmutableSortedSet<E> copyOf(
322          Comparator<? super E> comparator, Iterator<? extends E> elements) {
323        return copyOf(comparator, Lists.newArrayList(elements));
324      }
325    
326      /**
327       * Returns an immutable sorted set containing the given elements sorted by
328       * the given {@code Comparator}. When multiple elements are equivalent
329       * according to {@code compare()}, only the first one specified is
330       * included. This method iterates over {@code elements} at most once.
331       *
332       * <p>Despite the method name, this method attempts to avoid actually copying
333       * the data when it is safe to do so. The exact circumstances under which a
334       * copy will or will not be performed are undocumented and subject to change.
335       *
336       * @throws NullPointerException if {@code comparator} or any of {@code
337       *         elements} is null
338       */
339      public static <E> ImmutableSortedSet<E> copyOf(
340          Comparator<? super E> comparator, Iterable<? extends E> elements) {
341        checkNotNull(comparator);
342        boolean hasSameComparator =
343            SortedIterables.hasSameComparator(comparator, elements);
344    
345        if (hasSameComparator && (elements instanceof ImmutableSortedSet)) {
346          @SuppressWarnings("unchecked")
347          ImmutableSortedSet<E> original = (ImmutableSortedSet<E>) elements;
348          if (!original.isPartialView()) {
349            return original;
350          }
351        }
352        @SuppressWarnings("unchecked") // elements only contains E's; it's safe.
353        E[] array = (E[]) Iterables.toArray(elements);
354        return construct(comparator, array.length, array);
355      }
356    
357      /**
358       * Returns an immutable sorted set containing the given elements sorted by
359       * the given {@code Comparator}. When multiple elements are equivalent
360       * according to {@code compareTo()}, only the first one specified is
361       * included.
362       *
363       * <p>Despite the method name, this method attempts to avoid actually copying
364       * the data when it is safe to do so. The exact circumstances under which a
365       * copy will or will not be performed are undocumented and subject to change.
366       *
367       * <p>This method is safe to use even when {@code elements} is a synchronized
368       * or concurrent collection that is currently being modified by another
369       * thread.
370       *
371       * @throws NullPointerException if {@code comparator} or any of
372       *     {@code elements} is null
373       * @since 7.0 (source-compatible since 2.0)
374       */
375      public static <E> ImmutableSortedSet<E> copyOf(
376          Comparator<? super E> comparator, Collection<? extends E> elements) {
377        return copyOf(comparator, (Iterable<? extends E>) elements);
378      }
379    
380      /**
381       * Returns an immutable sorted set containing the elements of a sorted set,
382       * sorted by the same {@code Comparator}. That behavior differs from {@link
383       * #copyOf(Iterable)}, which always uses the natural ordering of the
384       * elements.
385       *
386       * <p>Despite the method name, this method attempts to avoid actually copying
387       * the data when it is safe to do so. The exact circumstances under which a
388       * copy will or will not be performed are undocumented and subject to change.
389       *
390       * <p>This method is safe to use even when {@code sortedSet} is a synchronized
391       * or concurrent collection that is currently being modified by another
392       * thread.
393       *
394       * @throws NullPointerException if {@code sortedSet} or any of its elements
395       *     is null
396       */
397      public static <E> ImmutableSortedSet<E> copyOfSorted(SortedSet<E> sortedSet) {
398        Comparator<? super E> comparator = SortedIterables.comparator(sortedSet);
399        E[] elements = (E[]) sortedSet.toArray();
400        if (elements.length == 0) {
401          return emptySet(comparator);
402        } else {
403          return new RegularImmutableSortedSet<E>(
404              ImmutableList.<E>asImmutableList(elements), comparator);
405        }
406      }
407    
408      /**
409       * Sorts and eliminates duplicates from the first {@code n} positions in {@code contents}.
410       * Returns the number of unique elements.  If this returns {@code k}, then the first {@code k}
411       * elements of {@code contents} will be the sorted, unique elements, and {@code
412       * contents[i] == null} for {@code k <= i < n}.
413       *
414       * @throws NullPointerException if any of the first {@code n} elements of {@code contents} is
415       *          null
416       */
417      static <E> int sortAndUnique(
418          Comparator<? super E> comparator, int n, E... contents) {
419        if (n == 0) {
420          return 0;
421        }
422        for (int i = 0; i < n; i++) {
423          ObjectArrays.checkElementNotNull(contents[i], i);
424        }
425        Arrays.sort(contents, 0, n, comparator);
426        int uniques = 1;
427        for (int i = 1; i < n; i++) {
428          E cur = contents[i];
429          E prev = contents[uniques - 1];
430          if (comparator.compare(cur, prev) != 0) {
431            contents[uniques++] = cur;
432          }
433        }
434        Arrays.fill(contents, uniques, n, null);
435        return uniques;
436      }
437    
438      /**
439       * Constructs an {@code ImmutableSortedSet} from the first {@code n} elements of
440       * {@code contents}.  If {@code k} is the size of the returned {@code ImmutableSortedSet}, then
441       * the sorted unique elements are in the first {@code k} positions of {@code contents}, and
442       * {@code contents[i] == null} for {@code k <= i < n}.
443       *
444       * <p>If {@code k == contents.length}, then {@code contents} may no longer be safe for
445       * modification.
446       *
447       * @throws NullPointerException if any of the first {@code n} elements of {@code contents} is
448       *          null
449       */
450      static <E> ImmutableSortedSet<E> construct(
451          Comparator<? super E> comparator, int n, E... contents) {
452        int uniques = sortAndUnique(comparator, n, contents);
453        if (uniques == 0) {
454          return emptySet(comparator);
455        } else if (uniques < contents.length) {
456          contents = ObjectArrays.arraysCopyOf(contents, uniques);
457        }
458        return new RegularImmutableSortedSet<E>(
459            ImmutableList.<E>asImmutableList(contents), comparator);
460      }
461    
462      /**
463       * Returns a builder that creates immutable sorted sets with an explicit
464       * comparator. If the comparator has a more general type than the set being
465       * generated, such as creating a {@code SortedSet<Integer>} with a
466       * {@code Comparator<Number>}, use the {@link Builder} constructor instead.
467       *
468       * @throws NullPointerException if {@code comparator} is null
469       */
470      public static <E> Builder<E> orderedBy(Comparator<E> comparator) {
471        return new Builder<E>(comparator);
472      }
473    
474      /**
475       * Returns a builder that creates immutable sorted sets whose elements are
476       * ordered by the reverse of their natural ordering.
477       *
478       * <p>Note: the type parameter {@code E} extends {@code Comparable<E>} rather
479       * than {@code Comparable<? super E>} as a workaround for javac <a
480       * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6468354">bug
481       * 6468354</a>.
482       */
483      public static <E extends Comparable<E>> Builder<E> reverseOrder() {
484        return new Builder<E>(Ordering.natural().reverse());
485      }
486    
487      /**
488       * Returns a builder that creates immutable sorted sets whose elements are
489       * ordered by their natural ordering. The sorted sets use {@link
490       * Ordering#natural()} as the comparator. This method provides more
491       * type-safety than {@link #builder}, as it can be called only for classes
492       * that implement {@link Comparable}.
493       *
494       * <p>Note: the type parameter {@code E} extends {@code Comparable<E>} rather
495       * than {@code Comparable<? super E>} as a workaround for javac <a
496       * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6468354">bug
497       * 6468354</a>.
498       */
499      public static <E extends Comparable<E>> Builder<E> naturalOrder() {
500        return new Builder<E>(Ordering.natural());
501      }
502    
503      /**
504       * A builder for creating immutable sorted set instances, especially {@code
505       * public static final} sets ("constant sets"), with a given comparator.
506       * Example: <pre>   {@code
507       *
508       *   public static final ImmutableSortedSet<Number> LUCKY_NUMBERS =
509       *       new ImmutableSortedSet.Builder<Number>(ODDS_FIRST_COMPARATOR)
510       *           .addAll(SINGLE_DIGIT_PRIMES)
511       *           .add(42)
512       *           .build();}</pre>
513       *
514       * Builder instances can be reused; it is safe to call {@link #build} multiple
515       * times to build multiple sets in series. Each set is a superset of the set
516       * created before it.
517       *
518       * @since 2.0 (imported from Google Collections Library)
519       */
520      public static final class Builder<E> extends ImmutableSet.Builder<E> {
521        private final Comparator<? super E> comparator;
522    
523        /**
524         * Creates a new builder. The returned builder is equivalent to the builder
525         * generated by {@link ImmutableSortedSet#orderedBy}.
526         */
527        public Builder(Comparator<? super E> comparator) {
528          this.comparator = checkNotNull(comparator);
529        }
530    
531        /**
532         * Adds {@code element} to the {@code ImmutableSortedSet}.  If the
533         * {@code ImmutableSortedSet} already contains {@code element}, then
534         * {@code add} has no effect. (only the previously added element
535         * is retained).
536         *
537         * @param element the element to add
538         * @return this {@code Builder} object
539         * @throws NullPointerException if {@code element} is null
540         */
541        @Override public Builder<E> add(E element) {
542          super.add(element);
543          return this;
544        }
545    
546        /**
547         * Adds each element of {@code elements} to the {@code ImmutableSortedSet},
548         * ignoring duplicate elements (only the first duplicate element is added).
549         *
550         * @param elements the elements to add
551         * @return this {@code Builder} object
552         * @throws NullPointerException if {@code elements} contains a null element
553         */
554        @Override public Builder<E> add(E... elements) {
555          super.add(elements);
556          return this;
557        }
558    
559        /**
560         * Adds each element of {@code elements} to the {@code ImmutableSortedSet},
561         * ignoring duplicate elements (only the first duplicate element is added).
562         *
563         * @param elements the elements to add to the {@code ImmutableSortedSet}
564         * @return this {@code Builder} object
565         * @throws NullPointerException if {@code elements} contains a null element
566         */
567        @Override public Builder<E> addAll(Iterable<? extends E> elements) {
568          super.addAll(elements);
569          return this;
570        }
571    
572        /**
573         * Adds each element of {@code elements} to the {@code ImmutableSortedSet},
574         * ignoring duplicate elements (only the first duplicate element is added).
575         *
576         * @param elements the elements to add to the {@code ImmutableSortedSet}
577         * @return this {@code Builder} object
578         * @throws NullPointerException if {@code elements} contains a null element
579         */
580        @Override public Builder<E> addAll(Iterator<? extends E> elements) {
581          super.addAll(elements);
582          return this;
583        }
584    
585        /**
586         * Returns a newly-created {@code ImmutableSortedSet} based on the contents
587         * of the {@code Builder} and its comparator.
588         */
589        @Override public ImmutableSortedSet<E> build() {
590          @SuppressWarnings("unchecked") // we're careful to put only E's in here
591          E[] contentsArray = (E[]) contents;
592          ImmutableSortedSet<E> result = construct(comparator, size, contentsArray);
593          this.size = result.size(); // we eliminated duplicates in-place in contentsArray
594          return result;
595        }
596      }
597    
598      int unsafeCompare(Object a, Object b) {
599        return unsafeCompare(comparator, a, b);
600      }
601    
602      static int unsafeCompare(
603          Comparator<?> comparator, Object a, Object b) {
604        // Pretend the comparator can compare anything. If it turns out it can't
605        // compare a and b, we should get a CCE on the subsequent line. Only methods
606        // that are spec'd to throw CCE should call this.
607        @SuppressWarnings("unchecked")
608        Comparator<Object> unsafeComparator = (Comparator<Object>) comparator;
609        return unsafeComparator.compare(a, b);
610      }
611    
612      final transient Comparator<? super E> comparator;
613    
614      ImmutableSortedSet(Comparator<? super E> comparator) {
615        this.comparator = comparator;
616      }
617    
618      /**
619       * Returns the comparator that orders the elements, which is
620       * {@link Ordering#natural()} when the natural ordering of the
621       * elements is used. Note that its behavior is not consistent with
622       * {@link SortedSet#comparator()}, which returns {@code null} to indicate
623       * natural ordering.
624       */
625      @Override
626      public Comparator<? super E> comparator() {
627        return comparator;
628      }
629    
630      @Override // needed to unify the iterator() methods in Collection and SortedIterable
631      public abstract UnmodifiableIterator<E> iterator();
632    
633      /**
634       * {@inheritDoc}
635       *
636       * <p>This method returns a serializable {@code ImmutableSortedSet}.
637       *
638       * <p>The {@link SortedSet#headSet} documentation states that a subset of a
639       * subset throws an {@link IllegalArgumentException} if passed a
640       * {@code toElement} greater than an earlier {@code toElement}. However, this
641       * method doesn't throw an exception in that situation, but instead keeps the
642       * original {@code toElement}.
643       */
644      @Override
645      public ImmutableSortedSet<E> headSet(E toElement) {
646        return headSet(toElement, false);
647      }
648    
649      /**
650       * @since 12.0
651       */
652      @GwtIncompatible("NavigableSet")
653      @Override
654      public ImmutableSortedSet<E> headSet(E toElement, boolean inclusive) {
655        return headSetImpl(checkNotNull(toElement), inclusive);
656      }
657    
658      /**
659       * {@inheritDoc}
660       *
661       * <p>This method returns a serializable {@code ImmutableSortedSet}.
662       *
663       * <p>The {@link SortedSet#subSet} documentation states that a subset of a
664       * subset throws an {@link IllegalArgumentException} if passed a
665       * {@code fromElement} smaller than an earlier {@code fromElement}. However,
666       * this method doesn't throw an exception in that situation, but instead keeps
667       * the original {@code fromElement}. Similarly, this method keeps the
668       * original {@code toElement}, instead of throwing an exception, if passed a
669       * {@code toElement} greater than an earlier {@code toElement}.
670       */
671      @Override
672      public ImmutableSortedSet<E> subSet(E fromElement, E toElement) {
673        return subSet(fromElement, true, toElement, false);
674      }
675    
676      /**
677       * @since 12.0
678       */
679      @GwtIncompatible("NavigableSet")
680      @Override
681      public ImmutableSortedSet<E> subSet(
682          E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
683        checkNotNull(fromElement);
684        checkNotNull(toElement);
685        checkArgument(comparator.compare(fromElement, toElement) <= 0);
686        return subSetImpl(fromElement, fromInclusive, toElement, toInclusive);
687      }
688    
689      /**
690       * {@inheritDoc}
691       *
692       * <p>This method returns a serializable {@code ImmutableSortedSet}.
693       *
694       * <p>The {@link SortedSet#tailSet} documentation states that a subset of a
695       * subset throws an {@link IllegalArgumentException} if passed a
696       * {@code fromElement} smaller than an earlier {@code fromElement}. However,
697       * this method doesn't throw an exception in that situation, but instead keeps
698       * the original {@code fromElement}.
699       */
700      @Override
701      public ImmutableSortedSet<E> tailSet(E fromElement) {
702        return tailSet(fromElement, true);
703      }
704    
705      /**
706       * @since 12.0
707       */
708      @GwtIncompatible("NavigableSet")
709      @Override
710      public ImmutableSortedSet<E> tailSet(E fromElement, boolean inclusive) {
711        return tailSetImpl(checkNotNull(fromElement), inclusive);
712      }
713    
714      /*
715       * These methods perform most headSet, subSet, and tailSet logic, besides
716       * parameter validation.
717       */
718      abstract ImmutableSortedSet<E> headSetImpl(E toElement, boolean inclusive);
719    
720      abstract ImmutableSortedSet<E> subSetImpl(
721          E fromElement, boolean fromInclusive, E toElement, boolean toInclusive);
722    
723      abstract ImmutableSortedSet<E> tailSetImpl(E fromElement, boolean inclusive);
724    
725      /**
726       * @since 12.0
727       */
728      @GwtIncompatible("NavigableSet")
729      @Override
730      public E lower(E e) {
731        return Iterables.getFirst(headSet(e, false).descendingSet(), null);
732      }
733    
734      /**
735       * @since 12.0
736       */
737      @GwtIncompatible("NavigableSet")
738      @Override
739      public E floor(E e) {
740        return Iterables.getFirst(headSet(e, true).descendingSet(), null);
741      }
742    
743      /**
744       * @since 12.0
745       */
746      @GwtIncompatible("NavigableSet")
747      @Override
748      public E ceiling(E e) {
749        return Iterables.getFirst(tailSet(e, true), null);
750      }
751    
752      /**
753       * @since 12.0
754       */
755      @GwtIncompatible("NavigableSet")
756      @Override
757      public E higher(E e) {
758        return Iterables.getFirst(tailSet(e, false), null);
759      }
760    
761      /**
762       * @since 12.0
763       */
764      @GwtIncompatible("NavigableSet")
765      @Override
766      public final E pollFirst() {
767        throw new UnsupportedOperationException();
768      }
769    
770      /**
771       * @since 12.0
772       */
773      @GwtIncompatible("NavigableSet")
774      @Override
775      public final E pollLast() {
776        throw new UnsupportedOperationException();
777      }
778    
779      @GwtIncompatible("NavigableSet")
780      transient ImmutableSortedSet<E> descendingSet;
781    
782      /**
783       * @since 12.0
784       */
785      @GwtIncompatible("NavigableSet")
786      @Override
787      public ImmutableSortedSet<E> descendingSet() {
788        ImmutableSortedSet<E> result = descendingSet;
789        if (result == null) {
790          result = descendingSet = createDescendingSet();
791          result.descendingSet = this;
792        }
793        return result;
794      }
795    
796      @GwtIncompatible("NavigableSet")
797      abstract ImmutableSortedSet<E> createDescendingSet();
798    
799      /**
800       * @since 12.0
801       */
802      @GwtIncompatible("NavigableSet")
803      @Override
804      public UnmodifiableIterator<E> descendingIterator() {
805        return descendingSet().iterator();
806      }
807    
808      /**
809       * Returns the position of an element within the set, or -1 if not present.
810       */
811      abstract int indexOf(@Nullable Object target);
812    
813      /*
814       * This class is used to serialize all ImmutableSortedSet instances,
815       * regardless of implementation type. It captures their "logical contents"
816       * only. This is necessary to ensure that the existence of a particular
817       * implementation type is an implementation detail.
818       */
819      private static class SerializedForm<E> implements Serializable {
820        final Comparator<? super E> comparator;
821        final Object[] elements;
822    
823        public SerializedForm(Comparator<? super E> comparator, Object[] elements) {
824          this.comparator = comparator;
825          this.elements = elements;
826        }
827    
828        @SuppressWarnings("unchecked")
829        Object readResolve() {
830          return new Builder<E>(comparator).add((E[]) elements).build();
831        }
832    
833        private static final long serialVersionUID = 0;
834      }
835    
836      private void readObject(ObjectInputStream stream)
837          throws InvalidObjectException {
838        throw new InvalidObjectException("Use SerializedForm");
839      }
840    
841      @Override Object writeReplace() {
842        return new SerializedForm<E>(comparator, toArray());
843      }
844    }
845