Skip to content

Commit 6e4ddde

Browse files
Format
1 parent 7bdc88a commit 6e4ddde

File tree

1 file changed

+25
-29
lines changed
  • firebase-firestore/src/main/java/com/google/firebase/firestore/model/value

1 file changed

+25
-29
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/model/value/ProtoValues.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.google.firestore.v1.ArrayValue;
2323
import com.google.firestore.v1.MapValue;
2424
import com.google.firestore.v1.Value;
25+
import com.google.protobuf.Timestamp;
26+
import com.google.type.LatLng;
2527
import java.util.Iterator;
2628
import java.util.List;
2729
import java.util.Map;
@@ -148,19 +150,19 @@ public static int compare(Value left, Value right) {
148150
case FieldValue.TYPE_ORDER_NUMBER:
149151
return compareNumbers(left, right);
150152
case FieldValue.TYPE_ORDER_TIMESTAMP:
151-
return compareTimestamps(left, right);
153+
return compareTimestamps(left.getTimestampValue(), right.getTimestampValue());
152154
case FieldValue.TYPE_ORDER_STRING:
153155
return left.getStringValue().compareTo(right.getStringValue());
154156
case FieldValue.TYPE_ORDER_BLOB:
155157
return Util.compareByteStrings(left.getBytesValue(), right.getBytesValue());
156158
case FieldValue.TYPE_ORDER_REFERENCE:
157-
return compareReferences(left, right);
159+
return compareReferences(left.getReferenceValue(), right.getReferenceValue());
158160
case FieldValue.TYPE_ORDER_GEOPOINT:
159-
return compareGeoPoints(left, right);
161+
return compareGeoPoints(left.getGeoPointValue(), right.getGeoPointValue());
160162
case FieldValue.TYPE_ORDER_ARRAY:
161-
return compareArrays(left, right);
163+
return compareArrays(left.getArrayValue(), right.getArrayValue());
162164
case FieldValue.TYPE_ORDER_OBJECT:
163-
return compareMaps(left, right);
165+
return compareMaps(left.getMapValue(), right.getMapValue());
164166
default:
165167
throw fail("Invalid value type: " + leftType);
166168
}
@@ -186,18 +188,17 @@ private static int compareNumbers(Value left, Value right) {
186188
throw fail("Unexpected values: %s vs %s", left, right);
187189
}
188190

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;
193195
}
194-
return Long.signum(
195-
left.getTimestampValue().getSeconds() - right.getTimestampValue().getSeconds());
196+
return Util.compareIntegers(left.getNanos(), right.getNanos());
196197
}
197198

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);
201202
int minLength = Math.min(leftSegments.size(), rightSegments.size());
202203
for (int i = 0; i < minLength; i++) {
203204
int cmp = leftSegments.get(i).compareTo(rightSegments.get(i));
@@ -208,35 +209,30 @@ private static int compareReferences(Value left, Value right) {
208209
return Util.compareIntegers(leftSegments.size(), rightSegments.size());
209210
}
210211

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());
215214
if (comparison == 0) {
216-
return Util.compareDoubles(
217-
left.getGeoPointValue().getLongitude(), right.getGeoPointValue().getLongitude());
215+
return Util.compareDoubles(left.getLongitude(), right.getLongitude());
218216
}
219217
return comparison;
220218
}
221219

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());
225222
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));
227224
if (cmp != 0) {
228225
return cmp;
229226
}
230227
}
231-
return Util.compareIntegers(
232-
left.getArrayValue().getValuesCount(), right.getArrayValue().getValuesCount());
228+
return Util.compareIntegers(left.getValuesCount(), right.getValuesCount());
233229
}
234230

235-
private static int compareMaps(Value left, Value right) {
231+
private static int compareMaps(MapValue left, MapValue right) {
236232
Iterator<Map.Entry<String, Value>> iterator1 =
237-
new TreeMap<>(left.getMapValue().getFieldsMap()).entrySet().iterator();
233+
new TreeMap<>(left.getFieldsMap()).entrySet().iterator();
238234
Iterator<Map.Entry<String, Value>> iterator2 =
239-
new TreeMap<>(right.getMapValue().getFieldsMap()).entrySet().iterator();
235+
new TreeMap<>(right.getFieldsMap()).entrySet().iterator();
240236
while (iterator1.hasNext() && iterator2.hasNext()) {
241237
Map.Entry<String, Value> entry1 = iterator1.next();
242238
Map.Entry<String, Value> entry2 = iterator2.next();

0 commit comments

Comments
 (0)