22
22
import com .google .firestore .v1 .ArrayValue ;
23
23
import com .google .firestore .v1 .MapValue ;
24
24
import com .google .firestore .v1 .Value ;
25
+ import com .google .protobuf .Timestamp ;
26
+ import com .google .type .LatLng ;
25
27
import java .util .Iterator ;
26
28
import java .util .List ;
27
29
import java .util .Map ;
@@ -148,19 +150,19 @@ public static int compare(Value left, Value right) {
148
150
case FieldValue .TYPE_ORDER_NUMBER :
149
151
return compareNumbers (left , right );
150
152
case FieldValue .TYPE_ORDER_TIMESTAMP :
151
- return compareTimestamps (left , right );
153
+ return compareTimestamps (left . getTimestampValue () , right . getTimestampValue () );
152
154
case FieldValue .TYPE_ORDER_STRING :
153
155
return left .getStringValue ().compareTo (right .getStringValue ());
154
156
case FieldValue .TYPE_ORDER_BLOB :
155
157
return Util .compareByteStrings (left .getBytesValue (), right .getBytesValue ());
156
158
case FieldValue .TYPE_ORDER_REFERENCE :
157
- return compareReferences (left , right );
159
+ return compareReferences (left . getReferenceValue () , right . getReferenceValue () );
158
160
case FieldValue .TYPE_ORDER_GEOPOINT :
159
- return compareGeoPoints (left , right );
161
+ return compareGeoPoints (left . getGeoPointValue () , right . getGeoPointValue () );
160
162
case FieldValue .TYPE_ORDER_ARRAY :
161
- return compareArrays (left , right );
163
+ return compareArrays (left . getArrayValue () , right . getArrayValue () );
162
164
case FieldValue .TYPE_ORDER_OBJECT :
163
- return compareMaps (left , right );
165
+ return compareMaps (left . getMapValue () , right . getMapValue () );
164
166
default :
165
167
throw fail ("Invalid value type: " + leftType );
166
168
}
@@ -186,18 +188,18 @@ private static int compareNumbers(Value left, Value right) {
186
188
throw fail ("Unexpected values: %s vs %s" , left , right );
187
189
}
188
190
189
- private static int compareTimestamps (Value left , Value right ) {
190
- if (left .getTimestampValue (). getSeconds () == right .getTimestampValue (). getSeconds ()) {
191
- return Integer . signum (
192
- left . getTimestampValue (). getNanos () - right . getTimestampValue (). getNanos ()) ;
191
+ private static int compareTimestamps (Timestamp left , Timestamp right ) {
192
+ int cmp = Util . compareLongs (left .getSeconds (), right .getSeconds ());
193
+ if ( cmp != 0 ) {
194
+ return cmp ;
193
195
}
194
- return Long . signum (
195
- left . getTimestampValue (). getSeconds () - right .getTimestampValue (). getSeconds ());
196
+
197
+ return Util . compareIntegers ( left . getNanos (), right .getNanos ());
196
198
}
197
199
198
- private static int compareReferences (Value left , Value right ) {
199
- List <String > leftSegments = Splitter .on ('/' ).splitToList (left . getReferenceValue () );
200
- List <String > rightSegments = Splitter .on ('/' ).splitToList (right . getReferenceValue () );
200
+ private static int compareReferences (String leftPath , String rightPath ) {
201
+ List <String > leftSegments = Splitter .on ('/' ).splitToList (leftPath );
202
+ List <String > rightSegments = Splitter .on ('/' ).splitToList (rightPath );
201
203
int minLength = Math .min (leftSegments .size (), rightSegments .size ());
202
204
for (int i = 0 ; i < minLength ; i ++) {
203
205
int cmp = leftSegments .get (i ).compareTo (rightSegments .get (i ));
@@ -208,35 +210,30 @@ private static int compareReferences(Value left, Value right) {
208
210
return Util .compareIntegers (leftSegments .size (), rightSegments .size ());
209
211
}
210
212
211
- private static int compareGeoPoints (Value left , Value right ) {
212
- int comparison =
213
- Util .compareDoubles (
214
- left .getGeoPointValue ().getLatitude (), right .getGeoPointValue ().getLatitude ());
213
+ private static int compareGeoPoints (LatLng left , LatLng right ) {
214
+ int comparison = Util .compareDoubles (left .getLatitude (), right .getLatitude ());
215
215
if (comparison == 0 ) {
216
- return Util .compareDoubles (
217
- left .getGeoPointValue ().getLongitude (), right .getGeoPointValue ().getLongitude ());
216
+ return Util .compareDoubles (left .getLongitude (), right .getLongitude ());
218
217
}
219
218
return comparison ;
220
219
}
221
220
222
- private static int compareArrays (Value left , Value right ) {
223
- int minLength =
224
- Math .min (left .getArrayValue ().getValuesCount (), right .getArrayValue ().getValuesCount ());
221
+ private static int compareArrays (ArrayValue left , ArrayValue right ) {
222
+ int minLength = Math .min (left .getValuesCount (), right .getValuesCount ());
225
223
for (int i = 0 ; i < minLength ; i ++) {
226
- int cmp = compare (left .getArrayValue (). getValues (i ), right . getArrayValue () .getValues (i ));
224
+ int cmp = compare (left .getValues (i ), right .getValues (i ));
227
225
if (cmp != 0 ) {
228
226
return cmp ;
229
227
}
230
228
}
231
- return Util .compareIntegers (
232
- left .getArrayValue ().getValuesCount (), right .getArrayValue ().getValuesCount ());
229
+ return Util .compareIntegers (left .getValuesCount (), right .getValuesCount ());
233
230
}
234
231
235
- private static int compareMaps (Value left , Value right ) {
232
+ private static int compareMaps (MapValue left , MapValue right ) {
236
233
Iterator <Map .Entry <String , Value >> iterator1 =
237
- new TreeMap <>(left .getMapValue (). getFieldsMap ()).entrySet ().iterator ();
234
+ new TreeMap <>(left .getFieldsMap ()).entrySet ().iterator ();
238
235
Iterator <Map .Entry <String , Value >> iterator2 =
239
- new TreeMap <>(right .getMapValue (). getFieldsMap ()).entrySet ().iterator ();
236
+ new TreeMap <>(right .getFieldsMap ()).entrySet ().iterator ();
240
237
while (iterator1 .hasNext () && iterator2 .hasNext ()) {
241
238
Map .Entry <String , Value > entry1 = iterator1 .next ();
242
239
Map .Entry <String , Value > entry2 = iterator2 .next ();
0 commit comments