/**
  * Returns a newly-created {@code ImmutableSortedSet} based on the contents of the {@code
  * Builder} and its comparator.
  */
 @Override
 public ImmutableSortedSet<E> build() {
   @SuppressWarnings("unchecked") // we're careful to put only E's in here
   E[] contentsArray = (E[]) contents;
   ImmutableSortedSet<E> result = construct(comparator, size, contentsArray);
   this.size = result.size(); // we eliminated duplicates in-place in contentsArray
   return result;
 }
 /** @since 12.0 */
 @GwtIncompatible("NavigableSet")
 @Override
 public ImmutableSortedSet<E> descendingSet() {
   // racy single-check idiom
   ImmutableSortedSet<E> result = descendingSet;
   if (result == null) {
     result = descendingSet = createDescendingSet();
     result.descendingSet = this;
   }
   return result;
 }
  /**
   * Returns an immutable sorted set containing the given elements sorted by the given {@code
   * Comparator}. When multiple elements are equivalent according to {@code compare()}, only the
   * first one specified is included. This method iterates over {@code elements} at most once.
   *
   * <p>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 {@code comparator} or any of {@code elements} is null
   */
  public static <E> ImmutableSortedSet<E> copyOf(
      Comparator<? super E> comparator, Iterable<? extends E> elements) {
    checkNotNull(comparator);
    boolean hasSameComparator = SortedIterables.hasSameComparator(comparator, elements);

    if (hasSameComparator && (elements instanceof ImmutableSortedSet)) {
      @SuppressWarnings("unchecked")
      ImmutableSortedSet<E> original = (ImmutableSortedSet<E>) elements;
      if (!original.isPartialView()) {
        return original;
      }
    }
    @SuppressWarnings("unchecked") // elements only contains E's; it's safe.
    E[] array = (E[]) Iterables.toArray(elements);
    return construct(comparator, array.length, array);
  }