Consistency in equals and hashcode implementations
Overriding the equals/hashcode methods defined on the Object class is very difficult to implement correctly (see Effective Java by Josh Block for a description of the minefield buried here). We should instead try to delegate the responsibility of overriding these methods to a framework to insure correct and consistent implementations. Possibilities her are:
Solution |
Pros |
Cons |
---|---|---|
Use the IDE's built-in code generators to generate equals and hashcode methods |
|
|
Use a framework with hashcode and equals generators, like the EqualsBuilder and HashCodeBuilder from the Apache Commons Lang library. |
|
|
Use a data object generation framework, which would take care of many of the common data object challenges. An example of such a framework can be found in the JEntity project, which is maintained by our own M. |
|
|
Continue as usual
Use IDE geenration
Use Apache framework
Use Jentity