/** * Returns an ordering that uses the natural order of the values. The ordering * throws a {@link NullPointerException} when passed a null parameter. * * <p>The type specification is {@code <T extends Comparable>}, instead of * the more specific {@code <T extends Comparable<? super T>>}, to support * classes defined without generics. */ @SuppressWarnings("unchecked") // see explanation in method Javadoc public static <T extends Comparable> Ordering<T> natural() { return Comparators.naturalOrder(); }
/** * Returns {@code true} if {@code elements} is a {@code SortedSet} that uses * {@code comparator} to order its elements. Note that equivalent comparators * may still return {@code false}, if {@code equals} doesn't consider them * equal. If one comparator is {@code null} and the other is * {@link Comparators#naturalOrder()}, this method returns {@code true}. */ private static boolean hasSameComparator(Object elements, Comparator<?> comparator) { if (elements instanceof SortedSet) { SortedSet<?> sortedSet = (SortedSet<?>) elements; Comparator<?> comparator2 = sortedSet.comparator(); return Objects.equal(comparator2, comparator) || (comparator == null && comparator2 == Comparators.naturalOrder()) || (comparator2 == null && comparator == Comparators.naturalOrder()); } return false; }
/** * Returns an immutable sorted set containing the given elements sorted by * their natural ordering. When multiple elements are equivalent according to * {@code compareTo()}, only the first one specified is included. * * <p>The type specification is {@code <E extends Comparable>}, instead of the * more specific {@code <E extends Comparable<? super E>>}, to support * classes defined without generics. * * @throws NullPointerException if any of {@code elements} is null */ @SuppressWarnings("unchecked") public static <E extends Comparable> ImmutableSortedSet<E> copyOf( Iterator<? extends E> elements) { return copyOfInternal(Comparators.naturalOrder(), elements); }
/** * Returns an immutable sorted set containing the given elements sorted by * their natural ordering. When multiple elements are equivalent according to * {@link Comparable#compareTo}, only the first one specified is included. * * <p>The type specification is {@code <E extends Comparable>}, instead of the * more specific {@code <E extends Comparable<? super E>>}, to support * classes defined without generics. * * @throws NullPointerException if any of {@code elements} is null */ @SuppressWarnings("unchecked") // See method Javadoc public static <E extends Comparable> ImmutableSortedSet<E> of(E... elements) { return ofInternal(Comparators.naturalOrder(), elements); }
/** * Returns an immutable sorted set containing the given elements sorted by * their natural ordering. When multiple elements are equivalent according to * {@code compareTo()}, only the first one specified is included. To create a * copy of a {@code SortedSet} that preserves the comparator, call * {@link #copyOfSorted} instead. * * <p>Note that if {@code s} is a {@code Set<String>}, then * {@code ImmutableSortedSet.copyOf(s)} returns a * {@code ImmutableSortedSet<String>} containing each of the strings in * {@code s}, while {@code ImmutableSortedSet.of(s)} returns a * {@code ImmutableSortedSet<Set<String>>} containing one element (the given * set itself). * * <p><b>Note:</b> Despite what the method name suggests, if {@code elements} * is an {@code ImmutableSortedSet}, it may be returned instead of a copy. * * <p>The type specification is {@code <E extends Comparable>}, instead of the * more specific {@code <E extends Comparable<? super E>>}, to support * classes defined without generics. * * @throws NullPointerException if any of {@code elements} is null */ @SuppressWarnings("unchecked") public static <E extends Comparable> ImmutableSortedSet<E> copyOf( Iterable<? extends E> elements) { return copyOfInternal(Comparators.naturalOrder(), elements, false); }