Skip to content

Commit d122680

Browse files
Improve equals contract in PersonProperties plugin (#314)
* Update equals and hashCode in PersonPropertyFilter and corresponding tests * Update equals and hashCode in PersonPropertyDimensionData and corresponding tests * Update equals and hashCode in PersonPropertyReportPluginData and corresponding tests * Update equals and hashCode in PersonPropertyInteractionReportPluginData and corresponding tests * Update equals and hashCode in PersonPropertiesPluginData and corresponding tests * Update equals and hashCode in PersonPropertyValueInitialization and corresponding tests * Update equals and hashCode for LocalPersonPropertyId in AT_PersonPropertyDataManager * Increase randomness for testEquals and testHashCode in AT_PersonPropertyFilter * Update getRandomPersonPropertyValueInitialization by removing booleans * Fix typos in comments for AT_PersonPropertyFilter and AT_PersonPropertyInitialization --------- Co-authored-by: shawnhatch <hatchs@leidos.com>
1 parent 1904ba2 commit d122680

13 files changed

Lines changed: 416 additions & 646 deletions

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/plugins/personproperties/datamanagers/PersonPropertiesPluginData.java

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.LinkedHashSet;
77
import java.util.List;
88
import java.util.Map;
9+
import java.util.Objects;
910
import java.util.Set;
1011

1112
import gov.hhs.aspr.ms.gcm.simulation.nucleus.PluginData;
@@ -69,49 +70,36 @@ private Data(Data data) {
6970
locked = data.locked;
7071
}
7172

73+
/**
74+
* Standard implementation consistent with the {@link #equals(Object)} method
75+
*/
7276
@Override
7377
public int hashCode() {
74-
final int prime = 31;
75-
int result = 1;
76-
result = prime * result + propertyDefinitions.hashCode();
77-
result = prime * result + propertyDefinitionTimes.hashCode();
78-
result = prime * result + propertyTrackingPolicies.hashCode();
79-
result = prime * result + propertyValues.hashCode();
80-
result = prime * result + propertyTimes.hashCode();
81-
return result;
78+
return Objects.hash(propertyDefinitions, propertyDefinitionTimes, propertyTrackingPolicies, propertyValues,
79+
propertyTimes);
8280
}
8381

82+
/**
83+
* Two {@link Data} instances are equal if and only if
84+
* their inputs are equal.
85+
*/
8486
@Override
8587
public boolean equals(Object obj) {
8688
if (this == obj) {
8789
return true;
8890
}
89-
if (!(obj instanceof Data)) {
90-
return false;
91-
}
92-
Data other = (Data) obj;
93-
94-
if (!propertyDefinitions.equals(other.propertyDefinitions)) {
95-
return false;
96-
}
97-
98-
if (!propertyDefinitionTimes.equals(other.propertyDefinitionTimes)) {
91+
if (obj == null) {
9992
return false;
10093
}
101-
102-
if (!propertyTrackingPolicies.equals(other.propertyTrackingPolicies)) {
94+
if (getClass() != obj.getClass()) {
10395
return false;
10496
}
105-
106-
if (!propertyValues.equals(other.propertyValues)) {
107-
return false;
108-
}
109-
110-
if (!propertyTimes.equals(other.propertyTimes)) {
111-
return false;
112-
}
113-
114-
return true;
97+
Data other = (Data) obj;
98+
return Objects.equals(propertyDefinitions, other.propertyDefinitions)
99+
&& Objects.equals(propertyDefinitionTimes, other.propertyDefinitionTimes)
100+
&& Objects.equals(propertyTrackingPolicies, other.propertyTrackingPolicies)
101+
&& Objects.equals(propertyValues, other.propertyValues)
102+
&& Objects.equals(propertyTimes, other.propertyTimes);
115103
}
116104

117105
@Override
@@ -538,30 +526,31 @@ public static boolean checkVersionSupported(String version) {
538526
return StandardVersioning.checkVersionSupported(version);
539527
}
540528

529+
/**
530+
* Standard implementation consistent with the {@link #equals(Object)} method
531+
*/
541532
@Override
542533
public int hashCode() {
543-
final int prime = 31;
544-
int result = 1;
545-
result = prime * result + data.hashCode();
546-
return result;
534+
return Objects.hash(data);
547535
}
548536

537+
/**
538+
* Two {@link PersonPropertiesPluginData} instances are equal if and only if
539+
* their inputs are equal.
540+
*/
549541
@Override
550542
public boolean equals(Object obj) {
551543
if (this == obj) {
552544
return true;
553545
}
554-
555-
if (!(obj instanceof PersonPropertiesPluginData)) {
546+
if (obj == null) {
556547
return false;
557548
}
558-
559-
PersonPropertiesPluginData other = (PersonPropertiesPluginData) obj;
560-
561-
if (!data.equals(other.data)) {
549+
if (getClass() != obj.getClass()) {
562550
return false;
563551
}
564-
return true;
552+
PersonPropertiesPluginData other = (PersonPropertiesPluginData) obj;
553+
return Objects.equals(data, other.data);
565554
}
566555

567556
@Override

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/plugins/personproperties/reports/PersonPropertyInteractionReportPluginData.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gov.hhs.aspr.ms.gcm.simulation.plugins.personproperties.reports;
22

33
import java.util.LinkedHashSet;
4+
import java.util.Objects;
45
import java.util.Set;
56

67
import gov.hhs.aspr.ms.gcm.simulation.nucleus.StandardVersioning;
@@ -36,31 +37,34 @@ private Data(Data data) {
3637
locked = data.locked;
3738
}
3839

40+
/**
41+
* Standard implementation consistent with the {@link #equals(Object)} method
42+
*/
3943
@Override
4044
public int hashCode() {
4145
final int prime = 31;
4246
int result = super.hashCode();
43-
result = prime * result + ((personPropertyIds == null) ? 0 : personPropertyIds.hashCode());
47+
result = prime * result + Objects.hash(personPropertyIds);
4448
return result;
4549
}
4650

51+
/**
52+
* Two {@link Data} instances are equal if and only if
53+
* their inputs are equal.
54+
*/
4755
@Override
4856
public boolean equals(Object obj) {
4957
if (this == obj) {
5058
return true;
5159
}
52-
if (!(obj instanceof Data)) {
60+
if (!super.equals(obj)) {
5361
return false;
5462
}
55-
Data other = (Data) obj;
56-
if (personPropertyIds == null) {
57-
if (other.personPropertyIds != null) {
58-
return false;
59-
}
60-
} else if (!personPropertyIds.equals(other.personPropertyIds)) {
63+
if (getClass() != obj.getClass()) {
6164
return false;
6265
}
63-
return super.equals(other);
66+
Data other = (Data) obj;
67+
return Objects.equals(personPropertyIds, other.personPropertyIds);
6468
}
6569

6670
@Override
@@ -169,31 +173,34 @@ public Set<PersonPropertyId> getPersonPropertyIds() {
169173
return new LinkedHashSet<>(data.personPropertyIds);
170174
}
171175

176+
/**
177+
* Standard implementation consistent with the {@link #equals(Object)} method
178+
*/
172179
@Override
173180
public int hashCode() {
174181
final int prime = 31;
175-
int result = 1;
176-
result = prime * result + ((data == null) ? 0 : data.hashCode());
182+
int result = super.hashCode();
183+
result = prime * result + Objects.hash(data);
177184
return result;
178185
}
179186

187+
/**
188+
* Two {@link PersonPropertyInteractionReportPluginData} instances are equal if and only if
189+
* their inputs are equal.
190+
*/
180191
@Override
181192
public boolean equals(Object obj) {
182193
if (this == obj) {
183194
return true;
184195
}
185-
if (!(obj instanceof PersonPropertyInteractionReportPluginData)) {
196+
if (!super.equals(obj)) {
186197
return false;
187198
}
188-
PersonPropertyInteractionReportPluginData other = (PersonPropertyInteractionReportPluginData) obj;
189-
if (data == null) {
190-
if (other.data != null) {
191-
return false;
192-
}
193-
} else if (!data.equals(other.data)) {
199+
if (getClass() != obj.getClass()) {
194200
return false;
195201
}
196-
return true;
202+
PersonPropertyInteractionReportPluginData other = (PersonPropertyInteractionReportPluginData) obj;
203+
return Objects.equals(data, other.data);
197204
}
198205

199206
@Override

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/plugins/personproperties/reports/PersonPropertyReportPluginData.java

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gov.hhs.aspr.ms.gcm.simulation.plugins.personproperties.reports;
22

33
import java.util.LinkedHashSet;
4+
import java.util.Objects;
45
import java.util.Set;
56

67
import gov.hhs.aspr.ms.gcm.simulation.nucleus.StandardVersioning;
@@ -47,43 +48,36 @@ private Data(Data data) {
4748
locked = data.locked;
4849
}
4950

51+
/**
52+
* Standard implementation consistent with the {@link #equals(Object)} method
53+
*/
5054
@Override
5155
public int hashCode() {
5256
final int prime = 31;
5357
int result = super.hashCode();
54-
result = prime * result + (defaultInclusionPolicy ? 1231 : 1237);
55-
result = prime * result + ((excludedProperties == null) ? 0 : excludedProperties.hashCode());
56-
result = prime * result + ((includedProperties == null) ? 0 : includedProperties.hashCode());
58+
result = prime * result + Objects.hash(includedProperties, excludedProperties, defaultInclusionPolicy);
5759
return result;
5860
}
5961

62+
/**
63+
* Two {@link Data} instances are equal if and only if
64+
* their inputs are equal.
65+
*/
6066
@Override
6167
public boolean equals(Object obj) {
6268
if (this == obj) {
6369
return true;
6470
}
65-
if (!(obj instanceof Data)) {
66-
return false;
67-
}
68-
Data other = (Data) obj;
69-
if (defaultInclusionPolicy != other.defaultInclusionPolicy) {
70-
return false;
71-
}
72-
if (excludedProperties == null) {
73-
if (other.excludedProperties != null) {
74-
return false;
75-
}
76-
} else if (!excludedProperties.equals(other.excludedProperties)) {
71+
if (!super.equals(obj)) {
7772
return false;
7873
}
79-
if (includedProperties == null) {
80-
if (other.includedProperties != null) {
81-
return false;
82-
}
83-
} else if (!includedProperties.equals(other.includedProperties)) {
74+
if (getClass() != obj.getClass()) {
8475
return false;
8576
}
86-
return super.equals(other);
77+
Data other = (Data) obj;
78+
return Objects.equals(includedProperties, other.includedProperties)
79+
&& Objects.equals(excludedProperties, other.excludedProperties)
80+
&& defaultInclusionPolicy == other.defaultInclusionPolicy;
8781
}
8882

8983
@Override
@@ -261,31 +255,34 @@ public boolean getDefaultInclusionPolicy() {
261255
return data.defaultInclusionPolicy;
262256
}
263257

258+
/**
259+
* Standard implementation consistent with the {@link #equals(Object)} method
260+
*/
264261
@Override
265262
public int hashCode() {
266263
final int prime = 31;
267-
int result = 1;
268-
result = prime * result + ((data == null) ? 0 : data.hashCode());
264+
int result = super.hashCode();
265+
result = prime * result + Objects.hash(data);
269266
return result;
270267
}
271268

269+
/**
270+
* Two {@link PersonPropertyReportPluginData} instances are equal if and only if
271+
* their inputs are equal.
272+
*/
272273
@Override
273274
public boolean equals(Object obj) {
274275
if (this == obj) {
275276
return true;
276277
}
277-
if (!(obj instanceof PersonPropertyReportPluginData)) {
278+
if (!super.equals(obj)) {
278279
return false;
279280
}
280-
PersonPropertyReportPluginData other = (PersonPropertyReportPluginData) obj;
281-
if (data == null) {
282-
if (other.data != null) {
283-
return false;
284-
}
285-
} else if (!data.equals(other.data)) {
281+
if (getClass() != obj.getClass()) {
286282
return false;
287283
}
288-
return true;
284+
PersonPropertyReportPluginData other = (PersonPropertyReportPluginData) obj;
285+
return Objects.equals(data, other.data);
289286
}
290287

291288
@Override

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/plugins/personproperties/support/PersonPropertyDimensionData.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,18 @@ private Data(Data data) {
177177
locked = data.locked;
178178
}
179179

180+
/**
181+
* Standard implementation consistent with the {@link #equals(Object)} method
182+
*/
180183
@Override
181184
public int hashCode() {
182185
return Objects.hash(levelNames, values, personPropertyId, trackTimes);
183186
}
184187

188+
/**
189+
* Two {@link Data} instances are equal if and only if
190+
* their inputs are equal.
191+
*/
185192
@Override
186193
public boolean equals(Object obj) {
187194
if (this == obj) {
@@ -194,10 +201,8 @@ public boolean equals(Object obj) {
194201
return false;
195202
}
196203
Data other = (Data) obj;
197-
return Objects.equals(levelNames, other.levelNames)
198-
&& Objects.equals(values, other.values)
199-
&& Objects.equals(personPropertyId, other.personPropertyId)
200-
&& trackTimes == other.trackTimes;
204+
return Objects.equals(levelNames, other.levelNames) && Objects.equals(values, other.values)
205+
&& Objects.equals(personPropertyId, other.personPropertyId) && trackTimes == other.trackTimes;
201206
}
202207

203208
@Override
@@ -277,11 +282,18 @@ public static boolean checkVersionSupported(String version) {
277282
return StandardVersioning.checkVersionSupported(version);
278283
}
279284

285+
/**
286+
* Standard implementation consistent with the {@link #equals(Object)} method
287+
*/
280288
@Override
281289
public int hashCode() {
282290
return Objects.hash(data);
283291
}
284292

293+
/**
294+
* Two {@link PersonPropertyDimensionData} instances are equal if and only if
295+
* their inputs are equal.
296+
*/
285297
@Override
286298
public boolean equals(Object obj) {
287299
if (this == obj) {

0 commit comments

Comments
 (0)