Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Threaded View
Hadoop, mail # user - Implementing compareTo in user-written keys where one extends the other is error prone


Copy link to this message
-
Re: Implementing compareTo in user-written keys where one extends the other is error prone
Sharad Agarwal 2009-05-04, 06:38

Marshall Schor wrote:
>
> public class Super implements WritableComparable<Super> {
>  . . .
>   public int compareTo(Super o) {
>     // sort on string value
>     . . .
>   }
>
> I implemented the 2nd key class (let's call it Sub)
>
> public class Sub extends Super {
>  . . .
>   public int compareTo(Sub o) {
>     // sort on boolean value
>     . . .
>     // if equal, use the super:
>     ... else
>      return super.compareTo(o);
>   }
>
The overridden method must have same arguments as the parent class
method. Otherwise it is just another method, not an overridden one.
In your case, if the current code looks like error prone, you can
make Super also as a template. Then you can use the Sub class in
the compareTo method However you will have to cast in the
Super class.

class Super<T> implements WritableComparable<T> {
  public int compareTo(T o) {
   Super other = (Super) o;
   ....
  }
}

class Sub extends Super<Sub> {
  public int compareTo(Sub o) {
    ...
  }
}

-Sharad