Skip to content

Commit 51a54c0

Browse files
Adding Equals to the Public API + Allowing empty merges + Test Fix (googleapis#2971)
1 parent 4164193 commit 51a54c0

File tree

18 files changed

+406
-177
lines changed

18 files changed

+406
-177
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/Blob.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,21 @@ public byte[] toBytes() {
7474
return byteString.toByteArray();
7575
}
7676

77+
/**
78+
* Returns true if this Blob is equal to the provided object.
79+
*
80+
* @param obj The object to compare against.
81+
* @return Whether this Blob is equal to the provided object.
82+
*/
7783
@Override
78-
public boolean equals(Object o) {
79-
if (this == o) {
84+
public boolean equals(Object obj) {
85+
if (this == obj) {
8086
return true;
8187
}
82-
if (o == null || getClass() != o.getClass()) {
88+
if (obj == null || getClass() != obj.getClass()) {
8389
return false;
8490
}
85-
Blob blob = (Blob) o;
91+
Blob blob = (Blob) obj;
8692
return Objects.equals(byteString, blob.byteString);
8793
}
8894

google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentChange.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.firestore;
1818

19+
import java.util.Objects;
1920
import javax.annotation.Nonnull;
2021

2122
/**
@@ -87,4 +88,30 @@ public int getOldIndex() {
8788
public int getNewIndex() {
8889
return newIndex;
8990
}
91+
92+
/**
93+
* Returns true if this DocumentChange is equal to the provided object.
94+
*
95+
* @param obj The object to compare against.
96+
* @return Whether this DocumentChange is equal to the provided object.
97+
*/
98+
@Override
99+
public boolean equals(Object obj) {
100+
if (this == obj) {
101+
return true;
102+
}
103+
if (obj == null || getClass() != obj.getClass()) {
104+
return false;
105+
}
106+
DocumentChange that = (DocumentChange) obj;
107+
return oldIndex == that.oldIndex
108+
&& newIndex == that.newIndex
109+
&& type == that.type
110+
&& Objects.equals(document, that.document);
111+
}
112+
113+
@Override
114+
public int hashCode() {
115+
return Objects.hash(type, document, oldIndex, newIndex);
116+
}
90117
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentReference.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,21 @@ public String toString() {
449449
return String.format("DocumentReference{path=%s}", path);
450450
}
451451

452+
/**
453+
* Returns true if this DocumentReference is equal to the provided object.
454+
*
455+
* @param obj The object to compare against.
456+
* @return Whether this DocumentReference is equal to the provided object.
457+
*/
452458
@Override
453-
public boolean equals(Object o) {
454-
if (this == o) {
459+
public boolean equals(Object obj) {
460+
if (this == obj) {
455461
return true;
456462
}
457-
if (o == null || getClass() != o.getClass()) {
463+
if (obj == null || getClass() != obj.getClass()) {
458464
return false;
459465
}
460-
DocumentReference that = (DocumentReference) o;
466+
DocumentReference that = (DocumentReference) obj;
461467
return Objects.equals(path, that.path) && Objects.equals(firestore, that.firestore);
462468
}
463469

google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSet.java

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -132,57 +132,4 @@ List<QueryDocumentSnapshot> toList() {
132132
public Iterator<QueryDocumentSnapshot> iterator() {
133133
return sortedSet.iterator();
134134
}
135-
136-
@Override
137-
public boolean equals(Object other) {
138-
if (this == other) {
139-
return true;
140-
}
141-
if (other == null || getClass() != other.getClass()) {
142-
return false;
143-
}
144-
145-
DocumentSet documentSet = (DocumentSet) other;
146-
147-
if (size() != documentSet.size()) {
148-
return false;
149-
}
150-
151-
Iterator<QueryDocumentSnapshot> thisIterator = iterator();
152-
Iterator<QueryDocumentSnapshot> otherIterator = documentSet.iterator();
153-
while (thisIterator.hasNext()) {
154-
DocumentSnapshot thisDoc = thisIterator.next();
155-
DocumentSnapshot otherDoc = otherIterator.next();
156-
if (!thisDoc.equals(otherDoc)) {
157-
return false;
158-
}
159-
}
160-
161-
return true;
162-
}
163-
164-
@Override
165-
public int hashCode() {
166-
int result = 0;
167-
for (DocumentSnapshot document : this) {
168-
result = 31 * result + document.hashCode();
169-
}
170-
return result;
171-
}
172-
173-
@Override
174-
public String toString() {
175-
StringBuilder builder = new StringBuilder("[");
176-
boolean first = true;
177-
for (DocumentSnapshot doc : this) {
178-
if (first) {
179-
first = false;
180-
} else {
181-
builder.append(", ");
182-
}
183-
builder.append(doc);
184-
}
185-
builder.append("]");
186-
return builder.toString();
187-
}
188135
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -411,25 +411,29 @@ Write.Builder toPb() {
411411
return write;
412412
}
413413

414+
/**
415+
* Returns true if the document's data and path in this DocumentSnapshot equals the provided
416+
* snapshot.
417+
*
418+
* @param obj The object to compare against.
419+
* @return Whether this DocumentSnapshot is equal to the provided object.
420+
*/
414421
@Override
415-
public boolean equals(Object o) {
416-
if (this == o) {
422+
public boolean equals(Object obj) {
423+
if (this == obj) {
417424
return true;
418425
}
419-
if (o == null || getClass() != o.getClass()) {
426+
if (obj == null || !(obj instanceof DocumentSnapshot)) {
420427
return false;
421428
}
422-
DocumentSnapshot that = (DocumentSnapshot) o;
429+
DocumentSnapshot that = (DocumentSnapshot) obj;
423430
return Objects.equals(firestore, that.firestore)
424431
&& Objects.equals(docRef, that.docRef)
425-
&& Objects.equals(fields, that.fields)
426-
&& Objects.equals(readTime, that.readTime)
427-
&& Objects.equals(updateTime, that.updateTime)
428-
&& Objects.equals(createTime, that.createTime);
432+
&& Objects.equals(fields, that.fields);
429433
}
430434

431435
@Override
432436
public int hashCode() {
433-
return Objects.hash(firestore, docRef, fields, readTime, updateTime, createTime);
437+
return Objects.hash(firestore, docRef, fields);
434438
}
435439
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/FieldValue.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public abstract class FieldValue {
2424
static final Object SERVER_TIMESTAMP_SENTINEL = new Object();
2525
static final Object DELETE_SENTINEL = new Object();
2626

27-
FieldValue() {}
27+
private FieldValue() {}
2828

2929
/**
3030
* Returns a sentinel used with set() or update() to include a server-generated timestamp in the
@@ -40,4 +40,20 @@ public static Object serverTimestamp() {
4040
public static Object delete() {
4141
return DELETE_SENTINEL;
4242
}
43+
44+
/**
45+
* Returns true if this FieldValue is equal to the provided object.
46+
*
47+
* @param obj The object to compare against.
48+
* @return Whether this FieldValue is equal to the provided object.
49+
*/
50+
@Override
51+
public boolean equals(Object obj) {
52+
return this == obj;
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
return super.hashCode();
58+
}
4359
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/GeoPoint.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.Serializable;
2222
import java.util.Objects;
2323
import javax.annotation.Nonnull;
24-
import javax.annotation.Nullable;
2524

2625
/** Immutable class representing a geographic location in Firestore */
2726
public class GeoPoint implements Serializable {
@@ -77,16 +76,21 @@ LatLng toProto() {
7776
public String toString() {
7877
return "GeoPoint { latitude=" + this.latitude + ", longitude=" + this.longitude + " }";
7978
}
80-
79+
/**
80+
* Returns true if this GeoPoint is equal to the provided object.
81+
*
82+
* @param obj The object to compare against.
83+
* @return Whether this GeoPoint is equal to the provided object.
84+
*/
8185
@Override
82-
public boolean equals(@Nullable Object o) {
83-
if (this == o) {
86+
public boolean equals(Object obj) {
87+
if (this == obj) {
8488
return true;
8589
}
86-
if (o == null || getClass() != o.getClass()) {
90+
if (obj == null || getClass() != obj.getClass()) {
8791
return false;
8892
}
89-
GeoPoint geoPoint = (GeoPoint) o;
93+
GeoPoint geoPoint = (GeoPoint) obj;
9094
return Double.compare(geoPoint.latitude, latitude) == 0
9195
&& Double.compare(geoPoint.longitude, longitude) == 0;
9296
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/Precondition.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,21 @@ com.google.firestore.v1beta1.Precondition toPb() {
8585
return precondition.build();
8686
}
8787

88+
/**
89+
* Returns true if this Precondition is equal to the provided object.
90+
*
91+
* @param obj The object to compare against.
92+
* @return Whether this Precondition is equal to the provided object.
93+
*/
8894
@Override
89-
public boolean equals(Object o) {
90-
if (this == o) {
95+
public boolean equals(Object obj) {
96+
if (this == obj) {
9197
return true;
9298
}
93-
if (o == null || getClass() != o.getClass()) {
99+
if (obj == null || getClass() != obj.getClass()) {
94100
return false;
95101
}
96-
Precondition that = (Precondition) o;
102+
Precondition that = (Precondition) obj;
97103
return Objects.equals(exists, that.exists) && Objects.equals(updateTime, that.updateTime);
98104
}
99105

google-cloud-firestore/src/main/java/com/google/cloud/firestore/Query.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.api.core.ApiFuture;
2626
import com.google.api.core.SettableApiFuture;
2727
import com.google.api.gax.rpc.ApiStreamObserver;
28-
import com.google.cloud.firestore.DocumentChange.Type;
2928
import com.google.common.base.Preconditions;
3029
import com.google.firestore.v1beta1.Cursor;
3130
import com.google.firestore.v1beta1.Document;
@@ -996,13 +995,10 @@ ApiFuture<QuerySnapshot> get(@Nullable ByteString transactionId) {
996995
stream(
997996
new QuerySnapshotObserver() {
998997
List<QueryDocumentSnapshot> documentSnapshots = new ArrayList<>();
999-
List<DocumentChange> documentChanges = new ArrayList<>();
1000998

1001999
@Override
10021000
public void onNext(QueryDocumentSnapshot documentSnapshot) {
10031001
documentSnapshots.add(documentSnapshot);
1004-
documentChanges.add(
1005-
new DocumentChange(documentSnapshot, Type.ADDED, -1, documentSnapshots.size() - 1));
10061002
}
10071003

10081004
@Override
@@ -1013,8 +1009,7 @@ public void onError(Throwable throwable) {
10131009
@Override
10141010
public void onCompleted() {
10151011
QuerySnapshot querySnapshot =
1016-
new QuerySnapshot(
1017-
Query.this, this.getReadTime(), documentSnapshots, documentChanges);
1012+
QuerySnapshot.withDocuments(Query.this, this.getReadTime(), documentSnapshots);
10181013
result.set(querySnapshot);
10191014
}
10201015
},
@@ -1071,15 +1066,21 @@ ResourcePath getResourcePath() {
10711066
return path;
10721067
}
10731068

1069+
/**
1070+
* Returns true if this Query is equal to the provided object.
1071+
*
1072+
* @param obj The object to compare against.
1073+
* @return Whether this Query is equal to the provided object.
1074+
*/
10741075
@Override
1075-
public boolean equals(Object o) {
1076-
if (this == o) {
1076+
public boolean equals(Object obj) {
1077+
if (this == obj) {
10771078
return true;
10781079
}
1079-
if (o == null || getClass() != o.getClass()) {
1080+
if (obj == null || !(obj instanceof Query)) {
10801081
return false;
10811082
}
1082-
Query query = (Query) o;
1083+
Query query = (Query) obj;
10831084
return Objects.equals(path, query.path)
10841085
&& Objects.equals(firestore, query.firestore)
10851086
&& Objects.equals(options, query.options);

0 commit comments

Comments
 (0)