src/share/classes/java/util/Comparators.java

Print this page
rev 6853 : 8010279: java.util.Stream.min/max((Comparator)null) is not consistent in throwing (unspecified) NPE


 244      */
 245     public static<T> Comparator<T> compose(Comparator<? super T> first, Comparator<? super T> second) {
 246         Objects.requireNonNull(first);
 247         Objects.requireNonNull(second);
 248         return (Comparator<T> & Serializable) (c1, c2) -> {
 249             int res = first.compare(c1, c2);
 250             return (res != 0) ? res : second.compare(c1, c2);
 251         };
 252     }
 253 
 254     /**
 255      * Constructs a {@link BinaryOperator} which returns the lesser of two elements
 256      * according to the specified {@code Comparator}
 257      *
 258      * @param comparator A {@code Comparator} for comparing the two values
 259      * @param <T> the type of the elements to be compared
 260      * @return a {@code BinaryOperator} which returns the lesser of its operands,
 261      * according to the supplied {@code Comparator}
 262      */
 263     public static<T> BinaryOperator<T> lesserOf(Comparator<? super T> comparator) {

 264         return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
 265     }
 266 
 267     /**
 268      * Constructs a {@link BinaryOperator} which returns the greater of two elements
 269      * according to the specified {@code Comparator}
 270      *
 271      * @param comparator A {@code Comparator} for comparing the two values
 272      * @param <T> the type of the elements to be compared
 273      * @return a {@code BinaryOperator} which returns the greater of its operands,
 274      * according to the supplied {@code Comparator}
 275      */
 276     public static<T> BinaryOperator<T> greaterOf(Comparator<? super T> comparator) {

 277         return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
 278     }
 279 }


 244      */
 245     public static<T> Comparator<T> compose(Comparator<? super T> first, Comparator<? super T> second) {
 246         Objects.requireNonNull(first);
 247         Objects.requireNonNull(second);
 248         return (Comparator<T> & Serializable) (c1, c2) -> {
 249             int res = first.compare(c1, c2);
 250             return (res != 0) ? res : second.compare(c1, c2);
 251         };
 252     }
 253 
 254     /**
 255      * Constructs a {@link BinaryOperator} which returns the lesser of two elements
 256      * according to the specified {@code Comparator}
 257      *
 258      * @param comparator A {@code Comparator} for comparing the two values
 259      * @param <T> the type of the elements to be compared
 260      * @return a {@code BinaryOperator} which returns the lesser of its operands,
 261      * according to the supplied {@code Comparator}
 262      */
 263     public static<T> BinaryOperator<T> lesserOf(Comparator<? super T> comparator) {
 264         Objects.requireNonNull(comparator);
 265         return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
 266     }
 267 
 268     /**
 269      * Constructs a {@link BinaryOperator} which returns the greater of two elements
 270      * according to the specified {@code Comparator}
 271      *
 272      * @param comparator A {@code Comparator} for comparing the two values
 273      * @param <T> the type of the elements to be compared
 274      * @return a {@code BinaryOperator} which returns the greater of its operands,
 275      * according to the supplied {@code Comparator}
 276      */
 277     public static<T> BinaryOperator<T> greaterOf(Comparator<? super T> comparator) {
 278         Objects.requireNonNull(comparator);
 279         return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
 280     }
 281 }