-
Notifications
You must be signed in to change notification settings - Fork 5.9k
8356995: Provide default methods min(T, T) and max(T, T) in Comparator interface #25297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
d72c0e8
2cda59b
9edc73e
f6fc4a9
b750c3f
38f9eb7
293b2c4
a278865
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -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 <i>total ordering</i> on | ||||||
|
@@ -189,6 +188,42 @@ default Comparator<T> 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 b} argument is returned. | ||||||
* | ||||||
* @param a an argument. | ||||||
* @param b another argument. | ||||||
* @return the larger of {@code a} and {@code b} according to this comparator. | ||||||
* @throws ClassCastException if the collection contains elements that are | ||||||
* not <i>mutually comparable</i> (for example, strings and | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I copied this sentence from |
||||||
* integers). | ||||||
* | ||||||
* @since 25 | ||||||
*/ | ||||||
default T max(T a, T b) { | ||||||
return compare(a, b) > 0 ? a : b; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Returns the smaller of two values according to this comparator. | ||||||
* If the arguments are equal with respect to this comparator, | ||||||
* the {@code a} argument is returned. | ||||||
* | ||||||
* @param a an argument. | ||||||
* @param b another argument. | ||||||
* @return the smaller of {@code a} and {@code b} according to this comparator. | ||||||
* @throws ClassCastException if the collection contains elements that are | ||||||
* not <i>mutually comparable</i> (for example, strings and | ||||||
* integers). | ||||||
* | ||||||
* @since 25 | ||||||
*/ | ||||||
default T min(T a, T b) { | ||||||
return compare(a, b) > 0 ? b : a; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Returns a lexicographic-order comparator with another comparator. | ||||||
* If this {@code Comparator} considers two elements equal, i.e. | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,74 @@ | ||||||
/* | ||||||
* 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 testng MinMaxTest | ||||||
*/ | ||||||
|
||||||
import org.testng.annotations.Test; | ||||||
|
||||||
import java.util.Comparator; | ||||||
import static org.testng.Assert.*; | ||||||
|
||||||
@Test(groups = "unit") | ||||||
public class MinMaxTest { | ||||||
public void testMin() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to this, test methods do not need to be
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thanks |
||||||
Comparator<String> c = Comparator.naturalOrder(); | ||||||
assertEquals(c.min("a", "b"), "a"); | ||||||
assertEquals(c.min("b", "a"), "a"); | ||||||
} | ||||||
|
||||||
public void testMax() { | ||||||
Comparator<String> c = Comparator.naturalOrder(); | ||||||
assertEquals(c.max("a", "b"), "b"); | ||||||
assertEquals(c.max("b", "a"), "b"); | ||||||
} | ||||||
|
||||||
public void testThrowsNPE() { | ||||||
Comparator<String> 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)); | ||||||
} | ||||||
|
||||||
public void testThrowsCCE() { | ||||||
@SuppressWarnings("unchecked") | ||||||
Comparator<Object> c = (Comparator<Object>) (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)); | ||||||
} | ||||||
|
||||||
public void testEqualReturnFirstOrLast() { | ||||||
Comparator<Object> allEqual = (_, _) -> 0; | ||||||
Object o1 = new Object(); | ||||||
Object o2 = new Object(); | ||||||
assertSame(allEqual.min(o1, o2), o1); | ||||||
assertSame(allEqual.max(o1, o2), o2); | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What 'collection' meaned here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thank you. The description was copied from
Collections::max
, which is not so appropriate here. I think it's better to copy it fromComparator::compare
(also, added@throws NPE
for consistency).