Skip to content

Commit 98953a3

Browse files
Add ProtoValues.contains()
1 parent 82c0095 commit 98953a3

File tree

4 files changed

+19
-25
lines changed

4 files changed

+19
-25
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/core/ArrayContainsAnyFilter.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ public boolean matches(Document doc) {
3333
if (!(other instanceof ArrayValue)) {
3434
return false;
3535
}
36-
for (Value otherVal : other.getProto().getArrayValue().getValuesList()) {
37-
for (Value thisVal : getValue().getProto().getArrayValue().getValuesList()) {
38-
if (ProtoValues.equals(otherVal, thisVal)) {
39-
return true;
40-
}
36+
for (Value val : other.getProto().getArrayValue().getValuesList()) {
37+
if (ProtoValues.contains(getValue().getProto().getArrayValue().getValuesList(), val)) {
38+
return true;
4139
}
4240
}
4341
return false;

firebase-firestore/src/main/java/com/google/firebase/firestore/core/ArrayContainsFilter.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.google.firebase.firestore.model.value.ArrayValue;
2020
import com.google.firebase.firestore.model.value.FieldValue;
2121
import com.google.firebase.firestore.model.value.ProtoValues;
22-
import com.google.firestore.v1.Value;
2322

2423
/** A Filter that implements the array-contains operator. */
2524
public class ArrayContainsFilter extends FieldFilter {
@@ -30,14 +29,8 @@ public class ArrayContainsFilter extends FieldFilter {
3029
@Override
3130
public boolean matches(Document doc) {
3231
FieldValue other = doc.getField(getField());
33-
if (!(other instanceof ArrayValue)) {
34-
return false;
35-
}
36-
for (Value otherVal : other.getProto().getArrayValue().getValuesList()) {
37-
if (ProtoValues.equals(otherVal, getValue().getProto())) {
38-
return true;
39-
}
40-
}
41-
return false;
32+
return other instanceof ArrayValue
33+
&& ProtoValues.contains(
34+
other.getProto().getArrayValue().getValuesList(), getValue().getProto());
4235
}
4336
}

firebase-firestore/src/main/java/com/google/firebase/firestore/core/InFilter.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.google.firebase.firestore.model.value.ArrayValue;
2020
import com.google.firebase.firestore.model.value.FieldValue;
2121
import com.google.firebase.firestore.model.value.ProtoValues;
22-
import com.google.firestore.v1.Value;
2322

2423
/** A Filter that implements the IN operator. */
2524
public class InFilter extends FieldFilter {
@@ -30,14 +29,8 @@ public class InFilter extends FieldFilter {
3029
@Override
3130
public boolean matches(Document doc) {
3231
FieldValue other = doc.getField(getField());
33-
if (other == null) {
34-
return false;
35-
}
36-
for (Value otherVal : getValue().getProto().getArrayValue().getValuesList()) {
37-
if (ProtoValues.equals(otherVal, other.getProto())) {
38-
return true;
39-
}
40-
}
41-
return false;
32+
return other != null
33+
&& ProtoValues.contains(
34+
getValue().getProto().getArrayValue().getValuesList(), other.getProto());
4235
}
4336
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ private static boolean objectEquals(Value left, Value right) {
155155
return true;
156156
}
157157

158+
/** Returns true if the Value list contains the specified element. */
159+
public static boolean contains(List<Value> haystack, Value needle) {
160+
for (Value haystackEl : haystack) {
161+
if (equals(haystackEl, needle)) {
162+
return true;
163+
}
164+
}
165+
return false;
166+
}
167+
158168
public static int compare(Value left, Value right) {
159169
int leftType = typeOrder(left);
160170
int rightType = typeOrder(right);

0 commit comments

Comments
 (0)