Skip to content

Commit e92b3f6

Browse files
Improve equals contract in Nucleus (#316)
* Update equals and hashCode in ActorId and corresponding tests * Update equals and hashCode in DataManagerId and corresponding tests * Update equals and hashCode in ReportId and corresponding tests * Update equals and hashCode in SimplePluginId and corresponding tests * Update equals and hashCode in SimulationState and corresponding tests * Update equals and hashCode in AT_ActorContext * Update equals and hashCode in TestScenarioReport and corresponding tests * Update equals and hashCode in RunContinuityPluginData and corresponding tests * Update equals and hashCode in ExperimentParameterData and corresponding tests * Update equals and hashCode in FunctionalDimensionData and corresponding tests * Update equals and hashCode in IdentifiableFunction and corresponding tests * Update equals and hashCode in TestReportPlan and corresponding tests * Update equals and hashCode in TestDataManagerPlan and corresponding tests * Update equals and hashCode in TestActorPlan and corresponding tests * Update equals and hashCode for TestDimensionData in AT_DimensionData * Update equals and hashCode in Plugin and corresponding tests * Update equals and hashCode in TestPluginData and corresponding tests * Minor updates and typo corrections --------- Co-authored-by: shawnhatch <hatchs@leidos.com>
1 parent 97a1e29 commit e92b3f6

32 files changed

Lines changed: 1242 additions & 984 deletions

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/nucleus/ActorId.java

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

3+
import java.util.Objects;
4+
35
/**
46
* The unique identifier for actors. Actors are constructed dynamically and ids
57
* are distributed contiguously from 0.
@@ -35,31 +37,29 @@ public String toString() {
3537
}
3638

3739
/**
38-
* Standard hash code implementation
39-
*/
40+
* Standard implementation consistent with the {@link #equals(Object)} method
41+
*/
4042
@Override
4143
public int hashCode() {
42-
final int prime = 31;
43-
int result = 1;
44-
result = prime * result + id;
45-
return result;
44+
return Objects.hash(id);
4645
}
4746

4847
/**
49-
* Actor Id instances are equal if and only if their values are equal
50-
*/
48+
* Two {@link ActorId} instances are equal if and only if
49+
* their inputs are equal.
50+
*/
5151
@Override
5252
public boolean equals(Object obj) {
53-
if (this == obj)
53+
if (this == obj) {
5454
return true;
55-
if (obj == null)
55+
}
56+
if (obj == null) {
5657
return false;
57-
if (getClass() != obj.getClass())
58+
}
59+
if (getClass() != obj.getClass()) {
5860
return false;
61+
}
5962
ActorId other = (ActorId) obj;
60-
if (id != other.id)
61-
return false;
62-
return true;
63+
return id == other.id;
6364
}
64-
6565
}

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/nucleus/DataManagerId.java

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

3+
import java.util.Objects;
4+
35
/**
46
* The unique identifier for data managers. DataManagerId values are constructed
57
* dynamically and are distributed contiguously from 0.
@@ -22,32 +24,30 @@ public final int getValue() {
2224
}
2325

2426
/**
25-
* Standard hash code implementation
26-
*/
27+
* Standard implementation consistent with the {@link #equals(Object)} method
28+
*/
2729
@Override
2830
public int hashCode() {
29-
final int prime = 31;
30-
int result = 1;
31-
result = prime * result + id;
32-
return result;
31+
return Objects.hash(id);
3332
}
3433

3534
/**
36-
* DataManagerId instances are equal if and only if their values are equal
37-
*/
35+
* Two {@link DataManagerId} instances are equal if and only if
36+
* their inputs are equal.
37+
*/
3838
@Override
3939
public boolean equals(Object obj) {
4040
if (this == obj) {
4141
return true;
4242
}
43-
if (!(obj instanceof DataManagerId)) {
43+
if (obj == null) {
4444
return false;
4545
}
46-
DataManagerId other = (DataManagerId) obj;
47-
if (id != other.id) {
46+
if (getClass() != obj.getClass()) {
4847
return false;
4948
}
50-
return true;
49+
DataManagerId other = (DataManagerId) obj;
50+
return id == other.id;
5151
}
5252

5353
/**

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/nucleus/ExperimentParameterData.java

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -170,61 +170,38 @@ private Data(Data data) {
170170
locked = data.locked;
171171
}
172172

173+
/**
174+
* Standard implementation consistent with the {@link #equals(Object)} method
175+
*/
173176
@Override
174177
public int hashCode() {
175-
final int prime = 31;
176-
int result = 1;
177-
result = prime * result + (continueFromProgressLog ? 1231 : 1237);
178-
result = prime * result + ((experimentProgressLogPath == null) ? 0 : experimentProgressLogPath.hashCode());
179-
result = prime * result + ((explicitScenarioIds == null) ? 0 : explicitScenarioIds.hashCode());
180-
result = prime * result + (haltOnException ? 1231 : 1237);
181-
result = prime * result + ((simulationHaltTime == null) ? 0 : simulationHaltTime.hashCode());
182-
result = prime * result + (stateRecordingIsScheduled ? 1231 : 1237);
183-
result = prime * result + threadCount;
184-
return result;
178+
return Objects.hash(threadCount, stateRecordingIsScheduled, simulationHaltTime, haltOnException,
179+
experimentProgressLogPath, continueFromProgressLog, explicitScenarioIds);
185180
}
186181

182+
/**
183+
* Two {@link Data} instances are equal if and only if
184+
* their inputs are equal.
185+
*/
187186
@Override
188187
public boolean equals(Object obj) {
189188
if (this == obj) {
190189
return true;
191190
}
192-
if (!(obj instanceof Data)) {
193-
return false;
194-
}
195-
Data other = (Data) obj;
196-
if (continueFromProgressLog != other.continueFromProgressLog) {
197-
return false;
198-
}
199-
if (experimentProgressLogPath == null) {
200-
if (other.experimentProgressLogPath != null) {
201-
return false;
202-
}
203-
} else if (!experimentProgressLogPath.equals(other.experimentProgressLogPath)) {
204-
return false;
205-
}
206-
if (!explicitScenarioIds.equals(other.explicitScenarioIds)) {
207-
return false;
208-
}
209-
if (haltOnException != other.haltOnException) {
210-
return false;
211-
}
212-
if (simulationHaltTime == null) {
213-
if (other.simulationHaltTime != null) {
214-
return false;
215-
}
216-
} else if (!simulationHaltTime.equals(other.simulationHaltTime)) {
191+
if (obj == null) {
217192
return false;
218193
}
219-
if (stateRecordingIsScheduled != other.stateRecordingIsScheduled) {
194+
if (getClass() != obj.getClass()) {
220195
return false;
221196
}
222-
if (threadCount != other.threadCount) {
223-
return false;
224-
}
225-
return true;
197+
Data other = (Data) obj;
198+
return threadCount == other.threadCount && stateRecordingIsScheduled == other.stateRecordingIsScheduled
199+
&& Objects.equals(simulationHaltTime, other.simulationHaltTime)
200+
&& haltOnException == other.haltOnException
201+
&& Objects.equals(experimentProgressLogPath, other.experimentProgressLogPath)
202+
&& continueFromProgressLog == other.continueFromProgressLog
203+
&& Objects.equals(explicitScenarioIds, other.explicitScenarioIds);
226204
}
227-
228205
}
229206

230207
/**
@@ -316,11 +293,18 @@ public static boolean checkVersionSupported(String version) {
316293
return StandardVersioning.checkVersionSupported(version);
317294
}
318295

296+
/**
297+
* Standard implementation consistent with the {@link #equals(Object)} method
298+
*/
319299
@Override
320300
public int hashCode() {
321301
return Objects.hash(data);
322302
}
323303

304+
/**
305+
* Two {@link ExperimentParameterData} instances are equal if and only if
306+
* their inputs are equal.
307+
*/
324308
@Override
325309
public boolean equals(Object obj) {
326310
if (this == obj) {

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/nucleus/FunctionalDimensionData.java

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.LinkedHashMap;
66
import java.util.List;
77
import java.util.Map;
8+
import java.util.Objects;
89
import java.util.function.Function;
910

1011
import gov.hhs.aspr.ms.util.errors.ContractException;
@@ -166,16 +167,18 @@ private Data(Data data) {
166167
locked = data.locked;
167168
}
168169

170+
/**
171+
* Standard implementation consistent with the {@link #equals(Object)} method
172+
*/
169173
@Override
170174
public int hashCode() {
171-
final int prime = 31;
172-
int result = 1;
173-
result = prime * result + ((levelNames == null) ? 0 : levelNames.hashCode());
174-
result = prime * result + ((values == null) ? 0 : values.hashCode());
175-
result = prime * result + ((metaData == null) ? 0 : metaData.hashCode());
176-
return result;
175+
return Objects.hash(levelNames, values, metaData);
177176
}
178177

178+
/**
179+
* Two {@link Data} instances are equal if and only if
180+
* their inputs are equal.
181+
*/
179182
@Override
180183
public boolean equals(Object obj) {
181184
if (this == obj) {
@@ -188,22 +191,8 @@ public boolean equals(Object obj) {
188191
return false;
189192
}
190193
Data other = (Data) obj;
191-
if (levelNames == null) {
192-
if (other.levelNames != null)
193-
return false;
194-
} else if (!levelNames.equals(other.levelNames))
195-
return false;
196-
if (values == null) {
197-
if (other.values != null)
198-
return false;
199-
} else if (!values.equals(other.values))
200-
return false;
201-
if (metaData == null) {
202-
if (other.metaData != null)
203-
return false;
204-
} else if (!metaData.equals(other.metaData))
205-
return false;
206-
return true;
194+
return Objects.equals(levelNames, other.levelNames) && Objects.equals(values, other.values)
195+
&& Objects.equals(metaData, other.metaData);
207196
}
208197

209198
@Override
@@ -271,29 +260,31 @@ public static boolean checkVersionSupported(String version) {
271260
return StandardVersioning.checkVersionSupported(version);
272261
}
273262

263+
/**
264+
* Standard implementation consistent with the {@link #equals(Object)} method
265+
*/
274266
@Override
275267
public int hashCode() {
276-
final int prime = 31;
277-
int result = 1;
278-
result = prime * result + ((data == null) ? 0 : data.hashCode());
279-
return result;
268+
return Objects.hash(data);
280269
}
281270

271+
/**
272+
* Two {@link FunctionalDimensionData} instances are equal if and only if
273+
* their inputs are equal.
274+
*/
282275
@Override
283276
public boolean equals(Object obj) {
284-
if (this == obj)
277+
if (this == obj) {
285278
return true;
286-
if (obj == null)
279+
}
280+
if (obj == null) {
287281
return false;
288-
if (getClass() != obj.getClass())
282+
}
283+
if (getClass() != obj.getClass()) {
289284
return false;
285+
}
290286
FunctionalDimensionData other = (FunctionalDimensionData) obj;
291-
if (data == null) {
292-
if (other.data != null)
293-
return false;
294-
} else if (!data.equals(other.data))
295-
return false;
296-
return true;
287+
return Objects.equals(data, other.data);
297288
}
298289

299290
@Override

simulation/src/main/java/gov/hhs/aspr/ms/gcm/simulation/nucleus/IdentifiableFunction.java

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

3+
import java.util.Objects;
34
import java.util.function.Function;
45

56
import gov.hhs.aspr.ms.util.errors.ContractException;
@@ -26,31 +27,30 @@ public Function<N, Object> getFunction() {
2627
return function;
2728
}
2829

30+
/**
31+
* Standard implementation consistent with the {@link #equals(Object)} method
32+
*/
2933
@Override
3034
public int hashCode() {
31-
final int prime = 31;
32-
int result = 1;
33-
result = prime * result + ((functionId == null) ? 0 : functionId.hashCode());
34-
return result;
35+
return Objects.hash(functionId);
3536
}
3637

38+
/**
39+
* Two {@link IdentifiableFunction} instances are equal if and only if
40+
* their functionIds are equal.
41+
*/
3742
@Override
3843
public boolean equals(Object obj) {
3944
if (this == obj) {
4045
return true;
4146
}
42-
if (!(obj instanceof IdentifiableFunction)) {
47+
if (obj == null) {
4348
return false;
4449
}
45-
IdentifiableFunction<?> other = (IdentifiableFunction<?>) obj;
46-
if (functionId == null) {
47-
if (other.functionId != null) {
48-
return false;
49-
}
50-
} else if (!functionId.equals(other.functionId)) {
50+
if (getClass() != obj.getClass()) {
5151
return false;
5252
}
53-
return true;
53+
IdentifiableFunction<?> other = (IdentifiableFunction<?>) obj;
54+
return Objects.equals(functionId, other.functionId);
5455
}
55-
5656
}

0 commit comments

Comments
 (0)