Skip to content

Commit 04c1b7a

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Reimplement our compare methods in terms of JDK equivalents.
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
1 parent e74da92 commit 04c1b7a

File tree

34 files changed

+90
-89
lines changed

34 files changed

+90
-89
lines changed

android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.google.common.collect.MutableClassToInstanceMap;
3434
import com.google.common.collect.Ordering;
3535
import com.google.common.collect.Sets;
36-
import com.google.common.primitives.Ints;
3736
import com.google.common.reflect.Invokable;
3837
import com.google.common.reflect.Parameter;
3938
import com.google.common.reflect.Reflection;
@@ -104,7 +103,7 @@ public int compare(Invokable<?, ?> left, Invokable<?, ?> right) {
104103
new Ordering<Invokable<?, ?>>() {
105104
@Override
106105
public int compare(Invokable<?, ?> left, Invokable<?, ?> right) {
107-
return Ints.compare(left.getParameters().size(), right.getParameters().size());
106+
return Integer.compare(left.getParameters().size(), right.getParameters().size());
108107
}
109108
};
110109

android/guava-testlib/src/com/google/common/util/concurrent/testing/TestingExecutors.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import com.google.common.annotations.GwtIncompatible;
2222
import com.google.common.collect.ImmutableList;
23-
import com.google.common.primitives.Longs;
2423
import com.google.common.util.concurrent.AbstractFuture;
2524
import com.google.common.util.concurrent.AbstractListeningExecutorService;
2625
import com.google.common.util.concurrent.ListenableScheduledFuture;
@@ -166,7 +165,7 @@ public long getDelay(TimeUnit unit) {
166165

167166
@Override
168167
public int compareTo(Delayed other) {
169-
return Longs.compare(getDelay(NANOSECONDS), other.getDelay(NANOSECONDS));
168+
return Long.compare(getDelay(NANOSECONDS), other.getDelay(NANOSECONDS));
170169
}
171170
}
172171
}

android/guava-tests/test/com/google/common/collect/CollectionBenchmarkSampleData.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

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

21-
import com.google.common.primitives.Ints;
2221
import java.util.Collections;
2322
import java.util.List;
2423
import java.util.Set;
@@ -123,7 +122,7 @@ public int hashCode() {
123122

124123
@Override
125124
public int compareTo(Element that) {
126-
return Ints.compare(hash, that.hash);
125+
return Integer.compare(hash, that.hash);
127126
}
128127

129128
@Override

android/guava-tests/test/com/google/common/collect/OrderingTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.google.common.collect.Ordering.ArbitraryOrdering;
3232
import com.google.common.collect.Ordering.IncomparableValueException;
3333
import com.google.common.collect.testing.Helpers;
34-
import com.google.common.primitives.Ints;
3534
import com.google.common.testing.EqualsTester;
3635
import com.google.common.testing.NullPointerTester;
3736
import java.util.Arrays;
@@ -209,6 +208,7 @@ public void testExplicit_sortingExample() {
209208
reserializeAndAssert(c);
210209
}
211210

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

11491149
static <T extends @Nullable Object> Function<Composite<T>, T> getValueFunction() {

android/guava-tests/test/com/google/common/primitives/UnsignedBytesTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public void testCompare() {
9191
byte y = VALUES[j];
9292
// note: spec requires only that the sign is the same
9393
assertWithMessage(x + ", " + y)
94-
.that(Math.signum(Ints.compare(i, j)))
95-
.isEqualTo(Math.signum(UnsignedBytes.compare(x, y)));
94+
.that(Math.signum(UnsignedBytes.compare(x, y)))
95+
.isEqualTo(Math.signum(Integer.compare(i, j)));
9696
}
9797
}
9898
}

android/guava-tests/test/com/google/common/util/concurrent/GeneratedMonitorTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import com.google.common.base.CaseFormat;
2222
import com.google.common.collect.ImmutableList;
23-
import com.google.common.primitives.Ints;
2423
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2524
import java.lang.reflect.InvocationTargetException;
2625
import java.lang.reflect.Method;
@@ -215,7 +214,7 @@ public int compare(Method m1, Method m2) {
215214
if (nameComparison != 0) {
216215
return nameComparison;
217216
} else {
218-
return Ints.compare(m1.getParameterTypes().length, m2.getParameterTypes().length);
217+
return Integer.compare(m1.getParameterTypes().length, m2.getParameterTypes().length);
219218
}
220219
}
221220
});

android/guava/src/com/google/common/collect/ComparisonChain.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
package com.google.common.collect;
1818

1919
import com.google.common.annotations.GwtCompatible;
20-
import com.google.common.primitives.Booleans;
21-
import com.google.common.primitives.Ints;
22-
import com.google.common.primitives.Longs;
2320
import java.util.Comparator;
2421
import org.checkerframework.checker.nullness.qual.Nullable;
2522

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

8380
@Override
8481
public ComparisonChain compare(int left, int right) {
85-
return classify(Ints.compare(left, right));
82+
return classify(Integer.compare(left, right));
8683
}
8784

8885
@Override
8986
public ComparisonChain compare(long left, long right) {
90-
return classify(Longs.compare(left, right));
87+
return classify(Long.compare(left, right));
9188
}
9289

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

103100
@Override
104101
public ComparisonChain compareTrueFirst(boolean left, boolean right) {
105-
return classify(Booleans.compare(right, left)); // reversed
102+
return classify(Boolean.compare(right, left)); // reversed
106103
}
107104

108105
@Override
109106
public ComparisonChain compareFalseFirst(boolean left, boolean right) {
110-
return classify(Booleans.compare(left, right));
107+
return classify(Boolean.compare(left, right));
111108
}
112109

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

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

212209
/**
213-
* Compares two {@code long} values as specified by {@link Longs#compare}, <i>if</i> the result of
210+
* Compares two {@code long} values as specified by {@link Long#compare}, <i>if</i> the result of
214211
* this comparison chain has not already been determined.
215212
*/
216213
public abstract ComparisonChain compare(long left, long right);

android/guava/src/com/google/common/collect/Cut.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static com.google.common.base.Preconditions.checkNotNull;
1818

1919
import com.google.common.annotations.GwtCompatible;
20-
import com.google.common.primitives.Booleans;
2120
import java.io.Serializable;
2221
import java.util.NoSuchElementException;
2322
import javax.annotation.CheckForNull;
@@ -83,7 +82,7 @@ public int compareTo(Cut<C> that) {
8382
return result;
8483
}
8584
// same value. below comes before above
86-
return Booleans.compare(this instanceof AboveValue, that instanceof AboveValue);
85+
return Boolean.compare(this instanceof AboveValue, that instanceof AboveValue);
8786
}
8887

8988
C endpoint() {

android/guava/src/com/google/common/math/DoubleMath.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.google.common.annotations.GwtCompatible;
3434
import com.google.common.annotations.GwtIncompatible;
3535
import com.google.common.annotations.VisibleForTesting;
36-
import com.google.common.primitives.Booleans;
3736
import com.google.errorprone.annotations.CanIgnoreReturnValue;
3837
import java.math.BigInteger;
3938
import java.math.RoundingMode;
@@ -393,7 +392,7 @@ public static int fuzzyCompare(double a, double b, double tolerance) {
393392
} else if (a > b) {
394393
return 1;
395394
} else {
396-
return Booleans.compare(Double.isNaN(a), Double.isNaN(b));
395+
return Boolean.compare(Double.isNaN(a), Double.isNaN(b));
397396
}
398397
}
399398

android/guava/src/com/google/common/math/LongMath.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.google.common.annotations.GwtCompatible;
2929
import com.google.common.annotations.GwtIncompatible;
3030
import com.google.common.annotations.VisibleForTesting;
31-
import com.google.common.primitives.Longs;
3231
import com.google.common.primitives.UnsignedLongs;
3332
import java.math.BigInteger;
3433
import java.math.RoundingMode;
@@ -1255,7 +1254,7 @@ public static double roundToDouble(long x, RoundingMode mode) {
12551254
if (roundArbitrarilyAsLong == Long.MAX_VALUE) {
12561255
/*
12571256
* For most values, the conversion from roundArbitrarily to roundArbitrarilyAsLong is
1258-
* lossless. In that case we can compare x to roundArbitrarily using Longs.compare(x,
1257+
* lossless. In that case we can compare x to roundArbitrarily using Long.compare(x,
12591258
* roundArbitrarilyAsLong). The exception is for values where the conversion to double rounds
12601259
* up to give roundArbitrarily equal to 2^63, so the conversion back to long overflows and
12611260
* roundArbitrarilyAsLong is Long.MAX_VALUE. (This is the only way this condition can occur as
@@ -1265,7 +1264,7 @@ public static double roundToDouble(long x, RoundingMode mode) {
12651264
*/
12661265
cmpXToRoundArbitrarily = -1;
12671266
} else {
1268-
cmpXToRoundArbitrarily = Longs.compare(x, roundArbitrarilyAsLong);
1267+
cmpXToRoundArbitrarily = Long.compare(x, roundArbitrarilyAsLong);
12691268
}
12701269

12711270
switch (mode) {
@@ -1324,7 +1323,7 @@ public static double roundToDouble(long x, RoundingMode mode) {
13241323
deltaToCeiling++;
13251324
}
13261325

1327-
int diff = Longs.compare(deltaToFloor, deltaToCeiling);
1326+
int diff = Long.compare(deltaToFloor, deltaToCeiling);
13281327
if (diff < 0) { // closer to floor
13291328
return roundFloorAsDouble;
13301329
} else if (diff > 0) { // closer to ceiling

android/guava/src/com/google/common/primitives/Booleans.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.common.base.Preconditions.checkPositionIndexes;
2121

2222
import com.google.common.annotations.GwtCompatible;
23+
import com.google.errorprone.annotations.InlineMe;
2324
import java.io.Serializable;
2425
import java.util.AbstractList;
2526
import java.util.Arrays;
@@ -121,8 +122,9 @@ public static int hashCode(boolean value) {
121122
* @return a positive number if only {@code a} is {@code true}, a negative number if only {@code
122123
* b} is true, or zero if {@code a == b}
123124
*/
125+
@InlineMe(replacement = "Boolean.compare(a, b)")
124126
public static int compare(boolean a, boolean b) {
125-
return (a == b) ? 0 : (a ? 1 : -1);
127+
return Boolean.compare(a, b);
126128
}
127129

128130
/**
@@ -310,7 +312,7 @@ private enum LexicographicalComparator implements Comparator<boolean[]> {
310312
public int compare(boolean[] left, boolean[] right) {
311313
int minLength = Math.min(left.length, right.length);
312314
for (int i = 0; i < minLength; i++) {
313-
int result = Booleans.compare(left[i], right[i]);
315+
int result = Boolean.compare(left[i], right[i]);
314316
if (result != 0) {
315317
return result;
316318
}

android/guava/src/com/google/common/primitives/Chars.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.common.annotations.GwtCompatible;
2323
import com.google.common.annotations.GwtIncompatible;
24+
import com.google.errorprone.annotations.InlineMe;
2425
import java.io.Serializable;
2526
import java.util.AbstractList;
2627
import java.util.Arrays;
@@ -113,8 +114,9 @@ public static char saturatedCast(long value) {
113114
* @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
114115
* greater than {@code b}; or zero if they are equal
115116
*/
117+
@InlineMe(replacement = "Character.compare(a, b)")
116118
public static int compare(char a, char b) {
117-
return a - b; // safe due to restricted range
119+
return Character.compare(a, b);
118120
}
119121

120122
/**
@@ -391,7 +393,7 @@ private enum LexicographicalComparator implements Comparator<char[]> {
391393
public int compare(char[] left, char[] right) {
392394
int minLength = Math.min(left.length, right.length);
393395
for (int i = 0; i < minLength; i++) {
394-
int result = Chars.compare(left[i], right[i]);
396+
int result = Character.compare(left[i], right[i]);
395397
if (result != 0) {
396398
return result;
397399
}

android/guava/src/com/google/common/primitives/Ints.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.annotations.GwtCompatible;
2323
import com.google.common.annotations.GwtIncompatible;
2424
import com.google.common.base.Converter;
25+
import com.google.errorprone.annotations.InlineMe;
2526
import java.io.Serializable;
2627
import java.util.AbstractList;
2728
import java.util.Arrays;
@@ -118,8 +119,9 @@ public static int saturatedCast(long value) {
118119
* @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
119120
* greater than {@code b}; or zero if they are equal
120121
*/
122+
@InlineMe(replacement = "Integer.compare(a, b)")
121123
public static int compare(int a, int b) {
122-
return (a < b) ? -1 : ((a > b) ? 1 : 0);
124+
return Integer.compare(a, b);
123125
}
124126

125127
/**
@@ -439,7 +441,7 @@ private enum LexicographicalComparator implements Comparator<int[]> {
439441
public int compare(int[] left, int[] right) {
440442
int minLength = Math.min(left.length, right.length);
441443
for (int i = 0; i < minLength; i++) {
442-
int result = Ints.compare(left[i], right[i]);
444+
int result = Integer.compare(left[i], right[i]);
443445
if (result != 0) {
444446
return result;
445447
}

android/guava/src/com/google/common/primitives/Longs.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.common.annotations.GwtCompatible;
2323
import com.google.common.base.Converter;
24+
import com.google.errorprone.annotations.InlineMe;
2425
import java.io.Serializable;
2526
import java.util.AbstractList;
2627
import java.util.Arrays;
@@ -89,8 +90,9 @@ public static int hashCode(long value) {
8990
* @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
9091
* greater than {@code b}; or zero if they are equal
9192
*/
93+
@InlineMe(replacement = "Long.compare(a, b)")
9294
public static int compare(long a, long b) {
93-
return (a < b) ? -1 : ((a > b) ? 1 : 0);
95+
return Long.compare(a, b);
9496
}
9597

9698
/**
@@ -543,7 +545,7 @@ private enum LexicographicalComparator implements Comparator<long[]> {
543545
public int compare(long[] left, long[] right) {
544546
int minLength = Math.min(left.length, right.length);
545547
for (int i = 0; i < minLength; i++) {
546-
int result = Longs.compare(left[i], right[i]);
548+
int result = Long.compare(left[i], right[i]);
547549
if (result != 0) {
548550
return result;
549551
}

android/guava/src/com/google/common/primitives/Shorts.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.annotations.GwtCompatible;
2323
import com.google.common.annotations.GwtIncompatible;
2424
import com.google.common.base.Converter;
25+
import com.google.errorprone.annotations.InlineMe;
2526
import java.io.Serializable;
2627
import java.util.AbstractList;
2728
import java.util.Arrays;
@@ -117,8 +118,9 @@ public static short saturatedCast(long value) {
117118
* @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
118119
* greater than {@code b}; or zero if they are equal
119120
*/
121+
@InlineMe(replacement = "Short.compare(a, b)")
120122
public static int compare(short a, short b) {
121-
return a - b; // safe due to restricted range
123+
return Short.compare(a, b);
122124
}
123125

124126
/**
@@ -441,7 +443,7 @@ private enum LexicographicalComparator implements Comparator<short[]> {
441443
public int compare(short[] left, short[] right) {
442444
int minLength = Math.min(left.length, right.length);
443445
for (int i = 0; i < minLength; i++) {
444-
int result = Shorts.compare(left[i], right[i]);
446+
int result = Short.compare(left[i], right[i]);
445447
if (result != 0) {
446448
return result;
447449
}

android/guava/src/com/google/common/primitives/SignedBytes.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ public static byte saturatedCast(long value) {
8989
* @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
9090
* greater than {@code b}; or zero if they are equal
9191
*/
92-
// TODO(kevinb): if Ints.compare etc. are ever removed, *maybe* remove this
93-
// one too, which would leave compare methods only on the Unsigned* classes.
9492
public static int compare(byte a, byte b) {
95-
return a - b; // safe due to restricted range
93+
return Byte.compare(a, b);
9694
}
9795

9896
/**
@@ -181,7 +179,7 @@ private enum LexicographicalComparator implements Comparator<byte[]> {
181179
public int compare(byte[] left, byte[] right) {
182180
int minLength = Math.min(left.length, right.length);
183181
for (int i = 0; i < minLength; i++) {
184-
int result = SignedBytes.compare(left[i], right[i]);
182+
int result = Byte.compare(left[i], right[i]);
185183
if (result != 0) {
186184
return result;
187185
}

0 commit comments

Comments
 (0)