Is it ok for a class' equeals to simply use its own compareTo?

N

natG

In a class that implements Comparable, and has a custom compareTo, why bother
writing a custom equeals, if equeals can simply check if compareTo returns 0?

public boolean equeals(Object o){
return (compareTo(o)=0);
}

Does this make sense?

Thanks;
-nat
 
J

Joona I Palaste

natG said:
In a class that implements Comparable, and has a custom compareTo, why bother
writing a custom equeals, if equeals can simply check if compareTo returns 0?
public boolean equeals(Object o){
return (compareTo(o)=0);
}
Does this make sense?

That makes sense, but I believe that no, it will not suffice. Consider
this: Comparing another object to "this" can have four results, not
three:
1) "this" is bigger.
2) "this" is smaller.
3) They're equal.
4) They can't be compared at all, most probably because the other object
is of the wrong class.
Your equals() (or as you have spelled it, equeals()) handles cases 1-3
but not case 4. The right thing to do in case 4 is throw a
ClassCastException with a message such as "Wrong class".
 
J

Jos A. Horsmeier

natG said:
In a class that implements Comparable, and has a custom compareTo, why bother
writing a custom equeals, if equeals can simply check if compareTo returns 0?

public boolean equeals(Object o){
return (compareTo(o)=0);
}

Does this make sense?

Sure it does (let go of the typo '=' instead of '==').
If you've implemented a PTO (Partial Total Ordering) correctly,
the 'equals' method is no more no less than you suggested above.

kind regards,

Jos
 
J

Joona I Palaste

Sure it does (let go of the typo '=' instead of '==').
If you've implemented a PTO (Partial Total Ordering) correctly,
the 'equals' method is no more no less than you suggested above.

Except that it will throw a ClassCastException when its parameter is of
a class that can't be compared to, when it should simply return false.
Try this:

public boolean equals(Object o) {
try {
return compareTo(o)==0;
}
catch (Exception e) {
return false;
}
}
 
N

natG

Thank you both.

I failed to mention, something that I took for granted, that I also check if the
object isInstanceOf (excuse wrong spelling please) the same class, and if not, let
equals simply return false. Like this I save the exception logic, and its a
one-liner.

TA
-nat
 
C

Chris Smith

Joona said:
Except that it will throw a ClassCastException when its parameter is of
a class that can't be compared to, when it should simply return false.

Exactly. Good catch! I was about to say it was fine, and hadn't
thought about those conditions. Same thing with equals(null), which
should return false, where compareTo(null) should throw
NullPointerException.
Try this:

public boolean equals(Object o) {
try {
return compareTo(o)==0;
}
catch (Exception e) {
return false;
}
}

Have you considered what happens when you run this code and compareTo
throws an IndexOutOfBoundsException because of some radically corrupted
program state? You end up hiding the failure for longer, making some
arbitrary choice that may lead incorrect data to be fed to some other
part of the program, with very bad results. Better to write:

public boolean equals(Object o)
{
if (!(o instanceof ThisClass)) return false;
else return (compareTo(o) == 0);
}

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top