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,17 @@ 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 comparison = Util . compareLongs (left .getSeconds (), right .getSeconds ());
193
+ if ( comparison != 0 ) {
194
+ return comparison ;
193
195
}
194
- return Long .signum (
195
- left .getTimestampValue ().getSeconds () - right .getTimestampValue ().getSeconds ());
196
+ return Util .compareIntegers (left .getNanos (), right .getNanos ());
196
197
}
197
198
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 () );
199
+ private static int compareReferences (String leftPath , String rightPath ) {
200
+ List <String > leftSegments = Splitter .on ('/' ).splitToList (leftPath );
201
+ List <String > rightSegments = Splitter .on ('/' ).splitToList (rightPath );
201
202
int minLength = Math .min (leftSegments .size (), rightSegments .size ());
202
203
for (int i = 0 ; i < minLength ; i ++) {
203
204
int cmp = leftSegments .get (i ).compareTo (rightSegments .get (i ));
@@ -208,35 +209,30 @@ private static int compareReferences(Value left, Value right) {
208
209
return Util .compareIntegers (leftSegments .size (), rightSegments .size ());
209
210
}
210
211
211
- private static int compareGeoPoints (Value left , Value right ) {
212
- int comparison =
213
- Util .compareDoubles (
214
- left .getGeoPointValue ().getLatitude (), right .getGeoPointValue ().getLatitude ());
212
+ private static int compareGeoPoints (LatLng left , LatLng right ) {
213
+ int comparison = Util .compareDoubles (left .getLatitude (), right .getLatitude ());
215
214
if (comparison == 0 ) {
216
- return Util .compareDoubles (
217
- left .getGeoPointValue ().getLongitude (), right .getGeoPointValue ().getLongitude ());
215
+ return Util .compareDoubles (left .getLongitude (), right .getLongitude ());
218
216
}
219
217
return comparison ;
220
218
}
221
219
222
- private static int compareArrays (Value left , Value right ) {
223
- int minLength =
224
- Math .min (left .getArrayValue ().getValuesCount (), right .getArrayValue ().getValuesCount ());
220
+ private static int compareArrays (ArrayValue left , ArrayValue right ) {
221
+ int minLength = Math .min (left .getValuesCount (), right .getValuesCount ());
225
222
for (int i = 0 ; i < minLength ; i ++) {
226
- int cmp = compare (left .getArrayValue (). getValues (i ), right . getArrayValue () .getValues (i ));
223
+ int cmp = compare (left .getValues (i ), right .getValues (i ));
227
224
if (cmp != 0 ) {
228
225
return cmp ;
229
226
}
230
227
}
231
- return Util .compareIntegers (
232
- left .getArrayValue ().getValuesCount (), right .getArrayValue ().getValuesCount ());
228
+ return Util .compareIntegers (left .getValuesCount (), right .getValuesCount ());
233
229
}
234
230
235
- private static int compareMaps (Value left , Value right ) {
231
+ private static int compareMaps (MapValue left , MapValue right ) {
236
232
Iterator <Map .Entry <String , Value >> iterator1 =
237
- new TreeMap <>(left .getMapValue (). getFieldsMap ()).entrySet ().iterator ();
233
+ new TreeMap <>(left .getFieldsMap ()).entrySet ().iterator ();
238
234
Iterator <Map .Entry <String , Value >> iterator2 =
239
- new TreeMap <>(right .getMapValue (). getFieldsMap ()).entrySet ().iterator ();
235
+ new TreeMap <>(right .getFieldsMap ()).entrySet ().iterator ();
240
236
while (iterator1 .hasNext () && iterator2 .hasNext ()) {
241
237
Map .Entry <String , Value > entry1 = iterator1 .next ();
242
238
Map .Entry <String , Value > entry2 = iterator2 .next ();
0 commit comments