The Tuple<L,R>
class is relatively simple to design and use.
In order to give you a bigger challenge,
we are going to adapt the Tuple we created to implement the Comparable
interface.
That means that one Tuple of type ComparableTuple<L,R>
can be compared to another one of the same type:
compareTo(ComparableTuple<L,R> other)
.
To start change your Tuple class to this: (Tip! Make this a new class!)
public ComparableTuple<L,R> extends Comparable<ComparableTuple<L,R>> {
// ...
}
Please study this line very carefully and make sure that you understand why it is the way it is.
Afterward ask yourself the question HOW will you determine
order when you don’t know what the L
and R
types are going
to be?
Both types must be restricted to extend Comparable as well:
public ComparableTuple<L extends Comparable<L>, R extends Comparable<R>> extends Comparable<ComparableTuple<L,R>> {
// ...
}
Please note that the last part remains unchanged. But L and R are now restricted to types that have a Comparable interface.
Assignment
ComparableTuple
class.Comparator<Tuple<L,R>>
(through the constructor)
to handle the ordering.