Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

elliVM
Copy link
Contributor

@elliVM elliVM commented Oct 14, 2024

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

@elliVM elliVM self-assigned this Oct 14, 2024
Copy link
Member

@kortemik kortemik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -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
Copy link
Member

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

Copy link
Contributor Author

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)
Copy link
Member

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing fields in equals

Copy link
Contributor Author

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);
Copy link
Member

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)

Copy link
Contributor Author

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);
Copy link
Member

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)

Copy link
Contributor Author

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);
Copy link
Member

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)

Copy link
Contributor Author

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;
Copy link
Member

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

Copy link
Contributor Author

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

@elliVM elliVM marked this pull request as draft October 14, 2024 12:39
@elliVM elliVM removed the request for review from StrongestNumber9 October 14, 2024 12:40
@elliVM elliVM marked this pull request as ready for review October 16, 2024 08:13
@elliVM
Copy link
Contributor Author

elliVM commented Oct 16, 2024

added equalsverifier libary tests

DSLContext ctx = DSL.using(new MockConnection(c -> new MockResult[0]));

@Test
void testEquality() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public keyword missing?

Copy link
Contributor Author

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

Copy link

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() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public keyword missing?

Copy link
Contributor Author

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

@elliVM elliVM requested a review from 51-code October 18, 2024 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants