Submitted On 12-FEB-2007
alexsmail
Why not simply to change equals method as following?
[code]
public final boolean equals(Object obj) {
Enum other = (Enum)obj;
if(this.ordinal==other.ordinal){
return true;
}
return false;
}
[/code]
Submitted On 01-APR-2007
I would rather implement #equals that way:
public final boolean equals(Object obj) {
return getClass().equals(obj.getClass())
? compareTo(obj.getClass().cast(obj)) == 0
: false;
}
Simple, and should work correctly.
Submitted On 01-APR-2007
Errata:
was:
? compareTo(obj.getClass().cast(obj)) == 0
should be:
? compareTo(getClass().cast(obj)) == 0
Submitted On 01-APR-2007
P.S.
my workaround for this to always use #compareTo instead of #equals.
Moreover, #compareTo could be considered to be always better than #equals because it forces us to use correct Enum type. With #equals one can use incorrect Enum, it will never throw any exception or warning, the result would be always false instead.
Submitted On 01-APR-2007
I was thinking about not working #equals method, and I am scared that the Collections API might not work as expected with Enums.
That could cause major headache for many developers working with enums in distributed applications :(
And the solution seems so simple, just replace bad #equals metod as described above or maybe implement #readObject so it will always produce singletons...
Submitted On 23-MAY-2007
Why not implement the readResolve method in the Enum class and just return the correct instance there?
Submitted On 19-JUN-2007
I think it is not enough to change the #equals method, because the comparison operator == must also work correctly
Submitted On 06-AUG-2007
a little W/A could be (And I tested it) overriding the equals method with the type of the Enum, example:
enum MyEnum {
A, B, C, D;
public boolean equals(MyEnum obj) {
return obj.ordinal() == this.ordinal();
}
}
this works, but does not solve the == problem.
Submitted On 06-AUG-2007
btw... anybody is working on this? because when you use EJB3 and the entities has enum types it's a MUST to correct this, there's no workaround about it...
how much time will it takes? because it was reported in 2005... :s
Submitted On 27-JUN-2008
For the first several responses, how is it possible to override equals which is declared final in the Enum class?
PLEASE NOTE: JDK6 is formerly known as Project Mustang
|