Skip to content

Commit 97a1e29

Browse files
Improve equals contract in Partitions plugin (#315)
* Update equals and hashCode in SimpleAttributeId and corresponding tests * Update equals and hashCode in AttributeDefinition and corresponding tests * Update equals and hashCode in AttributesPluginData and corresponding tests * Update equals and hashCode in Partition and corresponding tests * Update equals and hashCode in LabelSet and corresponding tests * Update equals and hashCode in AndFilter and corresponding tests * Update equals and hashCode in LocalFilter in AT_Filter * Update equals and hashCode in OrFilter and corresponding tests * Update equals and hashCode in NotFilter and corresponding tests * Fix typo in testEquals in AT_SimpleAttributeId * Add assertFalse(partition1 == partition2) to testEquals in AT_Partition * Fix typo in testEquals in AT_OrFilter * Add tests to show input order does not matter in testEquals and testHashCode in AndFilter * Add tests to show input order does not matter in testEquals and testHashCode in OrFilter * Update equals and hashCode in PartitionsPluginData and corresponding tests * Update equals and hashCode in AttributeFilter and corresponding tests * Update equals and hashCode in FalseFilter and corresponding tests * Update equals and hashCode in TrueFilter and corresponding tests * Minor updates to equals and testEquals for FalseFilter, TrueFilter, and PartitionsPluginData --------- Co-authored-by: shawnhatch <hatchs@leidos.com>
1 parent d122680 commit 97a1e29

25 files changed

Lines changed: 699 additions & 432 deletions

File tree

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/plugins/partitions/datamanagers/PartitionsPluginData.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package gov.hhs.aspr.ms.gcm.simulation.plugins.partitions.datamanagers;
22

3+
import java.util.Objects;
4+
35
import gov.hhs.aspr.ms.gcm.simulation.nucleus.PluginData;
46
import gov.hhs.aspr.ms.gcm.simulation.nucleus.PluginDataBuilder;
57
import gov.hhs.aspr.ms.gcm.simulation.nucleus.StandardVersioning;
@@ -76,29 +78,31 @@ private Data(Data data) {
7678
locked = data.locked;
7779
}
7880

81+
/**
82+
* Standard implementation consistent with the {@link #equals(Object)} method
83+
*/
7984
@Override
8085
public int hashCode() {
81-
82-
final int prime = 31;
83-
int result = 1;
84-
result = prime * result + (supportRunContinuity ? 1231 : 1237);
85-
return result;
86+
return Objects.hash(supportRunContinuity);
8687
}
8788

89+
/**
90+
* Two {@link Data} instances are equal if and only if
91+
* their inputs are equal.
92+
*/
8893
@Override
8994
public boolean equals(Object obj) {
90-
9195
if (this == obj) {
9296
return true;
9397
}
94-
if (!(obj instanceof Data)) {
98+
if (obj == null) {
9599
return false;
96100
}
97-
Data other = (Data) obj;
98-
if (supportRunContinuity != other.supportRunContinuity) {
101+
if (getClass() != obj.getClass()) {
99102
return false;
100103
}
101-
return true;
104+
Data other = (Data) obj;
105+
return supportRunContinuity == other.supportRunContinuity;
102106
}
103107

104108
@Override
@@ -157,27 +161,31 @@ public Builder toBuilder() {
157161
return new Builder(data);
158162
}
159163

164+
/**
165+
* Standard implementation consistent with the {@link #equals(Object)} method
166+
*/
160167
@Override
161168
public int hashCode() {
162-
final int prime = 31;
163-
int result = 1;
164-
result = prime * result + data.hashCode();
165-
return result;
169+
return Objects.hash(data);
166170
}
167171

172+
/**
173+
* Two {@link PartitionsPluginData} instances are equal if and only if
174+
* their inputs are equal.
175+
*/
168176
@Override
169177
public boolean equals(Object obj) {
170178
if (this == obj) {
171179
return true;
172180
}
173-
if (!(obj instanceof PartitionsPluginData)) {
181+
if (obj == null) {
174182
return false;
175183
}
176-
PartitionsPluginData other = (PartitionsPluginData) obj;
177-
if (!data.equals(other.data)) {
184+
if (getClass() != obj.getClass()) {
178185
return false;
179186
}
180-
return true;
187+
PartitionsPluginData other = (PartitionsPluginData) obj;
188+
return Objects.equals(data, other.data);
181189
}
182190

183191
@Override

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/plugins/partitions/support/LabelSet.java

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.LinkedHashMap;
55
import java.util.LinkedHashSet;
66
import java.util.Map;
7+
import java.util.Objects;
78
import java.util.Optional;
89
import java.util.Set;
910

@@ -28,29 +29,31 @@
2829
*/
2930
public final class LabelSet {
3031

32+
/**
33+
* Standard implementation consistent with the {@link #equals(Object)} method
34+
*/
3135
@Override
3236
public int hashCode() {
33-
final int prime = 31;
34-
int result = 1;
35-
result = prime * result + ((data.labels == null) ? 0 : data.labels.hashCode());
36-
return result;
37+
return Objects.hash(data);
3738
}
3839

40+
/**
41+
* Two {@link LabelSet} instances are equal if and only if
42+
* their inputs are equal.
43+
*/
3944
@Override
4045
public boolean equals(Object obj) {
41-
if (this == obj)
46+
if (this == obj) {
4247
return true;
43-
if (obj == null)
48+
}
49+
if (obj == null) {
4450
return false;
45-
if (getClass() != obj.getClass())
51+
}
52+
if (getClass() != obj.getClass()) {
4653
return false;
54+
}
4755
LabelSet other = (LabelSet) obj;
48-
if (data.labels == null) {
49-
if (other.data.labels != null)
50-
return false;
51-
} else if (!data.labels.equals(other.data.labels))
52-
return false;
53-
return true;
56+
return Objects.equals(data, other.data);
5457
}
5558

5659
/**
@@ -140,6 +143,33 @@ private Data(Data data) {
140143
dimensions = Collections.unmodifiableSet(new LinkedHashSet<>(data.labels.keySet()));
141144
locked = data.locked;
142145
}
146+
147+
/**
148+
* Standard implementation consistent with the {@link #equals(Object)} method
149+
*/
150+
@Override
151+
public int hashCode() {
152+
return Objects.hash(labels, dimensions);
153+
}
154+
155+
/**
156+
* Two {@link Data} instances are equal if and only if
157+
* their inputs are equal.
158+
*/
159+
@Override
160+
public boolean equals(Object obj) {
161+
if (this == obj) {
162+
return true;
163+
}
164+
if (obj == null) {
165+
return false;
166+
}
167+
if (getClass() != obj.getClass()) {
168+
return false;
169+
}
170+
Data other = (Data) obj;
171+
return Objects.equals(labels, other.labels) && Objects.equals(dimensions, other.dimensions);
172+
}
143173
}
144174

145175
/**

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/plugins/partitions/support/Partition.java

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.LinkedHashMap;
44
import java.util.LinkedHashSet;
55
import java.util.Map;
6+
import java.util.Objects;
67
import java.util.Optional;
78
import java.util.Set;
89

@@ -176,44 +177,32 @@ private Data(Data data) {
176177
locked = data.locked;
177178
}
178179

180+
/**
181+
* Standard implementation consistent with the {@link #equals(Object)} method
182+
*/
179183
@Override
180184
public int hashCode() {
181-
final int prime = 31;
182-
int result = 1;
183-
result = prime * result + ((filter == null) ? 0 : filter.hashCode());
184-
result = prime * result + ((labelers == null) ? 0 : labelers.hashCode());
185-
result = prime * result + (retainPersonKeys ? 1231 : 1237);
186-
return result;
185+
return Objects.hash(retainPersonKeys, labelers, filter);
187186
}
188187

188+
/**
189+
* Two {@link Data} instances are equal if and only if
190+
* their inputs are equal.
191+
*/
189192
@Override
190193
public boolean equals(Object obj) {
191-
192194
if (this == obj) {
193195
return true;
194196
}
195-
if (!(obj instanceof Data)) {
197+
if (obj == null) {
196198
return false;
197199
}
198-
Data other = (Data) obj;
199-
if (filter == null) {
200-
if (other.filter != null) {
201-
return false;
202-
}
203-
} else if (!filter.equals(other.filter)) {
200+
if (getClass() != obj.getClass()) {
204201
return false;
205202
}
206-
if (labelers == null) {
207-
if (other.labelers != null) {
208-
return false;
209-
}
210-
} else if (!labelers.equals(other.labelers)) {
211-
return false;
212-
}
213-
if (retainPersonKeys != other.retainPersonKeys) {
214-
return false;
215-
}
216-
return true;
203+
Data other = (Data) obj;
204+
return retainPersonKeys == other.retainPersonKeys && Objects.equals(labelers, other.labelers)
205+
&& Objects.equals(filter, other.filter);
217206
}
218207

219208
@Override
@@ -246,31 +235,31 @@ public Set<Labeler> getLabelers() {
246235
return new LinkedHashSet<>(data.labelers.values());
247236
}
248237

238+
/**
239+
* Standard implementation consistent with the {@link #equals(Object)} method
240+
*/
249241
@Override
250242
public int hashCode() {
251-
final int prime = 31;
252-
int result = 1;
253-
result = prime * result + ((data == null) ? 0 : data.hashCode());
254-
return result;
243+
return Objects.hash(data);
255244
}
256245

246+
/**
247+
* Two {@link Partition} instances are equal if and only if
248+
* their inputs are equal.
249+
*/
257250
@Override
258251
public boolean equals(Object obj) {
259252
if (this == obj) {
260253
return true;
261254
}
262-
if (!(obj instanceof Partition)) {
255+
if (obj == null) {
263256
return false;
264257
}
265-
Partition other = (Partition) obj;
266-
if (data == null) {
267-
if (other.data != null) {
268-
return false;
269-
}
270-
} else if (!data.equals(other.data)) {
258+
if (getClass() != obj.getClass()) {
271259
return false;
272260
}
273-
return true;
261+
Partition other = (Partition) obj;
262+
return Objects.equals(data, other.data);
274263
}
275264

276265
@Override

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/plugins/partitions/support/filters/AndFilter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,19 @@ public void validate(PartitionsContext partitionsContext) {
6969
b.validate(partitionsContext);
7070
}
7171

72+
/**
73+
* Standard implementation consistent with the {@link #equals(Object)} method
74+
*/
7275
@Override
7376
public int hashCode() {
7477
return a.hashCode() + b.hashCode();
7578
}
7679

80+
/**
81+
* Two {@link AndFilter} instances are equal if and only if
82+
* their inputs are equal. The order in which inputs are added
83+
* does not matter.
84+
*/
7785
@Override
7886
public boolean equals(Object obj) {
7987
if (this == obj) {

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/plugins/partitions/support/filters/FalseFilter.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,26 @@ public void validate(PartitionsContext partitionsContext) {
3838

3939
}
4040

41+
/**
42+
* Standard implementation consistent with the {@link #equals(Object)} method
43+
*/
4144
@Override
4245
public int hashCode() {
4346
return 0;
4447
}
4548

49+
/**
50+
* All {@link FalseFilter} instances are considered equal
51+
*/
4652
@Override
4753
public boolean equals(Object obj) {
4854
if (this == obj) {
4955
return true;
5056
}
51-
if (!(obj instanceof FalseFilter)) {
57+
if (obj == null) {
58+
return false;
59+
}
60+
if (getClass() != obj.getClass()) {
5261
return false;
5362
}
5463
return true;

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/plugins/partitions/support/filters/NotFilter.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package gov.hhs.aspr.ms.gcm.simulation.plugins.partitions.support.filters;
22

3+
import java.util.Objects;
34
import java.util.Set;
45

56
import gov.hhs.aspr.ms.gcm.simulation.plugins.partitions.support.FilterSensitivity;
@@ -54,27 +55,31 @@ public Set<FilterSensitivity<?>> getFilterSensitivities() {
5455
return a.getFilterSensitivities();
5556
}
5657

58+
/**
59+
* Standard implementation consistent with the {@link #equals(Object)} method
60+
*/
5761
@Override
5862
public int hashCode() {
59-
final int prime = 31;
60-
int result = 1;
61-
result = prime * result + a.hashCode();
62-
return result;
63+
return Objects.hash(a);
6364
}
6465

66+
/**
67+
* Two {@link NotFilter} instances are equal if and only if
68+
* their inputs are equal.
69+
*/
6570
@Override
6671
public boolean equals(Object obj) {
6772
if (this == obj) {
6873
return true;
6974
}
70-
if (!(obj instanceof NotFilter)) {
75+
if (obj == null) {
7176
return false;
7277
}
73-
NotFilter other = (NotFilter) obj;
74-
if (!a.equals(other.a)) {
78+
if (getClass() != obj.getClass()) {
7579
return false;
7680
}
77-
return true;
81+
NotFilter other = (NotFilter) obj;
82+
return Objects.equals(a, other.a);
7883
}
7984

8085
}

0 commit comments

Comments
 (0)