Well, the conventional way in a lot of programming languages (not just Java) is for the comparison functions to return a number, negative if the first one comes before the second one, zero if both should be considered of equal rank, and positive if the first one comes after the second one.
What you could do, if you like is add your own methods, for example:
boolean isBefore(Rank rank) {
return this.compareTo(rank) < 0;
}
boolean isSame(Rank rank) {
return this.compareTo(rank) == 0;
}
boolean isAfter(Rank rank) {
return this.compareTo(rank) > 0;
}
You can even create your own generic interface, with default methods:
public interface EasyComparable<C> extends Comparable<C> {
default boolean isBefore(C that) {
return this.compareTo(that) < 0;
}
default boolean isSame(C that) {
return this.compareTo(that) == 0;
}
default boolean isAfter(C that) {
return this.compareTo(that) > 0;
}
}
And then have your Rank
implement EasyComparator
instead of Comparator
.
Then you will be able to do:
if (rankA.isBefore(rankB)) {
doSomething();
}
else if (rankA.isSame(rankB)) {
doSomething();
}
else if (rankA.isAfter(rankB)) {
doSomething();
}
You will then be able to do this on other objects too, not just Rank
.
If you don't have control on the Rank
class you can also extend Comparator
instead, and add the 3 default methods too, but they will have to take two arguments instead of one.