12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- package com .google .firebase .firestore .util ;
15
+ package com .google .firebase .firestore .model ;
16
16
17
17
import static com .google .firebase .firestore .util .Assert .fail ;
18
- import static com .google .firebase .firestore .util .Assert .hardAssert ;
19
18
20
19
import androidx .annotation .Nullable ;
21
20
import com .google .common .base .Splitter ;
22
21
import com .google .firebase .firestore .model .value .FieldValue ;
22
+ import com .google .firebase .firestore .util .Util ;
23
23
import com .google .firestore .v1 .ArrayValue ;
24
24
import com .google .firestore .v1 .MapValue ;
25
25
import com .google .firestore .v1 .Value ;
28
28
import java .util .Map ;
29
29
import java .util .TreeMap ;
30
30
31
- public class ProtoValueUtil {
31
+ // TODO(mrschmidt): Make package-private
32
+ public class ProtoValues {
32
33
33
34
public static int typeOrder (Value value ) {
34
35
@@ -73,35 +74,17 @@ public static boolean equals(Value left, Value right) {
73
74
}
74
75
75
76
switch (leftType ) {
76
- case FieldValue .TYPE_ORDER_ARRAY :
77
- return arrayEquals (left , right );
78
77
case FieldValue .TYPE_ORDER_NUMBER :
79
78
return numberEquals (left , right );
79
+ case FieldValue .TYPE_ORDER_ARRAY :
80
+ return arrayEquals (left , right );
80
81
case FieldValue .TYPE_ORDER_OBJECT :
81
82
return objectEquals (left , right );
82
83
default :
83
84
return left .equals (right );
84
85
}
85
86
}
86
87
87
- private static boolean objectEquals (Value left , Value right ) {
88
- MapValue leftMap = left .getMapValue ();
89
- MapValue rightMap = right .getMapValue ();
90
-
91
- if (leftMap .getFieldsCount () != rightMap .getFieldsCount ()) {
92
- return false ;
93
- }
94
-
95
- for (Map .Entry <String , Value > entry : leftMap .getFieldsMap ().entrySet ()) {
96
- Value otherEntry = rightMap .getFieldsMap ().get (entry .getKey ());
97
- if (!entry .getValue ().equals (otherEntry )) {
98
- return false ;
99
- }
100
- }
101
-
102
- return true ;
103
- }
104
-
105
88
private static boolean numberEquals (Value left , Value right ) {
106
89
if (left .getValueTypeCase () == Value .ValueTypeCase .INTEGER_VALUE
107
90
&& right .getValueTypeCase () == Value .ValueTypeCase .INTEGER_VALUE ) {
@@ -132,6 +115,24 @@ private static boolean arrayEquals(Value left, Value right) {
132
115
return true ;
133
116
}
134
117
118
+ private static boolean objectEquals (Value left , Value right ) {
119
+ MapValue leftMap = left .getMapValue ();
120
+ MapValue rightMap = right .getMapValue ();
121
+
122
+ if (leftMap .getFieldsCount () != rightMap .getFieldsCount ()) {
123
+ return false ;
124
+ }
125
+
126
+ for (Map .Entry <String , Value > entry : leftMap .getFieldsMap ().entrySet ()) {
127
+ Value otherEntry = rightMap .getFieldsMap ().get (entry .getKey ());
128
+ if (!entry .getValue ().equals (otherEntry )) {
129
+ return false ;
130
+ }
131
+ }
132
+
133
+ return true ;
134
+ }
135
+
135
136
public static int compare (Value left , Value right ) {
136
137
int leftType = typeOrder (left );
137
138
int rightType = typeOrder (right );
@@ -152,7 +153,7 @@ public static int compare(Value left, Value right) {
152
153
case FieldValue .TYPE_ORDER_STRING :
153
154
return left .getStringValue ().compareTo (right .getStringValue ());
154
155
case FieldValue .TYPE_ORDER_BLOB :
155
- return Util .compareByteString (left .getBytesValue (), right .getBytesValue ());
156
+ return Util .compareByteStrings (left .getBytesValue (), right .getBytesValue ());
156
157
case FieldValue .TYPE_ORDER_REFERENCE :
157
158
return compareReferences (left , right );
158
159
case FieldValue .TYPE_ORDER_GEOPOINT :
@@ -166,39 +167,46 @@ public static int compare(Value left, Value right) {
166
167
}
167
168
}
168
169
169
- private static int compareMaps (Value left , Value right ) {
170
- Iterator <Map .Entry <String , Value >> iterator1 =
171
- new TreeMap <>(left .getMapValue ().getFieldsMap ()).entrySet ().iterator ();
172
- Iterator <Map .Entry <String , Value >> iterator2 =
173
- new TreeMap <>(right .getMapValue ().getFieldsMap ()).entrySet ().iterator ();
174
- while (iterator1 .hasNext () && iterator2 .hasNext ()) {
175
- Map .Entry <String , Value > entry1 = iterator1 .next ();
176
- Map .Entry <String , Value > entry2 = iterator2 .next ();
177
- int keyCompare = entry1 .getKey ().compareTo (entry2 .getKey ());
178
- if (keyCompare != 0 ) {
179
- return keyCompare ;
170
+ private static int compareNumbers (Value left , Value right ) {
171
+ if (left .getValueTypeCase () == Value .ValueTypeCase .DOUBLE_VALUE ) {
172
+ double thisDouble = left .getDoubleValue ();
173
+ if (right .getValueTypeCase () == Value .ValueTypeCase .DOUBLE_VALUE ) {
174
+ return Util .compareDoubles (thisDouble , right .getDoubleValue ());
175
+ } else if (right .getValueTypeCase () == Value .ValueTypeCase .INTEGER_VALUE ) {
176
+ return Util .compareMixed (thisDouble , right .getIntegerValue ());
180
177
}
181
- int valueCompare = compare (entry1 .getValue (), entry2 .getValue ());
182
- if (valueCompare != 0 ) {
183
- return valueCompare ;
178
+ } else if (left .getValueTypeCase () == Value .ValueTypeCase .INTEGER_VALUE ) {
179
+ long thisLong = left .getIntegerValue ();
180
+ if (right .getValueTypeCase () == Value .ValueTypeCase .INTEGER_VALUE ) {
181
+ return Util .compareLongs (thisLong , right .getIntegerValue ());
182
+ } else if (right .getValueTypeCase () == Value .ValueTypeCase .DOUBLE_VALUE ) {
183
+ return -1 * Util .compareMixed (right .getDoubleValue (), thisLong );
184
184
}
185
185
}
186
186
187
- // Only equal if both iterators are exhausted.
188
- return Util .compareBooleans (iterator1 .hasNext (), iterator2 .hasNext ());
187
+ throw fail ("Unexpected values: %s vs %s" , left , right );
189
188
}
190
189
191
- private static int compareArrays (Value left , Value right ) {
192
- int minLength =
193
- Math .min (left .getArrayValue ().getValuesCount (), right .getArrayValue ().getValuesCount ());
190
+ private static int compareTimestamps (Value left , Value right ) {
191
+ if (left .getTimestampValue ().getSeconds () == right .getTimestampValue ().getSeconds ()) {
192
+ return Integer .signum (
193
+ left .getTimestampValue ().getNanos () - right .getTimestampValue ().getNanos ());
194
+ }
195
+ return Long .signum (
196
+ left .getTimestampValue ().getSeconds () - right .getTimestampValue ().getSeconds ());
197
+ }
198
+
199
+ private static int compareReferences (Value left , Value right ) {
200
+ List <String > leftSegments = Splitter .on ('/' ).splitToList (left .getReferenceValue ());
201
+ List <String > rightSegments = Splitter .on ('/' ).splitToList (right .getReferenceValue ());
202
+ int minLength = Math .min (leftSegments .size (), rightSegments .size ());
194
203
for (int i = 0 ; i < minLength ; i ++) {
195
- int cmp = compare ( left . getArrayValue (). getValues ( i ), right . getArrayValue (). getValues (i ));
204
+ int cmp = leftSegments . get ( i ). compareTo ( rightSegments . get (i ));
196
205
if (cmp != 0 ) {
197
206
return cmp ;
198
207
}
199
208
}
200
- return Util .compareIntegers (
201
- left .getArrayValue ().getValuesCount (), right .getArrayValue ().getValuesCount ());
209
+ return Util .compareIntegers (leftSegments .size (), rightSegments .size ());
202
210
}
203
211
204
212
private static int compareGeoPoints (Value left , Value right ) {
@@ -212,55 +220,38 @@ private static int compareGeoPoints(Value left, Value right) {
212
220
return comparison ;
213
221
}
214
222
215
- private static int compareReferences (Value left , Value right ) {
216
- List <String > leftSegments = Splitter .on ('/' ).splitToList (left .getReferenceValue ());
217
- List <String > rightSegments = Splitter .on ('/' ).splitToList (right .getReferenceValue ());
218
- int minLength = Math .min (leftSegments .size (), rightSegments .size ());
223
+ private static int compareArrays (Value left , Value right ) {
224
+ int minLength =
225
+ Math .min (left .getArrayValue ().getValuesCount (), right .getArrayValue ().getValuesCount ());
219
226
for (int i = 0 ; i < minLength ; i ++) {
220
- int cmp = leftSegments . get ( i ). compareTo ( rightSegments . get (i ));
227
+ int cmp = compare ( left . getArrayValue (). getValues ( i ), right . getArrayValue (). getValues (i ));
221
228
if (cmp != 0 ) {
222
229
return cmp ;
223
230
}
224
231
}
225
- return Util .compareIntegers (leftSegments .size (), rightSegments .size ());
226
- }
227
-
228
- private static int compareTimestamps (Value left , Value right ) {
229
- if (left .getTimestampValue ().getSeconds () == right .getTimestampValue ().getSeconds ()) {
230
- return Integer .signum (
231
- left .getTimestampValue ().getNanos () - right .getTimestampValue ().getNanos ());
232
- }
233
- return Long .signum (
234
- left .getTimestampValue ().getSeconds () - right .getTimestampValue ().getSeconds ());
232
+ return Util .compareIntegers (
233
+ left .getArrayValue ().getValuesCount (), right .getArrayValue ().getValuesCount ());
235
234
}
236
235
237
- private static int compareNumbers (Value left , Value right ) {
238
- if ( left . getValueTypeCase () == Value . ValueTypeCase . DOUBLE_VALUE ) {
239
- double thisDouble = left .getDoubleValue ();
240
- if ( right . getValueTypeCase () == Value . ValueTypeCase . DOUBLE_VALUE ) {
241
- return Util . compareDoubles ( thisDouble , right .getDoubleValue () );
242
- } else {
243
- hardAssert (
244
- right . getValueTypeCase () == Value . ValueTypeCase . INTEGER_VALUE ,
245
- "Unexpected value type: %s" ,
246
- right );
247
- return Util . compareMixed ( thisDouble , right . getIntegerValue ()) ;
236
+ private static int compareMaps (Value left , Value right ) {
237
+ Iterator < Map . Entry < String , Value >> iterator1 =
238
+ new TreeMap <>( left .getMapValue (). getFieldsMap ()). entrySet (). iterator ();
239
+ Iterator < Map . Entry < String , Value >> iterator2 =
240
+ new TreeMap <>( right .getMapValue (). getFieldsMap ()). entrySet (). iterator ( );
241
+ while ( iterator1 . hasNext () && iterator2 . hasNext ()) {
242
+ Map . Entry < String , Value > entry1 = iterator1 . next ();
243
+ Map . Entry < String , Value > entry2 = iterator2 . next ();
244
+ int keyCompare = entry1 . getKey (). compareTo ( entry2 . getKey ());
245
+ if ( keyCompare != 0 ) {
246
+ return keyCompare ;
248
247
}
249
- } else {
250
- hardAssert (
251
- left .getValueTypeCase () == Value .ValueTypeCase .INTEGER_VALUE ,
252
- "Unexpected value type: %s" ,
253
- left );
254
- long thisLong = left .getIntegerValue ();
255
- if (right .getValueTypeCase () == Value .ValueTypeCase .INTEGER_VALUE ) {
256
- return Util .compareLongs (thisLong , right .getIntegerValue ());
257
- } else {
258
- hardAssert (
259
- right .getValueTypeCase () == Value .ValueTypeCase .DOUBLE_VALUE ,
260
- "Unexpected value type: %s" ,
261
- right );
262
- return -1 * Util .compareMixed (right .getDoubleValue (), thisLong );
248
+ int valueCompare = compare (entry1 .getValue (), entry2 .getValue ());
249
+ if (valueCompare != 0 ) {
250
+ return valueCompare ;
263
251
}
264
252
}
253
+
254
+ // Only equal if both iterators are exhausted.
255
+ return Util .compareBooleans (iterator1 .hasNext (), iterator2 .hasNext ());
265
256
}
266
257
}
0 commit comments