Skip to content

Commit

Permalink
Reimplement our compare methods in terms of JDK equivalents.
Browse files Browse the repository at this point in the history
And migrate our own calls off our methods.

Guava has required Java 8 for a while now.

Note also that the APIs in question are available even under Android [even without opt-in library desugaring](https://r8.googlesource.com/r8/+/refs/heads/main/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java).

Further notes:
- I did not touch `UnsignedInteger`, `UnsignedInts`, `UnsignedLong`, or `UnsignedLongs` because the JDK equivalents aren't available under GWT or J2CL.
- I did not touch `UnsignedBytes.compare` because `Bytes.compareUnsigned`, while available under any version of Android, is not available on the JVM until Java 9.
- I did touch an _assertion_ about `UnsignedBytes.compare` because I noticed that it had its actual and expected values reversed.

RELNOTES=n/a
PiperOrigin-RevId: 655152611
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jul 23, 2024
1 parent e74da92 commit 04c1b7a
Show file tree
Hide file tree
Showing 34 changed files with 90 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.common.collect.MutableClassToInstanceMap;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
import com.google.common.primitives.Ints;
import com.google.common.reflect.Invokable;
import com.google.common.reflect.Parameter;
import com.google.common.reflect.Reflection;
Expand Down Expand Up @@ -104,7 +103,7 @@ public int compare(Invokable<?, ?> left, Invokable<?, ?> right) {
new Ordering<Invokable<?, ?>>() {
@Override
public int compare(Invokable<?, ?> left, Invokable<?, ?> right) {
return Ints.compare(left.getParameters().size(), right.getParameters().size());
return Integer.compare(left.getParameters().size(), right.getParameters().size());
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Longs;
import com.google.common.util.concurrent.AbstractFuture;
import com.google.common.util.concurrent.AbstractListeningExecutorService;
import com.google.common.util.concurrent.ListenableScheduledFuture;
Expand Down Expand Up @@ -166,7 +165,7 @@ public long getDelay(TimeUnit unit) {

@Override
public int compareTo(Delayed other) {
return Longs.compare(getDelay(NANOSECONDS), other.getDelay(NANOSECONDS));
return Long.compare(getDelay(NANOSECONDS), other.getDelay(NANOSECONDS));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.primitives.Ints;
import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -123,7 +122,7 @@ public int hashCode() {

@Override
public int compareTo(Element that) {
return Ints.compare(hash, that.hash);
return Integer.compare(hash, that.hash);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.google.common.collect.Ordering.ArbitraryOrdering;
import com.google.common.collect.Ordering.IncomparableValueException;
import com.google.common.collect.testing.Helpers;
import com.google.common.primitives.Ints;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
import java.util.Arrays;
Expand Down Expand Up @@ -209,6 +208,7 @@ public void testExplicit_sortingExample() {
reserializeAndAssert(c);
}

@SuppressWarnings("DistinctVarargsChecker") // test of buggy call
public void testExplicit_withDuplicates() {
try {
Ordering.explicit(1, 2, 3, 4, 2);
Expand Down Expand Up @@ -1143,7 +1143,7 @@ private static class Composite<T extends @Nullable Object> implements Comparable
// order of 't'.
@Override
public int compareTo(Composite<T> that) {
return Ints.compare(rank, that.rank);
return Integer.compare(rank, that.rank);
}

static <T extends @Nullable Object> Function<Composite<T>, T> getValueFunction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public void testCompare() {
byte y = VALUES[j];
// note: spec requires only that the sign is the same
assertWithMessage(x + ", " + y)
.that(Math.signum(Ints.compare(i, j)))
.isEqualTo(Math.signum(UnsignedBytes.compare(x, y)));
.that(Math.signum(UnsignedBytes.compare(x, y)))
.isEqualTo(Math.signum(Integer.compare(i, j)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.google.common.base.CaseFormat;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -215,7 +214,7 @@ public int compare(Method m1, Method m2) {
if (nameComparison != 0) {
return nameComparison;
} else {
return Ints.compare(m1.getParameterTypes().length, m2.getParameterTypes().length);
return Integer.compare(m1.getParameterTypes().length, m2.getParameterTypes().length);
}
}
});
Expand Down
17 changes: 7 additions & 10 deletions android/guava/src/com/google/common/collect/ComparisonChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import com.google.common.primitives.Booleans;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import java.util.Comparator;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -82,12 +79,12 @@ public ComparisonChain compare(Comparable<?> left, Comparable<?> right) {

@Override
public ComparisonChain compare(int left, int right) {
return classify(Ints.compare(left, right));
return classify(Integer.compare(left, right));
}

@Override
public ComparisonChain compare(long left, long right) {
return classify(Longs.compare(left, right));
return classify(Long.compare(left, right));
}

@Override
Expand All @@ -102,12 +99,12 @@ public ComparisonChain compare(double left, double right) {

@Override
public ComparisonChain compareTrueFirst(boolean left, boolean right) {
return classify(Booleans.compare(right, left)); // reversed
return classify(Boolean.compare(right, left)); // reversed
}

@Override
public ComparisonChain compareFalseFirst(boolean left, boolean right) {
return classify(Booleans.compare(left, right));
return classify(Boolean.compare(left, right));
}

ComparisonChain classify(int result) {
Expand Down Expand Up @@ -204,13 +201,13 @@ public int result() {
@ParametricNullness T left, @ParametricNullness T right, Comparator<T> comparator);

/**
* Compares two {@code int} values as specified by {@link Ints#compare}, <i>if</i> the result of
* this comparison chain has not already been determined.
* Compares two {@code int} values as specified by {@link Integer#compare}, <i>if</i> the result
* of this comparison chain has not already been determined.
*/
public abstract ComparisonChain compare(int left, int right);

/**
* Compares two {@code long} values as specified by {@link Longs#compare}, <i>if</i> the result of
* Compares two {@code long} values as specified by {@link Long#compare}, <i>if</i> the result of
* this comparison chain has not already been determined.
*/
public abstract ComparisonChain compare(long left, long right);
Expand Down
3 changes: 1 addition & 2 deletions android/guava/src/com/google/common/collect/Cut.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.primitives.Booleans;
import java.io.Serializable;
import java.util.NoSuchElementException;
import javax.annotation.CheckForNull;
Expand Down Expand Up @@ -83,7 +82,7 @@ public int compareTo(Cut<C> that) {
return result;
}
// same value. below comes before above
return Booleans.compare(this instanceof AboveValue, that instanceof AboveValue);
return Boolean.compare(this instanceof AboveValue, that instanceof AboveValue);
}

C endpoint() {
Expand Down
3 changes: 1 addition & 2 deletions android/guava/src/com/google/common/math/DoubleMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.primitives.Booleans;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.math.BigInteger;
import java.math.RoundingMode;
Expand Down Expand Up @@ -393,7 +392,7 @@ public static int fuzzyCompare(double a, double b, double tolerance) {
} else if (a > b) {
return 1;
} else {
return Booleans.compare(Double.isNaN(a), Double.isNaN(b));
return Boolean.compare(Double.isNaN(a), Double.isNaN(b));
}
}

Expand Down
7 changes: 3 additions & 4 deletions android/guava/src/com/google/common/math/LongMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.primitives.Longs;
import com.google.common.primitives.UnsignedLongs;
import java.math.BigInteger;
import java.math.RoundingMode;
Expand Down Expand Up @@ -1255,7 +1254,7 @@ public static double roundToDouble(long x, RoundingMode mode) {
if (roundArbitrarilyAsLong == Long.MAX_VALUE) {
/*
* For most values, the conversion from roundArbitrarily to roundArbitrarilyAsLong is
* lossless. In that case we can compare x to roundArbitrarily using Longs.compare(x,
* lossless. In that case we can compare x to roundArbitrarily using Long.compare(x,
* roundArbitrarilyAsLong). The exception is for values where the conversion to double rounds
* up to give roundArbitrarily equal to 2^63, so the conversion back to long overflows and
* roundArbitrarilyAsLong is Long.MAX_VALUE. (This is the only way this condition can occur as
Expand All @@ -1265,7 +1264,7 @@ public static double roundToDouble(long x, RoundingMode mode) {
*/
cmpXToRoundArbitrarily = -1;
} else {
cmpXToRoundArbitrarily = Longs.compare(x, roundArbitrarilyAsLong);
cmpXToRoundArbitrarily = Long.compare(x, roundArbitrarilyAsLong);
}

switch (mode) {
Expand Down Expand Up @@ -1324,7 +1323,7 @@ public static double roundToDouble(long x, RoundingMode mode) {
deltaToCeiling++;
}

int diff = Longs.compare(deltaToFloor, deltaToCeiling);
int diff = Long.compare(deltaToFloor, deltaToCeiling);
if (diff < 0) { // closer to floor
return roundFloorAsDouble;
} else if (diff > 0) { // closer to ceiling
Expand Down
6 changes: 4 additions & 2 deletions android/guava/src/com/google/common/primitives/Booleans.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.base.Preconditions.checkPositionIndexes;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.InlineMe;
import java.io.Serializable;
import java.util.AbstractList;
import java.util.Arrays;
Expand Down Expand Up @@ -121,8 +122,9 @@ public static int hashCode(boolean value) {
* @return a positive number if only {@code a} is {@code true}, a negative number if only {@code
* b} is true, or zero if {@code a == b}
*/
@InlineMe(replacement = "Boolean.compare(a, b)")
public static int compare(boolean a, boolean b) {
return (a == b) ? 0 : (a ? 1 : -1);
return Boolean.compare(a, b);
}

/**
Expand Down Expand Up @@ -310,7 +312,7 @@ private enum LexicographicalComparator implements Comparator<boolean[]> {
public int compare(boolean[] left, boolean[] right) {
int minLength = Math.min(left.length, right.length);
for (int i = 0; i < minLength; i++) {
int result = Booleans.compare(left[i], right[i]);
int result = Boolean.compare(left[i], right[i]);
if (result != 0) {
return result;
}
Expand Down
6 changes: 4 additions & 2 deletions android/guava/src/com/google/common/primitives/Chars.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.errorprone.annotations.InlineMe;
import java.io.Serializable;
import java.util.AbstractList;
import java.util.Arrays;
Expand Down Expand Up @@ -113,8 +114,9 @@ public static char saturatedCast(long value) {
* @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
* greater than {@code b}; or zero if they are equal
*/
@InlineMe(replacement = "Character.compare(a, b)")
public static int compare(char a, char b) {
return a - b; // safe due to restricted range
return Character.compare(a, b);
}

/**
Expand Down Expand Up @@ -391,7 +393,7 @@ private enum LexicographicalComparator implements Comparator<char[]> {
public int compare(char[] left, char[] right) {
int minLength = Math.min(left.length, right.length);
for (int i = 0; i < minLength; i++) {
int result = Chars.compare(left[i], right[i]);
int result = Character.compare(left[i], right[i]);
if (result != 0) {
return result;
}
Expand Down
6 changes: 4 additions & 2 deletions android/guava/src/com/google/common/primitives/Ints.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Converter;
import com.google.errorprone.annotations.InlineMe;
import java.io.Serializable;
import java.util.AbstractList;
import java.util.Arrays;
Expand Down Expand Up @@ -118,8 +119,9 @@ public static int saturatedCast(long value) {
* @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
* greater than {@code b}; or zero if they are equal
*/
@InlineMe(replacement = "Integer.compare(a, b)")
public static int compare(int a, int b) {
return (a < b) ? -1 : ((a > b) ? 1 : 0);
return Integer.compare(a, b);
}

/**
Expand Down Expand Up @@ -439,7 +441,7 @@ private enum LexicographicalComparator implements Comparator<int[]> {
public int compare(int[] left, int[] right) {
int minLength = Math.min(left.length, right.length);
for (int i = 0; i < minLength; i++) {
int result = Ints.compare(left[i], right[i]);
int result = Integer.compare(left[i], right[i]);
if (result != 0) {
return result;
}
Expand Down
6 changes: 4 additions & 2 deletions android/guava/src/com/google/common/primitives/Longs.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Converter;
import com.google.errorprone.annotations.InlineMe;
import java.io.Serializable;
import java.util.AbstractList;
import java.util.Arrays;
Expand Down Expand Up @@ -89,8 +90,9 @@ public static int hashCode(long value) {
* @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
* greater than {@code b}; or zero if they are equal
*/
@InlineMe(replacement = "Long.compare(a, b)")
public static int compare(long a, long b) {
return (a < b) ? -1 : ((a > b) ? 1 : 0);
return Long.compare(a, b);
}

/**
Expand Down Expand Up @@ -543,7 +545,7 @@ private enum LexicographicalComparator implements Comparator<long[]> {
public int compare(long[] left, long[] right) {
int minLength = Math.min(left.length, right.length);
for (int i = 0; i < minLength; i++) {
int result = Longs.compare(left[i], right[i]);
int result = Long.compare(left[i], right[i]);
if (result != 0) {
return result;
}
Expand Down
6 changes: 4 additions & 2 deletions android/guava/src/com/google/common/primitives/Shorts.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Converter;
import com.google.errorprone.annotations.InlineMe;
import java.io.Serializable;
import java.util.AbstractList;
import java.util.Arrays;
Expand Down Expand Up @@ -117,8 +118,9 @@ public static short saturatedCast(long value) {
* @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
* greater than {@code b}; or zero if they are equal
*/
@InlineMe(replacement = "Short.compare(a, b)")
public static int compare(short a, short b) {
return a - b; // safe due to restricted range
return Short.compare(a, b);
}

/**
Expand Down Expand Up @@ -441,7 +443,7 @@ private enum LexicographicalComparator implements Comparator<short[]> {
public int compare(short[] left, short[] right) {
int minLength = Math.min(left.length, right.length);
for (int i = 0; i < minLength; i++) {
int result = Shorts.compare(left[i], right[i]);
int result = Short.compare(left[i], right[i]);
if (result != 0) {
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ public static byte saturatedCast(long value) {
* @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
* greater than {@code b}; or zero if they are equal
*/
// TODO(kevinb): if Ints.compare etc. are ever removed, *maybe* remove this
// one too, which would leave compare methods only on the Unsigned* classes.
public static int compare(byte a, byte b) {
return a - b; // safe due to restricted range
return Byte.compare(a, b);
}

/**
Expand Down Expand Up @@ -181,7 +179,7 @@ private enum LexicographicalComparator implements Comparator<byte[]> {
public int compare(byte[] left, byte[] right) {
int minLength = Math.min(left.length, right.length);
for (int i = 0; i < minLength; i++) {
int result = SignedBytes.compare(left[i], right[i]);
int result = Byte.compare(left[i], right[i]);
if (result != 0) {
return result;
}
Expand Down
Loading

0 comments on commit 04c1b7a

Please sign in to comment.