Skip to content

Commit 23158e1

Browse files
committed
Firestore: use Integer.compare() to compare integers instead of the bespoke Util.compareIntegers() method.
The `Util.compareIntegers()` method was originally added because it required minSdkVersion of 19 or greater which, at the time, was not the case. However, minSdkVersion has been 21 for some time now (and is increasing to 23 in the foreseeable future) so there is no longer any need for the bespoke implementation.
1 parent 84c4d81 commit 23158e1

File tree

7 files changed

+16
-30
lines changed

7 files changed

+16
-30
lines changed

firebase-firestore/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
* [fixed] Further improved performance of UTF-8 string ordering logic,
33
which had degraded in v25.1.2 and received some improvements in v25.1.3.
44
[#7053](//github.com/firebase/firebase-android-sdk/issues/7053)
5+
* [changed] Use `Integer.compare()` (which was added in Android API 19) to compare integers instead
6+
of Firestore's bespoke implementation, since minSdkVersion has been greater than 19 for some time.
7+
[#NNNN](//github.com/firebase/firebase-android-sdk/pull/NNNN)
58

69

710
# 25.1.4

firebase-firestore/src/main/java/com/google/firebase/firestore/core/View.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static com.google.firebase.firestore.core.Query.LimitType.LIMIT_TO_FIRST;
1818
import static com.google.firebase.firestore.core.Query.LimitType.LIMIT_TO_LAST;
1919
import static com.google.firebase.firestore.util.Assert.hardAssert;
20-
import static com.google.firebase.firestore.util.Util.compareIntegers;
2120

2221
import androidx.annotation.Nullable;
2322
import com.google.firebase.database.collection.ImmutableSortedMap;
@@ -301,7 +300,7 @@ public ViewChange applyChanges(
301300
Collections.sort(
302301
viewChanges,
303302
(DocumentViewChange o1, DocumentViewChange o2) -> {
304-
int typeComp = compareIntegers(View.changeTypeOrder(o1), View.changeTypeOrder(o2));
303+
int typeComp = Integer.compare(View.changeTypeOrder(o1), View.changeTypeOrder(o2));
305304
if (typeComp != 0) {
306305
return typeComp;
307306
}

firebase-firestore/src/main/java/com/google/firebase/firestore/local/DocumentReference.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
package com.google.firebase.firestore.local;
1616

17-
import static com.google.firebase.firestore.util.Util.compareIntegers;
18-
1917
import com.google.firebase.firestore.model.DocumentKey;
2018
import java.util.Comparator;
2119

@@ -60,12 +58,12 @@ int getId() {
6058
return keyComp;
6159
}
6260

63-
return compareIntegers(o1.targetOrBatchId, o2.targetOrBatchId);
61+
return Integer.compare(o1.targetOrBatchId, o2.targetOrBatchId);
6462
};
6563

6664
static final Comparator<DocumentReference> BY_TARGET =
6765
(o1, o2) -> {
68-
int targetComp = compareIntegers(o1.targetOrBatchId, o2.targetOrBatchId);
66+
int targetComp = Integer.compare(o1.targetOrBatchId, o2.targetOrBatchId);
6967

7068
if (targetComp != 0) {
7169
return targetComp;

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteMutationQueue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public List<MutationBatch> getAllMutationBatchesAffectingDocumentKeys(
324324
Collections.sort(
325325
result,
326326
(MutationBatch lhs, MutationBatch rhs) ->
327-
Util.compareIntegers(lhs.getBatchId(), rhs.getBatchId()));
327+
Integer.compare(lhs.getBatchId(), rhs.getBatchId()));
328328
}
329329
return result;
330330
}

firebase-firestore/src/main/java/com/google/firebase/firestore/model/BasePath.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public int compareTo(@NonNull B o) {
100100
}
101101
i++;
102102
}
103-
return Util.compareIntegers(myLength, theirLength);
103+
return Integer.compare(myLength, theirLength);
104104
}
105105

106106
private static int compareSegments(String lhs, String rhs) {

firebase-firestore/src/main/java/com/google/firebase/firestore/model/Values.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public static int compare(Value left, Value right) {
214214
int rightType = typeOrder(right);
215215

216216
if (leftType != rightType) {
217-
return Util.compareIntegers(leftType, rightType);
217+
return Integer.compare(leftType, rightType);
218218
}
219219

220220
switch (leftType) {
@@ -305,7 +305,7 @@ private static int compareTimestamps(Timestamp left, Timestamp right) {
305305
if (cmp != 0) {
306306
return cmp;
307307
}
308-
return Util.compareIntegers(left.getNanos(), right.getNanos());
308+
return Integer.compare(left.getNanos(), right.getNanos());
309309
}
310310

311311
private static int compareReferences(String leftPath, String rightPath) {
@@ -319,7 +319,7 @@ private static int compareReferences(String leftPath, String rightPath) {
319319
return cmp;
320320
}
321321
}
322-
return Util.compareIntegers(leftSegments.length, rightSegments.length);
322+
return Integer.compare(leftSegments.length, rightSegments.length);
323323
}
324324

325325
private static int compareGeoPoints(LatLng left, LatLng right) {
@@ -338,7 +338,7 @@ private static int compareArrays(ArrayValue left, ArrayValue right) {
338338
return cmp;
339339
}
340340
}
341-
return Util.compareIntegers(left.getValuesCount(), right.getValuesCount());
341+
return Integer.compare(left.getValuesCount(), right.getValuesCount());
342342
}
343343

344344
private static int compareMaps(MapValue left, MapValue right) {
@@ -372,7 +372,7 @@ private static int compareVectors(MapValue left, MapValue right) {
372372
ArrayValue rightArrayValue = rightMap.get(Values.VECTOR_MAP_VECTORS_KEY).getArrayValue();
373373

374374
int lengthCompare =
375-
Util.compareIntegers(leftArrayValue.getValuesCount(), rightArrayValue.getValuesCount());
375+
Integer.compare(leftArrayValue.getValuesCount(), rightArrayValue.getValuesCount());
376376
if (lengthCompare != 0) {
377377
return lengthCompare;
378378
}

firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,6 @@ public static int compareBooleans(boolean b1, boolean b2) {
7373
}
7474
}
7575

76-
/**
77-
* Utility function to compare integers. Note that we can't use Integer.compare because it's only
78-
* available after Android 19.
79-
*/
80-
public static int compareIntegers(int i1, int i2) {
81-
if (i1 < i2) {
82-
return -1;
83-
} else if (i1 > i2) {
84-
return 1;
85-
} else {
86-
return 0;
87-
}
88-
}
89-
9076
/** Compare strings in UTF-8 encoded byte order */
9177
public static int compareUtf8Strings(String left, String right) {
9278
// noinspection StringEquality
@@ -119,7 +105,7 @@ public static int compareUtf8Strings(String left, String right) {
119105
final char rightChar = right.charAt(i);
120106
if (leftChar != rightChar) {
121107
return (isSurrogate(leftChar) == isSurrogate(rightChar))
122-
? Util.compareIntegers(leftChar, rightChar)
108+
? Integer.compare(leftChar, rightChar)
123109
: isSurrogate(leftChar) ? 1 : -1;
124110
}
125111
}
@@ -274,7 +260,7 @@ public static int compareByteArrays(byte[] left, byte[] right) {
274260
}
275261
// Byte values are equal, continue with comparison
276262
}
277-
return Util.compareIntegers(left.length, right.length);
263+
return Integer.compare(left.length, right.length);
278264
}
279265

280266
public static int compareByteStrings(ByteString left, ByteString right) {
@@ -290,7 +276,7 @@ public static int compareByteStrings(ByteString left, ByteString right) {
290276
}
291277
// Byte values are equal, continue with comparison
292278
}
293-
return Util.compareIntegers(left.size(), right.size());
279+
return Integer.compare(left.size(), right.size());
294280
}
295281

296282
public static StringBuilder repeatSequence(

0 commit comments

Comments
 (0)