You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"hashcode" does not need to use all fields of "equals".
Of course, It is not allowed to use fields, that are not part of "equals".
But it should be perfectly valid, if "hashcode" only uses a subset of the fields in "equals".
It should also be perfectly valid, to return a constant value like 42. It may lead to performance issues within large HashMaps, but it fulfills the contract.
The following test fails with version 3.1.9, but it shouldn't (IMO): @test @DisplayName("equals and hashcode fulfill the contract")
void testEqualsAndHashcodeFulfillTheContract() {
EqualsVerifier.forClass(ValueObject.class).usingGetClass().verify();
}
static class ValueObject {
private final String id;
private final String info;
public ValueObject(String id, String info) {
this.id = id.trim();
this.info = info.trim();
}
public String getId() {
return id;
}
public String getInfo() {
return info;
}
@Override
public boolean equals(final Object other) {
if ((other == null) || (!getClass().equals(other.getClass()))) {
return false;
}
ValueObject castOther = (ValueObject) other;
return Objects.equals(id, castOther.id) && Objects.equals(info, castOther.info);
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
}
The text was updated successfully, but these errors were encountered:
"hashcode" does not need to use all fields of "equals".
Of course, It is not allowed to use fields, that are not part of "equals".
But it should be perfectly valid, if "hashcode" only uses a subset of the fields in "equals".
It should also be perfectly valid, to return a constant value like 42. It may lead to performance issues within large HashMaps, but it fulfills the contract.
The following test fails with version 3.1.9, but it shouldn't (IMO):
@test
@DisplayName("equals and hashcode fulfill the contract")
void testEqualsAndHashcodeFulfillTheContract() {
EqualsVerifier.forClass(ValueObject.class).usingGetClass().verify();
}
static class ValueObject {
private final String id;
private final String info;
The text was updated successfully, but these errors were encountered: