Skip to content

Commit 01b03ea

Browse files
Added equals/hashCode/toString methods to all bean objects (#127)
This makes unit testing easier for consumers. It shouldn't change any behaviors. I generated everything with Android Studio. The only places I saw equals/hashCode/toString already had them directly after constructors, and ordered them: toString equals hashCode so I preserved that position and order (and moved AssignmentCacheEntry's existing equals/hashCode to make it match that order).
1 parent e742f86 commit 01b03ea

24 files changed

+598
-23
lines changed

src/main/java/cloud/eppo/BanditEvaluationResult.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cloud.eppo;
22

3+
import java.util.Objects;
4+
35
import cloud.eppo.api.DiscriminableAttributes;
46

57
public class BanditEvaluationResult {
@@ -35,6 +37,40 @@ public BanditEvaluationResult(
3537
this.optimalityGap = optimalityGap;
3638
}
3739

40+
@Override
41+
public String toString() {
42+
return "BanditEvaluationResult{" +
43+
"flagKey='" + flagKey + '\'' +
44+
", subjectKey='" + subjectKey + '\'' +
45+
", subjectAttributes=" + subjectAttributes +
46+
", actionKey='" + actionKey + '\'' +
47+
", actionAttributes=" + actionAttributes +
48+
", actionScore=" + actionScore +
49+
", actionWeight=" + actionWeight +
50+
", gamma=" + gamma +
51+
", optimalityGap=" + optimalityGap +
52+
'}';
53+
}
54+
55+
@Override
56+
public boolean equals(Object o) {
57+
if (o == null || getClass() != o.getClass()) return false;
58+
BanditEvaluationResult that = (BanditEvaluationResult) o;
59+
return Double.compare(actionScore, that.actionScore) == 0
60+
&& Double.compare(actionWeight, that.actionWeight) == 0
61+
&& Double.compare(gamma, that.gamma) == 0
62+
&& Double.compare(optimalityGap, that.optimalityGap) == 0
63+
&& Objects.equals(flagKey, that.flagKey)
64+
&& Objects.equals(subjectKey, that.subjectKey)
65+
&& Objects.equals(subjectAttributes, that.subjectAttributes)
66+
&& Objects.equals(actionKey, that.actionKey) && Objects.equals(actionAttributes, that.actionAttributes);
67+
}
68+
69+
@Override
70+
public int hashCode() {
71+
return Objects.hash(flagKey, subjectKey, subjectAttributes, actionKey, actionAttributes, actionScore, actionWeight, gamma, optimalityGap);
72+
}
73+
3874
public String getFlagKey() {
3975
return flagKey;
4076
}

src/main/java/cloud/eppo/FlagEvaluationResult.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cloud.eppo.api.Attributes;
44
import cloud.eppo.ufc.dto.Variation;
55
import java.util.Map;
6+
import java.util.Objects;
67

78
public class FlagEvaluationResult {
89

@@ -31,6 +32,37 @@ public FlagEvaluationResult(
3132
this.doLog = doLog;
3233
}
3334

35+
@Override
36+
public String toString() {
37+
return "FlagEvaluationResult{" +
38+
"flagKey='" + flagKey + '\'' +
39+
", subjectKey='" + subjectKey + '\'' +
40+
", subjectAttributes=" + subjectAttributes +
41+
", allocationKey='" + allocationKey + '\'' +
42+
", variation=" + variation +
43+
", extraLogging=" + extraLogging +
44+
", doLog=" + doLog +
45+
'}';
46+
}
47+
48+
@Override
49+
public boolean equals(Object o) {
50+
if (o == null || getClass() != o.getClass()) return false;
51+
FlagEvaluationResult that = (FlagEvaluationResult) o;
52+
return doLog == that.doLog
53+
&& Objects.equals(flagKey, that.flagKey)
54+
&& Objects.equals(subjectKey, that.subjectKey)
55+
&& Objects.equals(subjectAttributes, that.subjectAttributes)
56+
&& Objects.equals(allocationKey, that.allocationKey)
57+
&& Objects.equals(variation, that.variation)
58+
&& Objects.equals(extraLogging, that.extraLogging);
59+
}
60+
61+
@Override
62+
public int hashCode() {
63+
return Objects.hash(flagKey, subjectKey, subjectAttributes, allocationKey, variation, extraLogging, doLog);
64+
}
65+
3466
public String getFlagKey() {
3567
return flagKey;
3668
}

src/main/java/cloud/eppo/SDKKey.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.Collections;
66
import java.util.HashMap;
77
import java.util.Map;
8+
import java.util.Objects;
9+
810
import org.slf4j.Logger;
911
import org.slf4j.LoggerFactory;
1012

@@ -24,6 +26,27 @@ public SDKKey(String sdkToken) {
2426
this.decodedParams = decodeToken(sdkToken);
2527
}
2628

29+
@Override
30+
public String toString() {
31+
return "SDKKey{" +
32+
"sdkTokenString='" + sdkTokenString + '\'' +
33+
", decodedParams=" + decodedParams +
34+
'}';
35+
}
36+
37+
@Override
38+
public boolean equals(Object o) {
39+
if (o == null || getClass() != o.getClass()) return false;
40+
SDKKey sdkKey = (SDKKey) o;
41+
return Objects.equals(sdkTokenString, sdkKey.sdkTokenString) &&
42+
Objects.equals(decodedParams, sdkKey.decodedParams);
43+
}
44+
45+
@Override
46+
public int hashCode() {
47+
return Objects.hash(sdkTokenString, decodedParams);
48+
}
49+
2750
private Map<String, String> decodeToken(String token) {
2851
try {
2952
String[] parts = token.split("\\.");

src/main/java/cloud/eppo/api/BanditResult.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cloud.eppo.api;
22

3+
import java.util.Objects;
4+
35
public class BanditResult {
46
private final String variation;
57
private final String action;
@@ -9,6 +11,27 @@ public BanditResult(String variation, String action) {
911
this.action = action;
1012
}
1113

14+
@Override
15+
public String toString() {
16+
return "BanditResult{" +
17+
"variation='" + variation + '\'' +
18+
", action='" + action + '\'' +
19+
'}';
20+
}
21+
22+
@Override
23+
public boolean equals(Object o) {
24+
if (o == null || getClass() != o.getClass()) return false;
25+
BanditResult that = (BanditResult) o;
26+
return Objects.equals(variation, that.variation)
27+
&& Objects.equals(action, that.action);
28+
}
29+
30+
@Override
31+
public int hashCode() {
32+
return Objects.hash(variation, action);
33+
}
34+
1235
public String getVariation() {
1336
return variation;
1437
}

src/main/java/cloud/eppo/api/Configuration.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import com.fasterxml.jackson.databind.ObjectMapper;
99
import com.fasterxml.jackson.databind.node.ObjectNode;
1010
import java.io.*;
11+
import java.util.Arrays;
1112
import java.util.Collections;
1213
import java.util.Map;
14+
import java.util.Objects;
1315
import java.util.Set;
1416
import java.util.stream.Collectors;
1517
import org.jetbrains.annotations.Nullable;
@@ -107,6 +109,35 @@ public static Configuration emptyConfig() {
107109
null);
108110
}
109111

112+
@Override
113+
public String toString() {
114+
return "Configuration{" +
115+
"banditReferences=" + banditReferences +
116+
", flags=" + flags +
117+
", bandits=" + bandits +
118+
", isConfigObfuscated=" + isConfigObfuscated +
119+
", flagConfigJson=" + Arrays.toString(flagConfigJson) +
120+
", banditParamsJson=" + Arrays.toString(banditParamsJson) +
121+
'}';
122+
}
123+
124+
@Override
125+
public boolean equals(Object o) {
126+
if (o == null || getClass() != o.getClass()) return false;
127+
Configuration that = (Configuration) o;
128+
return isConfigObfuscated == that.isConfigObfuscated
129+
&& Objects.equals(banditReferences, that.banditReferences)
130+
&& Objects.equals(flags, that.flags)
131+
&& Objects.equals(bandits, that.bandits)
132+
&& Objects.deepEquals(flagConfigJson, that.flagConfigJson)
133+
&& Objects.deepEquals(banditParamsJson, that.banditParamsJson);
134+
}
135+
136+
@Override
137+
public int hashCode() {
138+
return Objects.hash(banditReferences, flags, bandits, isConfigObfuscated, Arrays.hashCode(flagConfigJson), Arrays.hashCode(banditParamsJson));
139+
}
140+
110141
public FlagConfig getFlag(String flagKey) {
111142
String flagKeyForLookup = flagKey;
112143
if (isConfigObfuscated()) {

src/main/java/cloud/eppo/cache/AssignmentCacheEntry.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ public static AssignmentCacheEntry fromBanditAssignment(BanditAssignment assignm
2727
new BanditCacheValue(assignment.getBandit(), assignment.getAction()));
2828
}
2929

30+
@Override
31+
public boolean equals(Object o) {
32+
if (o == null || getClass() != o.getClass()) return false;
33+
AssignmentCacheEntry that = (AssignmentCacheEntry) o;
34+
return Objects.equals(key, that.key)
35+
&& Objects.equals(value, that.value);
36+
}
37+
38+
@Override
39+
public int hashCode() {
40+
return Objects.hash(key, value);
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return "AssignmentCacheEntry{" +
46+
"key=" + key +
47+
", value=" + value +
48+
'}';
49+
}
50+
3051
@NotNull public AssignmentCacheKey getKey() {
3152
return key;
3253
}
@@ -43,17 +64,4 @@ public static AssignmentCacheEntry fromBanditAssignment(BanditAssignment assignm
4364
return value;
4465
}
4566

46-
@Override
47-
public boolean equals(Object o) {
48-
if (this == o) return true;
49-
if (o == null || getClass() != o.getClass()) return false;
50-
AssignmentCacheEntry that = (AssignmentCacheEntry) o;
51-
return Objects.equals(key, that.key)
52-
&& Objects.equals(value.getValueIdentifier(), that.value.getValueIdentifier());
53-
}
54-
55-
@Override
56-
public int hashCode() {
57-
return Objects.hash(key, value);
58-
}
5967
}

src/main/java/cloud/eppo/cache/AssignmentCacheKey.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,30 @@ public AssignmentCacheKey(String subjectKey, String flagKey) {
1717
this.flagKey = flagKey;
1818
}
1919

20-
public String getSubjectKey() {
21-
return subjectKey;
22-
}
23-
24-
public String getFlagKey() {
25-
return flagKey;
26-
}
27-
2820
@Override
2921
public String toString() {
3022
return subjectKey + ";" + flagKey;
3123
}
3224

3325
@Override
3426
public boolean equals(Object o) {
35-
if (this == o) return true;
3627
if (o == null || getClass() != o.getClass()) return false;
3728
AssignmentCacheKey that = (AssignmentCacheKey) o;
38-
return Objects.equals(toString(), that.toString());
29+
return Objects.equals(subjectKey, that.subjectKey)
30+
&& Objects.equals(flagKey, that.flagKey);
3931
}
4032

4133
@Override
4234
public int hashCode() {
4335
return Objects.hash(subjectKey, flagKey);
4436
}
37+
38+
public String getSubjectKey() {
39+
return subjectKey;
40+
}
41+
42+
public String getFlagKey() {
43+
return flagKey;
44+
}
45+
4546
}

src/main/java/cloud/eppo/model/ShardRange.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonProperty;
55

6+
import java.util.Objects;
7+
68
/** Shard Range Class */
79
public class ShardRange {
810
private final int start;
@@ -14,6 +16,19 @@ public ShardRange(@JsonProperty("start") int start, @JsonProperty("end") int end
1416
this.end = end;
1517
}
1618

19+
@Override
20+
public boolean equals(Object o) {
21+
if (o == null || getClass() != o.getClass()) return false;
22+
ShardRange that = (ShardRange) o;
23+
return start == that.start &&
24+
end == that.end;
25+
}
26+
27+
@Override
28+
public int hashCode() {
29+
return Objects.hash(start, end);
30+
}
31+
1732
@Override
1833
public String toString() {
1934
return "[start: " + start + "| end: " + end + "]";

src/main/java/cloud/eppo/ufc/dto/Allocation.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Date;
44
import java.util.List;
5+
import java.util.Objects;
56
import java.util.Set;
67

78
public class Allocation {
@@ -27,6 +28,35 @@ public Allocation(
2728
this.doLog = doLog;
2829
}
2930

31+
@Override
32+
public String toString() {
33+
return "Allocation{" +
34+
"key='" + key + '\'' +
35+
", rules=" + rules +
36+
", startAt=" + startAt +
37+
", endAt=" + endAt +
38+
", splits=" + splits +
39+
", doLog=" + doLog +
40+
'}';
41+
}
42+
43+
@Override
44+
public boolean equals(Object o) {
45+
if (o == null || getClass() != o.getClass()) return false;
46+
Allocation that = (Allocation) o;
47+
return doLog == that.doLog
48+
&& Objects.equals(key, that.key)
49+
&& Objects.equals(rules, that.rules)
50+
&& Objects.equals(startAt, that.startAt)
51+
&& Objects.equals(endAt, that.endAt)
52+
&& Objects.equals(splits, that.splits);
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
return Objects.hash(key, rules, startAt, endAt, splits, doLog);
58+
}
59+
3060
public String getKey() {
3161
return key;
3262
}

0 commit comments

Comments
 (0)