Why don't you let your IDE (Eclipse/NetBeans/IntelliJ) generate the hashCode() and equals() methods for you. They are doing quite a good job at it.
Why don't you let your IDE (Eclipse/NetBeans/IntelliJ) generate the hashCode() and equals() methods for you. They are doing quite a good job at it. AOP will work, of course, but it's quite a complication.
And this will mean you won't be able to use these objects with almost no collection or utility. The other logical solution is to just remove the implementations of those methods where they do not work, thsus effectively leaving only the implementations in Object.
Bozho: +1, that's what Hemal Panda said he was doing in his comment and I didn't think about it (even tough we're using custom templates). I was too focused on having something like "@NotNull" :) – SyntaxT3rr0r Feb 5 '10 at 19:53.
I agree with your assessment of this being a problem with hashCode and equals being defined in Object in the first place. I've long held the view that equality should be handled in the same way as ordering - with an interface saying "I can be compared with an instance of X" and another saying "I can compare two instances of X". On the other hand, has this actually caused any bugs for you?
Have people been trying to use equals and hashCode where they shouldn't? Because even if you can make every single class in your codebase throw an exception when those methods are called inappropriately, that won't be true of other classes you're using, whether from the JDK or third party libraries. I'm sure you could do this with AOP of some form or other, whether that's normal annotation processing or something else - but do you have evidence that the reward would be worth the effort?
Another way of looking at it: this is only in the case where you're extending another class which already overrides hashCode and equals, right? Otherwise you can use the "equality = identity" nature of Object's hashCode/equals methods, which can still be useful. Do you have very many classes which fall into this category?
Could you not just write a unit test to find all such types via reflection, and check that those types throw an exception when you call hashCode/equals? (This could either be automated if they have a parameterless constructor, or have a manual list of types which have been checked - the unit test could fail if there's a new type which isn't on the "known good" list. ).
Agree with the AOP approach. – Thorbjørn Ravn Andersen Feb 5 '10 at 7:40 2 +1 for wanting a separate interface for equals/hashCode. That would prevent people from using objects as hash keys that just don't work there (an Immutable interface would also be nice in that regard).
– Thilo Feb 5 '10 at 8:56 regarding the reward/effort this is of course a concern: but the same probably could have been said if, when annotation came out, I suggested an @NotNull annotation. All it would take would be someone to write this once, then we could all reuse it. Interestingly I'm using custom templates and didn't think about simply making, by default, hashCode() and equals() throw the UnsupportedOperationException and then simply change the implementation for classes where equals() and hashCode() makes sense.
– SyntaxT3rr0r Feb 5 '10 at 19:39 I think nullability tends to be a much bigger concern. I've seen lots of bugs due to nullity concerns, but I can't remember ever trying to hash something which shouldn't be hashed. – Jon Skeet Feb 5 '10 at 21:47.
The meaning of equality is defined by the class. Thus, using Object’s equal is perfectly valid. If you’re not overriding equals then you’re defining each instance as being unique.
There seems to be a misconception that equals is one of those methods that always needs overriding, and that it must check all of its fields. I would argue for the opposite – don’t override equals unless your definition of equality differs. I also disagree with the artima article, in particular “Pitfall #3: Defining equals in terms of mutable fields�.
It’s perfectly valid for a class to defined its equality based on mutable fields. It’s up the user to be aware of this when using it with collections. If a mutable object defines its equality on its mutable state, then don't expect two instances to be equals after one has changed.
I think that throwing UnsupportedOperation violates the sprint of equals. Object’s equals states: The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true). So, I should be able to call equals and get a true or false value depending on either Object’s equals definition or the overridden equals definition.
1 +1: even if nothing else is useful, you could still apply object identity as the equality measure by simply using the equals/hashCode implementation in Object. – Joachim Sauer Feb 5 '10 at 8:20 @Joachim: Yes, but if object equality is not meaningful for his class, and he does not need equals/hashCode at all, throwing UnsupportedOperation just makes more sense. At the very least this helps finding errors when some part of the code mistakenly does call these methods.
– Thilo Feb 5 '10 at 8:48 @Kuo: -1, I don't think it, I know it, and it has been proven :) You disagree with Martin Odersky (the Scala author) and Joshua Bloch. Fine. You're entitled to your opinions.It has been proven that you simply cannot guarantee the transitivity contract of equals(), for a start.
Even the almighty Jon Skeet from SO fame and c.l.j.p. Fame agrees with my statement that it's the very presence of equals() and hashCode() at the top of the object hierarchy that is a problem here.My concern is exactly what Thilo commented: where it makes no sense, it should throw an UnsupportedOperationException. – SyntaxT3rr0r Feb 5 '10 at 19:49.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.