Skip to content

Commit

Permalink
Make Comparators.max/min return the type of the comparands, even …
Browse files Browse the repository at this point in the history
…if the `Comparator` accepts a more general type.

RELNOTES=n/a
PiperOrigin-RevId: 623747164
  • Loading branch information
java-team-github-bot authored and Google Java Core Libraries committed Apr 11, 2024
1 parent 983b451 commit 75de44c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ public void testMinMaxComparator() {
assertThat(Comparators.max(2, 1, reverse)).isEqualTo(1);
}

/**
* Fails compilation if the signature of min and max is changed to take {@code Comparator<T>}
* instead of {@code Comparator<? super T>}.
*/
public void testMinMaxWithSupertypeComparator() {
Comparator<Number> numberComparator =
// Can't use Comparator.comparing(Number::intValue) due to Java 7 compatibility.
new Comparator<Number>() {
@Override
public int compare(Number a, Number b) {
return a.intValue() - b.intValue();
}
};
Integer comparand1 = 1;
Integer comparand2 = 2;

Integer min = Comparators.min(comparand1, comparand2, numberComparator);
Integer max = Comparators.max(comparand1, comparand2, numberComparator);

assertThat(min).isEqualTo(1);
assertThat(max).isEqualTo(2);
}

public void testMinMaxComparator_equalInstances() {
Comparator<Foo> natural = Ordering.natural();
Comparator<Foo> reverse = Collections.reverseOrder(natural);
Expand Down
4 changes: 2 additions & 2 deletions android/guava/src/com/google/common/collect/Comparators.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static <T extends Comparable<? super T>> T min(T a, T b) {
*/
@ParametricNullness
public static <T extends @Nullable Object> T min(
@ParametricNullness T a, @ParametricNullness T b, Comparator<T> comparator) {
@ParametricNullness T a, @ParametricNullness T b, Comparator<? super T> comparator) {
return (comparator.compare(a, b) <= 0) ? a : b;
}

Expand Down Expand Up @@ -240,7 +240,7 @@ public static <T extends Comparable<? super T>> T max(T a, T b) {
*/
@ParametricNullness
public static <T extends @Nullable Object> T max(
@ParametricNullness T a, @ParametricNullness T b, Comparator<T> comparator) {
@ParametricNullness T a, @ParametricNullness T b, Comparator<? super T> comparator) {
return (comparator.compare(a, b) >= 0) ? a : b;
}
}
23 changes: 23 additions & 0 deletions guava-tests/test/com/google/common/collect/ComparatorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,29 @@ public void testMinMaxComparator() {
assertThat(Comparators.max(2, 1, reverse)).isEqualTo(1);
}

/**
* Fails compilation if the signature of min and max is changed to take {@code Comparator<T>}
* instead of {@code Comparator<? super T>}.
*/
public void testMinMaxWithSupertypeComparator() {
Comparator<Number> numberComparator =
// Can't use Comparator.comparing(Number::intValue) due to Java 7 compatibility.
new Comparator<Number>() {
@Override
public int compare(Number a, Number b) {
return a.intValue() - b.intValue();
}
};
Integer comparand1 = 1;
Integer comparand2 = 2;

Integer min = Comparators.min(comparand1, comparand2, numberComparator);
Integer max = Comparators.max(comparand1, comparand2, numberComparator);

assertThat(min).isEqualTo(1);
assertThat(max).isEqualTo(2);
}

public void testMinMaxComparator_equalInstances() {
Comparator<Foo> natural = Ordering.natural();
Comparator<Foo> reverse = Collections.reverseOrder(natural);
Expand Down
4 changes: 2 additions & 2 deletions guava/src/com/google/common/collect/Comparators.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public static <T extends Comparable<? super T>> T min(T a, T b) {
*/
@ParametricNullness
public static <T extends @Nullable Object> T min(
@ParametricNullness T a, @ParametricNullness T b, Comparator<T> comparator) {
@ParametricNullness T a, @ParametricNullness T b, Comparator<? super T> comparator) {
return (comparator.compare(a, b) <= 0) ? a : b;
}

Expand Down Expand Up @@ -265,7 +265,7 @@ public static <T extends Comparable<? super T>> T max(T a, T b) {
*/
@ParametricNullness
public static <T extends @Nullable Object> T max(
@ParametricNullness T a, @ParametricNullness T b, Comparator<T> comparator) {
@ParametricNullness T a, @ParametricNullness T b, Comparator<? super T> comparator) {
return (comparator.compare(a, b) >= 0) ? a : b;
}
}

0 comments on commit 75de44c

Please sign in to comment.