< prev index next >
src/java.base/share/classes/java/util/Comparator.java
Print this page
@@ -21,18 +21,16 @@
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
-package java.util;
+package javany.util;
import java.io.Serializable;
-import java.util.function.Function;
-import java.util.function.ToIntFunction;
-import java.util.function.ToLongFunction;
-import java.util.function.ToDoubleFunction;
-import java.util.Comparators;
+import java.util.Objects;
+
+import javany.util.function.Function;
/**
* A comparison function, which imposes a <i>total ordering</i> on some
* collection of objects. Comparators can be passed to a sort method (such
* as {@link Collections#sort(List,Comparator) Collections.sort} or {@link
@@ -104,11 +102,11 @@
* @see Comparable
* @see java.io.Serializable
* @since 1.2
*/
@FunctionalInterface
-public interface Comparator<T> {
+public interface Comparator<any T> {
/**
* Compares its two arguments for order. Returns a negative integer,
* zero, or a positive integer as the first argument is less than, equal
* to, or greater than the second.<p>
*
@@ -180,11 +178,11 @@
* @return a comparator that imposes the reverse ordering of this
* comparator.
* @since 1.8
*/
default Comparator<T> reversed() {
- return Collections.reverseOrder(this);
+ return new Comparators.ReverseComparator<>(this);
}
/**
* Returns a lexicographic-order comparator with another comparator.
* If this {@code Comparator} considers two elements equal, i.e.
@@ -233,11 +231,11 @@
* @throws NullPointerException if either argument is null.
* @see #comparing(Function, Comparator)
* @see #thenComparing(Comparator)
* @since 1.8
*/
- default <U> Comparator<T> thenComparing(
+ default <any U> Comparator<T> thenComparing(
Function<? super T, ? extends U> keyExtractor,
Comparator<? super U> keyComparator)
{
return thenComparing(comparing(keyExtractor, keyComparator));
}
@@ -274,15 +272,15 @@
*
* @param keyExtractor the function used to extract the integer sort key
* @return a lexicographic-order comparator composed of this and then the
* {@code int} sort key
* @throws NullPointerException if the argument is null.
- * @see #comparingInt(ToIntFunction)
+ * @see #comparingInt(Function)
* @see #thenComparing(Comparator)
* @since 1.8
*/
- default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor) {
+ default Comparator<T> thenComparingInt(Function<? super T, int> keyExtractor) {
return thenComparing(comparingInt(keyExtractor));
}
/**
* Returns a lexicographic-order comparator with a function that
@@ -293,15 +291,15 @@
*
* @param keyExtractor the function used to extract the long sort key
* @return a lexicographic-order comparator composed of this and then the
* {@code long} sort key
* @throws NullPointerException if the argument is null.
- * @see #comparingLong(ToLongFunction)
+ * @see #comparingLong(Function)
* @see #thenComparing(Comparator)
* @since 1.8
*/
- default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) {
+ default Comparator<T> thenComparingLong(Function<? super T, long> keyExtractor) {
return thenComparing(comparingLong(keyExtractor));
}
/**
* Returns a lexicographic-order comparator with a function that
@@ -312,15 +310,15 @@
*
* @param keyExtractor the function used to extract the double sort key
* @return a lexicographic-order comparator composed of this and then the
* {@code double} sort key
* @throws NullPointerException if the argument is null.
- * @see #comparingDouble(ToDoubleFunction)
+ * @see #comparingDouble(Function)
* @see #thenComparing(Comparator)
* @since 1.8
*/
- default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) {
+ default Comparator<T> thenComparingDouble(Function<? super T, double> keyExtractor) {
return thenComparing(comparingDouble(keyExtractor));
}
/**
* Returns a comparator that imposes the reverse of the <em>natural
@@ -333,12 +331,12 @@
* @return a comparator that imposes the reverse of the <i>natural
* ordering</i> on {@code Comparable} objects.
* @see Comparable
* @since 1.8
*/
- public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() {
- return Collections.reverseOrder();
+ public static <any T> Comparator<T> reverseOrder() {
+ return Comparators.<T>naturalOrder().reversed();
}
/**
* Returns a comparator that compares {@link Comparable} objects in natural
* order.
@@ -351,12 +349,12 @@
* Comparable} objects.
* @see Comparable
* @since 1.8
*/
@SuppressWarnings("unchecked")
- public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
- return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE;
+ public static <any T> Comparator<T> naturalOrder() {
+ return Comparators.naturalOrder();
}
/**
* Returns a null-friendly comparator that considers {@code null} to be
* less than non-null. When both are {@code null}, they are considered
@@ -424,11 +422,11 @@
* @return a comparator that compares by an extracted key using the
* specified {@code Comparator}
* @throws NullPointerException if either argument is null
* @since 1.8
*/
- public static <T, U> Comparator<T> comparing(
+ public static <any T, any U> Comparator<T> comparing(
Function<? super T, ? extends U> keyExtractor,
Comparator<? super U> keyComparator)
{
Objects.requireNonNull(keyExtractor);
Objects.requireNonNull(keyComparator);
@@ -459,11 +457,11 @@
* Comparable} sort key
* @return a comparator that compares by an extracted key
* @throws NullPointerException if the argument is null
* @since 1.8
*/
- public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
+ public static <any T, U extends Comparable<? super U>> Comparator<T> comparing(
Function<? super T, ? extends U> keyExtractor)
{
Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable)
(c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
@@ -482,14 +480,14 @@
* @return a comparator that compares by an extracted key
* @see #comparing(Function)
* @throws NullPointerException if the argument is null
* @since 1.8
*/
- public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) {
+ public static <any T> Comparator<T> comparingInt(Function<? super T, int> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable)
- (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2));
+ (c1, c2) -> Integer.compare(keyExtractor.apply(c1), keyExtractor.apply(c2));
}
/**
* Accepts a function that extracts a {@code long} sort key from a type
* {@code T}, and returns a {@code Comparator<T>} that compares by that
@@ -503,14 +501,14 @@
* @return a comparator that compares by an extracted key
* @see #comparing(Function)
* @throws NullPointerException if the argument is null
* @since 1.8
*/
- public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor) {
+ public static <any T> Comparator<T> comparingLong(Function<? super T, long> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable)
- (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2));
+ (c1, c2) -> Long.compare(keyExtractor.apply(c1), keyExtractor.apply(c2));
}
/**
* Accepts a function that extracts a {@code double} sort key from a type
* {@code T}, and returns a {@code Comparator<T>} that compares by that
@@ -524,11 +522,11 @@
* @return a comparator that compares by an extracted key
* @see #comparing(Function)
* @throws NullPointerException if the argument is null
* @since 1.8
*/
- public static<T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) {
+ public static <any T> Comparator<T> comparingDouble(Function<? super T, double> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable)
- (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2));
+ (c1, c2) -> Double.compare(keyExtractor.apply(c1), keyExtractor.apply(c2));
}
}
< prev index next >