diff --git a/src/java.base/share/classes/java/util/Comparator.java b/src/java.base/share/classes/java/util/Comparator.java index 6e0420d26e886..5f5233b0dd343 100644 --- a/src/java.base/share/classes/java/util/Comparator.java +++ b/src/java.base/share/classes/java/util/Comparator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,7 +30,6 @@ import java.util.function.ToIntFunction; import java.util.function.ToLongFunction; import java.util.function.ToDoubleFunction; -import java.util.Comparators; /** * A comparison function, which imposes a total ordering on @@ -189,6 +188,46 @@ default Comparator reversed() { return Collections.reverseOrder(this); } + /** + * Returns the greater of two values according to this comparator. + * If the arguments are equal with respect to this comparator, + * the {@code o1} argument is returned. + * + * @param o1 an argument. + * @param o2 another argument. + * @param the type of the arguments and the result. + * @return the larger of {@code o1} and {@code o2} according to this comparator. + * @throws NullPointerException if an argument is null and this + * comparator does not permit null arguments + * @throws ClassCastException if the arguments' types prevent them from + * being compared by this comparator. + * + * @since 26 + */ + default U max(U o1, U o2) { + return compare(o1, o2) >= 0 ? o1 : o2; + } + + /** + * Returns the smaller of two values according to this comparator. + * If the arguments are equal with respect to this comparator, + * the {@code o1} argument is returned. + * + * @param o1 an argument. + * @param o2 another argument. + * @param the type of the arguments and the result. + * @return the smaller of {@code o1} and {@code o2} according to this comparator. + * @throws NullPointerException if an argument is null and this + * comparator does not permit null arguments + * @throws ClassCastException if the arguments' types prevent them from + * being compared by this comparator. + * + * @since 26 + */ + default U min(U o1, U o2) { + return compare(o1, o2) <= 0 ? o1 : o2; + } + /** * Returns a lexicographic-order comparator with another comparator. * If this {@code Comparator} considers two elements equal, i.e. diff --git a/test/jdk/java/util/Comparator/MinMaxTest.java b/test/jdk/java/util/Comparator/MinMaxTest.java new file mode 100644 index 0000000000000..f1ef7d2d16475 --- /dev/null +++ b/test/jdk/java/util/Comparator/MinMaxTest.java @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * 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. + */ + +/** + * @test + * @bug 8356995 + * @summary Comparator min/max method tests + * @run junit MinMaxTest + */ + +import org.junit.jupiter.api.Test; + +import java.util.Comparator; + +import static org.junit.jupiter.api.Assertions.*; + +public class MinMaxTest { + @Test + void testMin() { + Comparator c = Comparator.naturalOrder(); + assertEquals("a", c.min("a", "b")); + assertEquals("a", c.min("b", "a")); + } + + @Test + void testMax() { + Comparator c = Comparator.naturalOrder(); + assertEquals("b", c.max("a", "b")); + assertEquals("b", c.max("b", "a")); + } + + @Test + void testThrowsNPE() { + Comparator c = Comparator.naturalOrder(); + assertThrows(NullPointerException.class, () -> c.min(null, "a")); + assertThrows(NullPointerException.class, () -> c.min("a", null)); + assertThrows(NullPointerException.class, () -> c.max(null, "a")); + assertThrows(NullPointerException.class, () -> c.max("a", null)); + } + + @Test + void testThrowsCCE() { + @SuppressWarnings("unchecked") + Comparator c = (Comparator) (Comparator)Comparator.naturalOrder(); + assertThrows(ClassCastException.class, () -> c.min(1, "a")); + assertThrows(ClassCastException.class, () -> c.min("a", 1)); + assertThrows(ClassCastException.class, () -> c.max(1, "a")); + assertThrows(ClassCastException.class, () -> c.max("a", 1)); + } + + @Test + void testEqualReturnFirst() { + Comparator allEqual = (_, _) -> 0; + Object o1 = new Object(); + Object o2 = new Object(); + assertSame(o1, allEqual.min(o1, o2)); + assertSame(o1, allEqual.max(o1, o2)); + } + + @Test + void testComparatorSubtype() { + Comparator byLength = Comparator.comparing(CharSequence::length); + String s1 = "long_string"; + String s2 = "short"; + String min = byLength.min(s1, s2); + String max = byLength.max(s1, s2); + assertEquals(s1, max); + assertEquals(s2, min); + } +}