13
13
// limitations under the License.
14
14
package com.google.firebase.firestore.model
15
15
16
+ import com.google.cloud.datastore.core.number.NumberComparisonHelper.firestoreCompareDoubleWithLong
17
+ import com.google.cloud.datastore.core.number.NumberComparisonHelper.firestoreCompareDoubles
16
18
import com.google.firebase.firestore.Blob
17
19
import com.google.firebase.firestore.DocumentReference
18
20
import com.google.firebase.firestore.GeoPoint
@@ -28,7 +30,6 @@ import com.google.protobuf.ByteString
28
30
import com.google.protobuf.NullValue
29
31
import com.google.protobuf.Timestamp
30
32
import com.google.type.LatLng
31
- import java.lang.Double.doubleToLongBits
32
33
import java.util.Date
33
34
import java.util.TreeMap
34
35
import kotlin.math.min
@@ -175,42 +176,40 @@ internal object Values {
175
176
ValueTypeCase .INTEGER_VALUE ->
176
177
when (right.valueTypeCase) {
177
178
ValueTypeCase .INTEGER_VALUE -> left.integerValue == right.integerValue
178
- ValueTypeCase .DOUBLE_VALUE -> right.doubleValue.compareTo(left.integerValue) == 0
179
+ ValueTypeCase .DOUBLE_VALUE ->
180
+ firestoreCompareDoubleWithLong(right.doubleValue, left.integerValue) == 0
179
181
else -> false
180
182
}
181
183
ValueTypeCase .DOUBLE_VALUE ->
182
184
when (right.valueTypeCase) {
183
185
ValueTypeCase .INTEGER_VALUE ->
184
- compareDoubleWithLong (left.doubleValue, right.integerValue) == 0
186
+ firestoreCompareDoubleWithLong (left.doubleValue, right.integerValue) == 0
185
187
ValueTypeCase .DOUBLE_VALUE ->
186
- doubleToLongBits (left.doubleValue) == doubleToLongBits( right.doubleValue)
188
+ firestoreCompareDoubles (left.doubleValue, right.doubleValue) == 0
187
189
else -> false
188
190
}
189
191
else -> false
190
192
}
191
193
192
- private fun compareDoubleWithLong (double : Double , long : Long ): Int =
193
- if (double.isNaN()) - 1 else double.compareTo(long)
194
+ private fun strictArrayEquals (left : Value , right : Value ): Boolean? {
195
+ val leftArray = left.arrayValue
196
+ val rightArray = right.arrayValue
194
197
195
- private fun strictArrayEquals (left : Value , right : Value ): Boolean? {
196
- val leftArray = left.arrayValue
197
- val rightArray = right.arrayValue
198
-
199
- if (leftArray.valuesCount != rightArray.valuesCount) {
200
- return false
201
- }
198
+ if (leftArray.valuesCount != rightArray.valuesCount) {
199
+ return false
200
+ }
202
201
203
- var foundNull = false
204
- for (i in 0 until leftArray.valuesCount) {
205
- val equals = strictEquals(leftArray.getValues(i), rightArray.getValues(i))
206
- if (equals == = null ) {
207
- foundNull = true
208
- } else if (! equals) {
209
- return false
210
- }
202
+ var foundNull = false
203
+ for (i in 0 until leftArray.valuesCount) {
204
+ val equals = strictEquals(leftArray.getValues(i), rightArray.getValues(i))
205
+ if (equals == = null ) {
206
+ foundNull = true
207
+ } else if (! equals) {
208
+ return false
209
+ }
210
+ }
211
+ return if (foundNull) null else true
211
212
}
212
- return if (foundNull) null else true
213
- }
214
213
215
214
private fun arrayEquals (left : Value , right : Value ): Boolean {
216
215
val leftArray = left.arrayValue
@@ -286,7 +285,7 @@ internal object Values {
286
285
val rightType = typeOrder(right)
287
286
288
287
if (leftType != rightType) {
289
- return Util .compareIntegers(leftType, rightType)
288
+ return leftType.compareTo( rightType)
290
289
}
291
290
292
291
return compareInternal(leftType, left, right)
@@ -296,7 +295,7 @@ internal object Values {
296
295
when (leftType) {
297
296
TYPE_ORDER_NULL ,
298
297
TYPE_ORDER_MAX_VALUE -> 0
299
- TYPE_ORDER_BOOLEAN -> Util .compareBooleans( left.booleanValue, right.booleanValue)
298
+ TYPE_ORDER_BOOLEAN -> left.booleanValue.compareTo( right.booleanValue)
300
299
TYPE_ORDER_NUMBER -> compareNumbers(left, right)
301
300
TYPE_ORDER_TIMESTAMP -> compareTimestamps(left.timestampValue, right.timestampValue)
302
301
TYPE_ORDER_SERVER_TIMESTAMP ->
@@ -359,27 +358,27 @@ internal object Values {
359
358
private fun compareNumbers (left : Value , right : Value ): Int {
360
359
if (left.hasDoubleValue()) {
361
360
if (right.hasDoubleValue()) {
362
- return Util .compareDoubles (left.doubleValue, right.doubleValue)
361
+ return firestoreCompareDoubles (left.doubleValue, right.doubleValue)
363
362
} else if (right.hasIntegerValue()) {
364
- return Util .compareMixed (left.doubleValue, right.integerValue)
363
+ return firestoreCompareDoubleWithLong (left.doubleValue, right.integerValue)
365
364
}
366
365
} else if (left.hasIntegerValue()) {
367
366
if (right.hasIntegerValue()) {
368
- return Util .compareLongs (left.integerValue, right.integerValue)
367
+ return java.lang. Long .compare (left.integerValue, right.integerValue)
369
368
} else if (right.hasDoubleValue()) {
370
- return - 1 * Util .compareMixed (right.doubleValue, left.integerValue)
369
+ return - 1 * firestoreCompareDoubleWithLong (right.doubleValue, left.integerValue)
371
370
}
372
371
}
373
372
374
373
throw Assert .fail(" Unexpected values: %s vs %s" , left, right)
375
374
}
376
375
377
376
private fun compareTimestamps (left : Timestamp , right : Timestamp ): Int {
378
- val cmp = Util .compareLongs( left.seconds, right.seconds)
377
+ val cmp = left.seconds.compareTo( right.seconds)
379
378
if (cmp != 0 ) {
380
379
return cmp
381
380
}
382
- return Util .compareIntegers( left.nanos, right.nanos)
381
+ return left.nanos.compareTo( right.nanos)
383
382
}
384
383
385
384
private fun compareReferences (leftPath : String , rightPath : String ): Int {
@@ -393,13 +392,13 @@ internal object Values {
393
392
return cmp
394
393
}
395
394
}
396
- return Util .compareIntegers( leftSegments.size, rightSegments.size)
395
+ return leftSegments.size.compareTo( rightSegments.size)
397
396
}
398
397
399
398
private fun compareGeoPoints (left : LatLng , right : LatLng ): Int {
400
- val comparison = Util .compareDoubles (left.latitude, right.latitude)
399
+ val comparison = firestoreCompareDoubles (left.latitude, right.latitude)
401
400
if (comparison == 0 ) {
402
- return Util .compareDoubles (left.longitude, right.longitude)
401
+ return firestoreCompareDoubles (left.longitude, right.longitude)
403
402
}
404
403
return comparison
405
404
}
@@ -412,7 +411,7 @@ internal object Values {
412
411
return cmp
413
412
}
414
413
}
415
- return Util .compareIntegers( left.valuesCount, right.valuesCount)
414
+ return left.valuesCount.compareTo( right.valuesCount)
416
415
}
417
416
418
417
private fun compareMaps (left : MapValue , right : MapValue ): Int {
@@ -432,7 +431,7 @@ internal object Values {
432
431
}
433
432
434
433
// Only equal if both iterators are exhausted.
435
- return Util .compareBooleans( iterator1.hasNext(), iterator2.hasNext())
434
+ return iterator1.hasNext().compareTo( iterator2.hasNext())
436
435
}
437
436
438
437
private fun compareVectors (left : MapValue , right : MapValue ): Int {
@@ -443,8 +442,7 @@ internal object Values {
443
442
val leftArrayValue = leftMap[VECTOR_MAP_VECTORS_KEY ]!! .arrayValue
444
443
val rightArrayValue = rightMap[VECTOR_MAP_VECTORS_KEY ]!! .arrayValue
445
444
446
- val lengthCompare =
447
- Util .compareIntegers(leftArrayValue.valuesCount, rightArrayValue.valuesCount)
445
+ val lengthCompare = leftArrayValue.valuesCount.compareTo(rightArrayValue.valuesCount)
448
446
if (lengthCompare != 0 ) {
449
447
return lengthCompare
450
448
}
0 commit comments