Skip to content

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

amaembo
Copy link
Contributor

@amaembo amaembo commented May 19, 2025

Implementation of Comparator.min and Comparator.max methods. Preliminary discussion is in this thread:
https://mail.openjdk.org/pipermail/core-libs-dev/2025-May/145638.html
The specification is mostly composed of Math.min/max and Collections.min/max specifications.

The methods are quite trivial, so I don't think we need more extensive testing (e.g., using different comparators). But if you have ideas of new useful tests, I'll gladly add them.

I'm not sure whether we should specify exactly the behavior in case if the comparator returns 0. I feel that it could be a useful invariant that Comparator.min(a, b) and Comparator.max(a, b) always return different argument, partitioning the set of {a, b} objects (even if they are equal). But I'm open to suggestions here.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed (2 reviews required, with at least 2 Reviewers)
  • Change requires a CSR request matching fixVersion 25 to be approved (needs to be created)

Issue

  • JDK-8356995: Provide default methods min(T, T) and max(T, T) in Comparator interface (Enhancement - P4)(⚠️ The fixVersion in this issue is [26] but the fixVersion in .jcheck/conf is 25, a new backport will be created when this pr is integrated.)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/25297/head:pull/25297
$ git checkout pull/25297

Update a local copy of the PR:
$ git checkout pull/25297
$ git pull https://git.openjdk.org/jdk.git pull/25297/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25297

View PR using the GUI difftool:
$ git pr show -t 25297

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/25297.diff

Using Webrev

Link to Webrev Comment

@amaembo
Copy link
Contributor Author

amaembo commented May 19, 2025

/csr

@bridgekeeper
Copy link

bridgekeeper bot commented May 19, 2025

👋 Welcome back tvaleev! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented May 19, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added the csr Pull request needs approved CSR before integration label May 19, 2025
@openjdk
Copy link

openjdk bot commented May 19, 2025

@amaembo has indicated that a compatibility and specification (CSR) request is needed for this pull request.

@amaembo please create a CSR request for issue JDK-8356995 with the correct fix version. This pull request cannot be integrated until the CSR request is approved.

@openjdk
Copy link

openjdk bot commented May 19, 2025

@amaembo The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the rfr Pull request is ready for review label May 19, 2025
@mlbridge
Copy link

mlbridge bot commented May 19, 2025

Webrevs

@archiecobbs
Copy link
Contributor

I'm not sure whether we should specify exactly the behavior in case if the comparator returns 0. I feel that it could be a useful invariant that Comparator.min(a, b) and Comparator.max(a, b) always return different argument, partitioning the set of {a, b} objects (even if they are equal). But I'm open to suggestions here.

IMHO it makes sense. It's the min/max analog to a stable sort.

@rgiulietti
Copy link
Contributor

@amaembo Could you please switch the test class to make use of JUnit? For new tests we now prefer JUnit over TestNG.
Thanks!

Copy link
Contributor

@RogerRiggs RogerRiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Min and Max for the equals case would be more consistent if the value returned for equals was the same as the condition, as if => or <= were used for the comparison.
The code would be easier to read and match the description if max used >= and min used <=.

@amaembo
Copy link
Contributor Author

amaembo commented May 21, 2025

@rgiulietti I tried to mimic the nearby tests which use testng. Converted to junit 5.

@archiecobbs

IMHO it makes sense. It's the min/max analog to a stable sort

On the second thought, there is a consistency argument. We already have BinaryOperator.minBy and BinaryOperator.maxBy, which always return the first argument in case of tie (though this is not specified, probably it should be?). So it looks like it will be better to have both APIs consistent. One more point is that Guava's Ordering (which extends Comparator) also returns the first argument in case of tie, so if we do the same, Ordering will not violate the Comparator contract. Thus I've changed the implementation to be in sync with both BinaryOperator and Ordering. This also addresses the @RogerRiggs comment: now implementations use >= and <=.

Copy link
Contributor

@rgiulietti rgiulietti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of nits.

* @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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<em> is more intent revealing.

Suggested change
* not <i>mutually comparable</i> (for example, strings and
* not <em>mutually comparable</em> (for example, strings and

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this sentence from java.util.Collections where <i> is used 48 times. I'll replace it here with <em>, but probably it should be updated en-masse for consistency?


public class MinMaxTest {
@Test
public void testMin() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this, test methods do not need to be public (but cannot be private).

Suggested change
public void testMin() {
void testMin() {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks

@plevart
Copy link
Contributor

plevart commented May 21, 2025

Hi,

There exists a class implementing Comparator and min and max methods too. But they are generic:

https://github.com/google/guava/blob/f1b962f1f1d1c96068c62b7ff55426ea249cbd55/guava/src/com/google/common/collect/Ordering.java#L622C10-L622C79

  public <E extends T> E max(E a, E b) {
    return (compare(a, b) >= 0) ? a : b;
  }

So when I have:

Comparator<CharSequence> cmp = ...

var greatest = cmp.max("foo", "bar");

...then the type of greatest would be String and not CharSequence.

Copy link
Contributor

@RogerRiggs RogerRiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks

@plevart
Copy link
Contributor

plevart commented May 21, 2025

What I was trying to say is that not only would Guava's Ordering not compile any more (it would still be binary compatible I think, as both methods have same erasure), but I also think that generic max and min as defined by Guava's Ordering are more useful too. So what do you think of making them generic like this:

    default <E extends T> E max(E e1, E e2) {
        return compare(e1, e2) >= 0 ? e1 : e2;
    }
    
    default <E extends T> E min(E e1, E e2) {
        return compare(e1, e2) <= 0 ? e1 : e2;
    }

@RogerRiggs
Copy link
Contributor

/reviewers 2 reviewer

@openjdk
Copy link

openjdk bot commented May 21, 2025

@RogerRiggs
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 2 Reviewers).

@amaembo
Copy link
Contributor Author

amaembo commented May 22, 2025

@plevart this is definitely a good idea, and it will make the methods more convenient. I used U as a new type parameter, because it looks like it's more common in the OpenJDK codebase (e.g., StructuredTaskScope::fork). Now, Guava should be source-compatible, though it's possible that some other libraries declare non-generic min/max and they will not be source-compatible anymore. I'll mention this in CSR.

*
* @since 25
*/
default <U extends T> U max(U a, U b) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter names are o1 and o2 in the compare method min and max build on.
Though a and b are used in the class javadoc example and x and y are used in the spec description.
Can we be consistent in the API? (o1, o2) perhaps. Someday, the parameter names may be more significant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks.

* @param b another argument.
* @param <U> the type of the arguments and the result.
* @return the larger of {@code a} and {@code b} according to this comparator.
* @throws ClassCastException if the collection contains elements that are
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What 'collection' meaned here?

Copy link
Contributor Author

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 from Comparator::compare (also, added @throws NPE for consistency).

Copy link
Contributor

@RogerRiggs RogerRiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed the PR and CSR. Thanks for the updates. Looks good.
Set the fixVersion to 25 and Finalize or at least Proposed to start the CSR review.

@amaembo
Copy link
Contributor Author

amaembo commented May 27, 2025

@RogerRiggs thank you! I always forget that CSR should be moved forward by its author...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs [email protected] csr Pull request needs approved CSR before integration rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

6 participants