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