Skip to content

Commit 6da90d0

Browse files
committed
Implement Options.RequestIdOption.(equals, hashCode)
1 parent 633c3b0 commit 6da90d0

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Options.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,12 +1088,20 @@ void appendToOptions(Options options) {
10881088

10891089
@Override
10901090
public int hashCode() {
1091-
return RequestIdOption.class.hashCode();
1091+
return this.reqId.hashCode();
10921092
}
10931093

10941094
@Override
10951095
public boolean equals(Object o) {
1096-
return o instanceof RequestIdOption;
1096+
// instanceof for a null object returns false.
1097+
if (!(o instanceof RequestIdOption)) {
1098+
return false;
1099+
}
1100+
RequestIdOption other = (RequestIdOption) o;
1101+
if (this.reqId == null || other.reqId == null) {
1102+
return this.reqId == null && other.reqId == null;
1103+
}
1104+
return this.reqId.equals(other.reqId);
10971105
}
10981106
}
10991107
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/OptionsTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,22 @@ public void testRequestId() {
900900
assertThat(option3.toString()).doesNotContain("requestId");
901901
}
902902

903+
@Test
904+
public void testRequestIdOptionEqualsAndHashCode() {
905+
XGoogSpannerRequestId reqId1 = XGoogSpannerRequestId.of(1, 2, 3, 4);
906+
XGoogSpannerRequestId reqId2 = XGoogSpannerRequestId.of(2, 3, 4, 5);
907+
Options.RequestIdOption opt1 = Options.requestId(reqId1);
908+
Options.RequestIdOption opt1Prime = Options.requestId(reqId1);
909+
Options.RequestIdOption opt2 = Options.requestId(reqId2);
910+
911+
assertTrue(opt1.equals(opt1));
912+
assertTrue(opt1.equals(opt1Prime));
913+
assertEquals(opt1.hashCode(), opt1Prime.hashCode());
914+
assertFalse(opt1.equals(opt2));
915+
assertNotEquals(opt1, opt2);
916+
assertNotEquals(opt1.hashCode(), opt2.hashCode());
917+
}
918+
903919
@Test
904920
public void testOptions_WithMultipleDifferentRequestIds() {
905921
XGoogSpannerRequestId reqId1 = XGoogSpannerRequestId.of(1, 1, 1, 1);

0 commit comments

Comments
 (0)