-
Notifications
You must be signed in to change notification settings - Fork 71
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
Overriding hashCode
method when overriding equals
method
#46
Comments
I made an attempt to generate a I did notice the current |
@Neutius it doesn't compare by the P.S. There is a useful class we can probably use to simplify the code to write |
@pomadchin I didn't notice the name was not used intententionally, thanks for pointing that out. Using that class directly would add a dependency to this project, which I would like to avoid. I usually use IntelliJ to generate I'm pretty sure we could find a way to make readable and maintainable code without adding a dependency. |
@Neutius ah yeah, I didn’t mean to add an extra dependency, but to use this class as an example of how we could make a more consistent hash code generation. |
I fiddled around a bit, still not happy with it, but at least it's something: https://github.com/Neutius/proj4j/commit/c3f48a0312180fa82c9d8a372d20b5c2c448d176 |
Is it a fair statement to say that the state used to compute As a snarky aside, the fact that these classes are mutable makes me shudder to think that anyone would ever put them in a |
I fully agree with you on the first part. In my attempt, I included every field included in the I could generate and refactor Edit: my co-worker pointed out I should use the Objects.hash method, which results in something like this: https://github.com/Neutius/proj4j/commit/0679ed05be0385cc50d7f54245fa2357cfd32a0d I used all the field in the |
That would be my assumption, but I defer to the others here. I wonder if we should consider using this or something similar? |
@metasim dunno; I will look into it, thanks for the link. I think it is a critical issue now, I will have to look into it. |
As of pull request #45 the class org.locationtech.proj4j.proj.Projection and ten of its subclasses override the
equals
method that is inherited from the Object class.However, the
hashCode
method is not overridden, and I'm pretty sure that's considered bad practice. This is described in more detail by Joshua Bloch in Item 11 of his book Effective Java, but it boils down to this: if two objects are considered equal by theequals
method, they should also return the same value when their respectivehashCode
method is called.Overriding
equals
but nothashCode
violates the contract for thehashCode
method, and makes instances of this class unusable for collections such asHashMap
andHashSet
.The text was updated successfully, but these errors were encountered: