jdk/src/share/classes/java/util/function/BinaryOperator.java
changeset 18571 8e3cb3c46ae8
parent 16036 a30224365db2
child 18584 430628bf6412
--- a/jdk/src/share/classes/java/util/function/BinaryOperator.java	Thu Jun 27 19:22:51 2013 -0700
+++ b/jdk/src/share/classes/java/util/function/BinaryOperator.java	Tue Jun 11 13:41:38 2013 -0700
@@ -24,6 +24,9 @@
  */
 package java.util.function;
 
+import java.util.Objects;
+import java.util.Comparator;
+
 /**
  * An operation upon two operands yielding a result. This is a specialization of
  * {@code BiFunction} where the operands and the result are all of the same type.
@@ -35,4 +38,31 @@
  */
 @FunctionalInterface
 public interface BinaryOperator<T> extends BiFunction<T,T,T> {
+    /**
+     * Returns a {@link BinaryOperator} which returns the lesser of two elements
+     * according to the specified {@code Comparator}
+     *
+     * @param  comparator a {@code Comparator} for comparing the two values
+     * @return a {@code BinaryOperator} which returns the lesser of its operands,
+     *         according to the supplied {@code Comparator}
+     * @throws NullPointerException if the argument is null
+     */
+    public static<T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
+        Objects.requireNonNull(comparator);
+        return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
+    }
+
+    /**
+     * Returns a {@link BinaryOperator} which returns the greater of two elements
+     * according to the specified {@code Comparator}
+     *
+     * @param  comparator a {@code Comparator} for comparing the two values
+     * @return a {@code BinaryOperator} which returns the greater of its operands,
+     *         according to the supplied {@code Comparator}
+     * @throws NullPointerException if the argument is null
+     */
+    public static<T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
+        Objects.requireNonNull(comparator);
+        return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
+    }
 }