Skip to content

Commit

Permalink
Add Comparators.min/max
Browse files Browse the repository at this point in the history
RELNOTES=`collect`: Added two-element min and max methods to Comparators.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=315365436
  • Loading branch information
herbyderby authored and kluever committed Jun 9, 2020
1 parent 2d34685 commit 958186c
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 0 deletions.
Expand Up @@ -16,6 +16,7 @@

package com.google.common.collect;

import static com.google.common.truth.Truth.assertThat;
import static java.util.Arrays.asList;

import com.google.common.annotations.GwtCompatible;
Expand Down Expand Up @@ -71,4 +72,59 @@ public void testIsInStrictOrder() {
assertTrue(Comparators.isInStrictOrder(Collections.singleton(1), Ordering.natural()));
assertTrue(Comparators.isInStrictOrder(Collections.<Integer>emptyList(), Ordering.natural()));
}

public void testMinMaxNatural() {
assertThat(Comparators.min(1, 2)).isEqualTo(1);
assertThat(Comparators.min(2, 1)).isEqualTo(1);
assertThat(Comparators.max(1, 2)).isEqualTo(2);
assertThat(Comparators.max(2, 1)).isEqualTo(2);
}

public void testMinMaxNatural_equalInstances() {
Foo a = new Foo(1);
Foo b = new Foo(1);
assertThat(Comparators.min(a, b)).isSameInstanceAs(a);
assertThat(Comparators.max(a, b)).isSameInstanceAs(a);
}

public void testMinMaxComparator() {
Comparator<Integer> natural = Ordering.natural();
Comparator<Integer> reverse = Collections.reverseOrder(natural);
assertThat(Comparators.min(1, 2, reverse)).isEqualTo(2);
assertThat(Comparators.min(2, 1, reverse)).isEqualTo(2);
assertThat(Comparators.max(1, 2, reverse)).isEqualTo(1);
assertThat(Comparators.max(2, 1, reverse)).isEqualTo(1);
}

public void testMinMaxComparator_equalInstances() {
Comparator<Foo> natural = Ordering.natural();
Comparator<Foo> reverse = Collections.reverseOrder(natural);
Foo a = new Foo(1);
Foo b = new Foo(1);
assertThat(Comparators.min(a, b, reverse)).isSameInstanceAs(a);
assertThat(Comparators.max(a, b, reverse)).isSameInstanceAs(a);
}

private static class Foo implements Comparable<Foo> {
final Integer value;

Foo(int value) {
this.value = value;
}

@Override
public int hashCode() {
return value.hashCode();
}

@Override
public boolean equals(Object o) {
return (o instanceof Foo) && ((Foo) o).value.equals(value);
}

@Override
public int compareTo(Foo other) {
return value.compareTo(other.value);
}
}
}
69 changes: 69 additions & 0 deletions android/guava/src/com/google/common/collect/Comparators.java
Expand Up @@ -22,6 +22,7 @@
import com.google.common.annotations.GwtCompatible;
import java.util.Comparator;
import java.util.Iterator;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

/**
* Provides static methods for working with {@link Comparator} instances. For many other helpful
Expand Down Expand Up @@ -103,4 +104,72 @@ public static <T> boolean isInStrictOrder(
}
return true;
}

/**
* Returns the minimum of the two values. If the values compare as 0, the first is returned.
*
* <p>To find the minimum of more than two values, use {@code Collections.min(Arrays.asList(a, b,
* c))} (static imports recommended).
*
* @param a first value to compare, returned if less than or equal to b.
* @param b second value to compare.
* @throws ClassCastException if the parameters are not <i>mutually comparable</i>.
* @since NEXT
*/
@Beta
public static <T extends Comparable<? super T>> T min(T a, T b) {
return (a.compareTo(b) <= 0) ? a : b;
}

/**
* Returns the minimum of the two values, according to the given comparator. If the values compare
* as equal, the first is returned.
*
* <p>To find the minimum of more than two values, use {@code Collections.min(Arrays.asList(a, b,
* c), comparator)} (static imports recommended).
*
* @param a first value to compare, returned if less than or equal to b
* @param b second value to compare.
* @throws ClassCastException if the parameters are not <i>mutually comparable</i> using the given
* comparator.
* @since NEXT
*/
@Beta
public static <T> T min(@NullableDecl T a, @NullableDecl T b, Comparator<T> comparator) {
return (comparator.compare(a, b) <= 0) ? a : b;
}

/**
* Returns the maximum of the two values. If the values compare as 0, the first is returned.
*
* <p>To find the maximum of more than two values, use {@code Collections.max(Arrays.asList(a, b,
* c))} (static imports recommended).
*
* @param a first value to compare, returned if greater than or equal to b.
* @param b second value to compare.
* @throws ClassCastException if the parameters are not <i>mutually comparable</i>.
* @since NEXT
*/
@Beta
public static <T extends Comparable<? super T>> T max(T a, T b) {
return (a.compareTo(b) >= 0) ? a : b;
}

/**
* Returns the maximum of the two values, according to the given comparator. If the values compare
* as equal, the first is returned.
*
* <p>To find the maximum of more than two values, use {@code Collections.max(Arrays.asList(a, b,
* c), comparator)} (static imports recommended).
*
* @param a first value to compare, returned if greater than or equal to b.
* @param b second value to compare.
* @throws ClassCastException if the parameters are not <i>mutually comparable</i> using the given
* comparator.
* @since NEXT
*/
@Beta
public static <T> T max(@NullableDecl T a, @NullableDecl T b, Comparator<T> comparator) {
return (comparator.compare(a, b) >= 0) ? a : b;
}
}
20 changes: 20 additions & 0 deletions guava-gwt/test/com/google/common/collect/ComparatorsTest_gwt.java
Expand Up @@ -52,4 +52,24 @@ public void testLexicographical() throws Exception {
com.google.common.collect.ComparatorsTest testCase = new com.google.common.collect.ComparatorsTest();
testCase.testLexicographical();
}

public void testMinMaxComparator() throws Exception {
com.google.common.collect.ComparatorsTest testCase = new com.google.common.collect.ComparatorsTest();
testCase.testMinMaxComparator();
}

public void testMinMaxComparator_equalInstances() throws Exception {
com.google.common.collect.ComparatorsTest testCase = new com.google.common.collect.ComparatorsTest();
testCase.testMinMaxComparator_equalInstances();
}

public void testMinMaxNatural() throws Exception {
com.google.common.collect.ComparatorsTest testCase = new com.google.common.collect.ComparatorsTest();
testCase.testMinMaxNatural();
}

public void testMinMaxNatural_equalInstances() throws Exception {
com.google.common.collect.ComparatorsTest testCase = new com.google.common.collect.ComparatorsTest();
testCase.testMinMaxNatural_equalInstances();
}
}
56 changes: 56 additions & 0 deletions guava-tests/test/com/google/common/collect/ComparatorsTest.java
Expand Up @@ -16,6 +16,7 @@

package com.google.common.collect;

import static com.google.common.truth.Truth.assertThat;
import static java.util.Arrays.asList;
import static java.util.Comparator.comparing;
import static java.util.Comparator.naturalOrder;
Expand Down Expand Up @@ -114,4 +115,59 @@ public void testEmptiesLast() {
// Just demonstrate that no explicit type parameter is required
comparator = Comparators.emptiesLast(naturalOrder());
}

public void testMinMaxNatural() {
assertThat(Comparators.min(1, 2)).isEqualTo(1);
assertThat(Comparators.min(2, 1)).isEqualTo(1);
assertThat(Comparators.max(1, 2)).isEqualTo(2);
assertThat(Comparators.max(2, 1)).isEqualTo(2);
}

public void testMinMaxNatural_equalInstances() {
Foo a = new Foo(1);
Foo b = new Foo(1);
assertThat(Comparators.min(a, b)).isSameInstanceAs(a);
assertThat(Comparators.max(a, b)).isSameInstanceAs(a);
}

public void testMinMaxComparator() {
Comparator<Integer> natural = Ordering.natural();
Comparator<Integer> reverse = Collections.reverseOrder(natural);
assertThat(Comparators.min(1, 2, reverse)).isEqualTo(2);
assertThat(Comparators.min(2, 1, reverse)).isEqualTo(2);
assertThat(Comparators.max(1, 2, reverse)).isEqualTo(1);
assertThat(Comparators.max(2, 1, reverse)).isEqualTo(1);
}

public void testMinMaxComparator_equalInstances() {
Comparator<Foo> natural = Ordering.natural();
Comparator<Foo> reverse = Collections.reverseOrder(natural);
Foo a = new Foo(1);
Foo b = new Foo(1);
assertThat(Comparators.min(a, b, reverse)).isSameInstanceAs(a);
assertThat(Comparators.max(a, b, reverse)).isSameInstanceAs(a);
}

private static class Foo implements Comparable<Foo> {
final Integer value;

Foo(int value) {
this.value = value;
}

@Override
public int hashCode() {
return value.hashCode();
}

@Override
public boolean equals(Object o) {
return (o instanceof Foo) && ((Foo) o).value.equals(value);
}

@Override
public int compareTo(Foo other) {
return value.compareTo(other.value);
}
}
}
69 changes: 69 additions & 0 deletions guava/src/com/google/common/collect/Comparators.java
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Optional;
import java.util.stream.Collector;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Provides static methods for working with {@link Comparator} instances. For many other helpful
Expand Down Expand Up @@ -190,4 +191,72 @@ public static <T> Comparator<Optional<T>> emptiesLast(Comparator<? super T> valu
checkNotNull(valueComparator);
return Comparator.comparing(o -> o.orElse(null), Comparator.nullsLast(valueComparator));
}

/**
* Returns the minimum of the two values. If the values compare as 0, the first is returned.
*
* <p>To find the minimum of more than two values, use {@code Collections.min(Arrays.asList(a, b,
* c))} (static imports recommended).
*
* @param a first value to compare, returned if less than or equal to b.
* @param b second value to compare.
* @throws ClassCastException if the parameters are not <i>mutually comparable</i>.
* @since NEXT
*/
@Beta
public static <T extends Comparable<? super T>> T min(T a, T b) {
return (a.compareTo(b) <= 0) ? a : b;
}

/**
* Returns the minimum of the two values, according to the given comparator. If the values compare
* as equal, the first is returned.
*
* <p>To find the minimum of more than two values, use {@code Collections.min(Arrays.asList(a, b,
* c), comparator)} (static imports recommended).
*
* @param a first value to compare, returned if less than or equal to b
* @param b second value to compare.
* @throws ClassCastException if the parameters are not <i>mutually comparable</i> using the given
* comparator.
* @since NEXT
*/
@Beta
public static <T> T min(@Nullable T a, @Nullable T b, Comparator<T> comparator) {
return (comparator.compare(a, b) <= 0) ? a : b;
}

/**
* Returns the maximum of the two values. If the values compare as 0, the first is returned.
*
* <p>To find the maximum of more than two values, use {@code Collections.max(Arrays.asList(a, b,
* c))} (static imports recommended).
*
* @param a first value to compare, returned if greater than or equal to b.
* @param b second value to compare.
* @throws ClassCastException if the parameters are not <i>mutually comparable</i>.
* @since NEXT
*/
@Beta
public static <T extends Comparable<? super T>> T max(T a, T b) {
return (a.compareTo(b) >= 0) ? a : b;
}

/**
* Returns the maximum of the two values, according to the given comparator. If the values compare
* as equal, the first is returned.
*
* <p>To find the maximum of more than two values, use {@code Collections.max(Arrays.asList(a, b,
* c), comparator)} (static imports recommended).
*
* @param a first value to compare, returned if greater than or equal to b.
* @param b second value to compare.
* @throws ClassCastException if the parameters are not <i>mutually comparable</i> using the given
* comparator.
* @since NEXT
*/
@Beta
public static <T> T max(@Nullable T a, @Nullable T b, Comparator<T> comparator) {
return (comparator.compare(a, b) >= 0) ? a : b;
}
}

0 comments on commit 958186c

Please sign in to comment.