-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add hash code override methods where equals method is overwritten #100
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see https://www.baeldung.com/java-equals-hashcode-contracts
use equalsverifier in tests
@@ -175,4 +177,9 @@ public boolean equals(final Object object) { | |||
return this.originTable.equals(cast.originTable) && this.ctx == cast.ctx && // equal only if same instance of DSLContext |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
table condition missing from equals
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added missing field
@@ -165,4 +166,9 @@ public boolean equals(final Object object) { | |||
return this.ctx == cast.ctx && this.value.equals(cast.value) && this.table.equals(cast.table) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
recordsInMetadata missing from equals
@@ -136,4 +137,9 @@ public boolean equals(final Object object) { | |||
final IndexStatementCondition cast = (IndexStatementCondition) object; | |||
return this.value.equals(cast.value) && this.config.equals(cast.config); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing fields in equals
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added missing field
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong method used Objects.hashCode(value) while should use Objects.hash(value)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to Objects.hash(value)
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong method used Objects.hashCode(value) while should use Objects.hash(value)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to Objects.hash(value)
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(element); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong method used Objects.hashCode(element) while should use Objects.hash(element)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to Objects.hash(value)
@@ -100,4 +102,9 @@ public boolean equals(final Object object) { | |||
boolean equalValue = this.element.getAttribute("value").equals(cast.element.getAttribute("value")); | |||
return equalName && equalOperation && equalValue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
equal() and hashCode calculation methods differ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed methods to use only element field
added equalsverifier libary tests |
DSLContext ctx = DSL.using(new MockConnection(c -> new MockResult[0])); | ||
|
||
@Test | ||
void testEquality() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public keyword missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made test classes and methods public
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still missing from ConditionConfigTest
?
@@ -86,4 +87,18 @@ void testNotEquals() { | |||
Assertions.assertNotEquals(value2, value1); | |||
Assertions.assertNotEquals(value1, null); | |||
} | |||
|
|||
@Test | |||
void testHashCode() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public keyword missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made test classes and methods public
Use Objects.hash(values) to generate hash code for objects where equals() method is overwritten.
Objects.hash uses formula
result = 31 * result + elementHashCode
for good hash distribution.31 * result
which is same as(x << 5) - result)
(bitwise shift) is optimized for JVM on modern hardware.null safe method, null values return hash value of 0.
note: fixed missing object parameter value in ConditionConfig equals method and added test for class