Skip to content

Implement Filter equals method. #5210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions firebase-firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* [feature] Expose MultiDb support in API. [#4015](//github.com/firebase/firebase-android-sdk/issues/4015)
* [fixed] Fixed a thread interference issue that may lead to a ConcurrentModificationException.
(GitHub [#5091](//github.com/firebase/firebase-android-sdk/issues/5091){: .external})
* [fixed] Implement equals method on Filter class. [#5210](//github.com/firebase/firebase-android-sdk/issues/5210)

# 24.6.1
* [feature] Implemented an optimization in the local cache synchronization logic that reduces the number of billed document reads when documents were deleted on the server while the client was not actively listening to the query (e.g. while the client was offline). (GitHub [#4982](//github.com/firebase/firebase-android-sdk/pull/4982){: .external})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.firebase.firestore.core.FieldFilter.Operator;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
* A {@code Filter} represents a restriction on one or more field values and can be used to refine
Expand Down Expand Up @@ -48,6 +49,26 @@ public Operator getOperator() {
public Object getValue() {
return value;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

UnaryFilter that = (UnaryFilter) o;

return this.operator == that.operator
&& Objects.equals(this.field, that.field)
&& Objects.equals(this.value, that.value);
}

@Override
public int hashCode() {
int result = field != null ? field.hashCode() : 0;
result = 31 * result + (operator != null ? operator.hashCode() : 0);
result = 31 * result + (value != null ? value.hashCode() : 0);
return result;
}
}

static class CompositeFilter extends Filter {
Expand All @@ -68,6 +89,23 @@ public List<Filter> getFilters() {
public com.google.firebase.firestore.core.CompositeFilter.Operator getOperator() {
return operator;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

CompositeFilter that = (CompositeFilter) o;

return this.operator == that.operator && Objects.equals(this.filters, that.filters);
}

@Override
public int hashCode() {
int result = filters != null ? filters.hashCode() : 0;
result = 31 * result + (operator != null ? operator.hashCode() : 0);
return result;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.firestore;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class FilterTest {

@Test
public void equalTo() {
Filter filter = Filter.equalTo("x", "v");
assertEquals(filter, filter);
assertEquals(filter, Filter.equalTo(FieldPath.of("x"), "v"));
assertNotEquals(filter, Filter.equalTo("x", "z"));
assertNotEquals(filter, Filter.equalTo("y", "v"));
assertNotEquals(filter, Filter.notEqualTo("x", "v"));
}

@Test
public void notEqualTo() {
Filter filter = Filter.notEqualTo("x", "v");
assertEquals(filter, filter);
assertEquals(filter, Filter.notEqualTo(FieldPath.of("x"), "v"));
assertNotEquals(filter, Filter.notEqualTo("x", "z"));
assertNotEquals(filter, Filter.notEqualTo("y", "v"));
assertNotEquals(filter, Filter.equalTo("x", "v"));
}

@Test
public void greaterThan() {
Filter filter = Filter.greaterThan("x", "v");
assertEquals(filter, filter);
assertEquals(filter, Filter.greaterThan(FieldPath.of("x"), "v"));
assertNotEquals(filter, Filter.greaterThan("x", "z"));
assertNotEquals(filter, Filter.greaterThan("y", "v"));
assertNotEquals(filter, Filter.lessThan("x", "v"));
}

@Test
public void greaterThanOrEqualTo() {
Filter filter = Filter.greaterThanOrEqualTo("x", "v");
assertEquals(filter, filter);
assertEquals(filter, Filter.greaterThanOrEqualTo(FieldPath.of("x"), "v"));
assertNotEquals(filter, Filter.greaterThanOrEqualTo("x", "z"));
assertNotEquals(filter, Filter.greaterThanOrEqualTo("y", "v"));
assertNotEquals(filter, Filter.lessThanOrEqualTo("x", "v"));
}

@Test
public void lessThan() {
Filter filter = Filter.lessThan("x", "v");
assertEquals(filter, filter);
assertEquals(filter, Filter.lessThan(FieldPath.of("x"), "v"));
assertNotEquals(filter, Filter.lessThan("x", "z"));
assertNotEquals(filter, Filter.lessThan("y", "v"));
assertNotEquals(filter, Filter.greaterThan("x", "v"));
}

@Test
public void lessThanOrEqualTo() {
Filter filter = Filter.lessThanOrEqualTo("x", "v");
assertEquals(filter, filter);
assertEquals(filter, Filter.lessThanOrEqualTo(FieldPath.of("x"), "v"));
assertNotEquals(filter, Filter.lessThanOrEqualTo("x", "z"));
assertNotEquals(filter, Filter.lessThanOrEqualTo("y", "v"));
assertNotEquals(filter, Filter.greaterThanOrEqualTo("x", "v"));
}

@Test
public void arrayContains() {
Filter filter = Filter.arrayContains("x", "v");
assertEquals(filter, filter);
assertEquals(filter, Filter.arrayContains(FieldPath.of("x"), "v"));
assertNotEquals(filter, Filter.arrayContains("x", "z"));
assertNotEquals(filter, Filter.arrayContains("y", "v"));
assertNotEquals(filter, Filter.equalTo("x", "v"));
}

@Test
public void arrayContainsAny() {
Filter filter = Filter.arrayContainsAny("x", ImmutableList.of("v1", "v2"));
assertEquals(filter, filter);
assertEquals(filter, Filter.arrayContainsAny(FieldPath.of("x"), ImmutableList.of("v1", "v2")));
assertNotEquals(filter, Filter.arrayContainsAny("x", ImmutableList.of("v2", "v1")));
assertNotEquals(filter, Filter.arrayContainsAny("x", ImmutableList.of("v2", "v3")));
assertNotEquals(filter, Filter.arrayContainsAny("y", ImmutableList.of("v1", "v2")));
assertNotEquals(filter, Filter.equalTo("x", "v"));
}

@Test
public void inArray() {
Filter filter = Filter.inArray("x", ImmutableList.of("v1", "v2"));
assertEquals(filter, filter);
assertEquals(filter, Filter.inArray(FieldPath.of("x"), ImmutableList.of("v1", "v2")));
assertNotEquals(filter, Filter.inArray("x", ImmutableList.of("v2", "v1")));
assertNotEquals(filter, Filter.inArray("x", ImmutableList.of("v2", "v3")));
assertNotEquals(filter, Filter.inArray("y", ImmutableList.of("v1", "v2")));
assertNotEquals(filter, Filter.notInArray("x", ImmutableList.of("v1", "v2")));
}

@Test
public void notInArray() {
Filter filter = Filter.notInArray("x", ImmutableList.of("v1", "v2"));
assertEquals(filter, filter);
assertEquals(filter, Filter.notInArray(FieldPath.of("x"), ImmutableList.of("v1", "v2")));
assertNotEquals(filter, Filter.notInArray("x", ImmutableList.of("v2", "v1")));
assertNotEquals(filter, Filter.notInArray("x", ImmutableList.of("v2", "v3")));
assertNotEquals(filter, Filter.notInArray("y", ImmutableList.of("v1", "v2")));
assertNotEquals(filter, Filter.inArray("x", ImmutableList.of("v1", "v2")));
}

@Test
public void or() {
Filter filter =
Filter.or(
Filter.inArray("x", ImmutableList.of("v1", "v2")),
Filter.inArray("y", ImmutableList.of("v3", "v4")));
assertEquals(
filter,
Filter.or(
Filter.inArray(FieldPath.of("x"), ImmutableList.of("v1", "v2")),
Filter.inArray(FieldPath.of("y"), ImmutableList.of("v3", "v4"))));
assertNotEquals(
filter,
Filter.and(
Filter.inArray("x", ImmutableList.of("v1", "v2")),
Filter.inArray("y", ImmutableList.of("v3", "v4"))));
assertNotEquals(
filter,
Filter.or(
Filter.inArray("y", ImmutableList.of("v3", "v4")),
Filter.inArray("x", ImmutableList.of("v1", "v2"))));
}

@Test
public void and() {
Filter filter =
Filter.and(
Filter.inArray("x", ImmutableList.of("v1", "v2")),
Filter.inArray("y", ImmutableList.of("v3", "v4")));
assertEquals(
filter,
Filter.and(
Filter.inArray(FieldPath.of("x"), ImmutableList.of("v1", "v2")),
Filter.inArray(FieldPath.of("y"), ImmutableList.of("v3", "v4"))));
assertNotEquals(
filter,
Filter.or(
Filter.inArray("x", ImmutableList.of("v1", "v2")),
Filter.inArray("y", ImmutableList.of("v3", "v4"))));
assertNotEquals(
filter,
Filter.and(
Filter.inArray("y", ImmutableList.of("v3", "v4")),
Filter.inArray("x", ImmutableList.of("v1", "v2"))));
}
}