0

I have the following objects (assuming the class Rank implements Comparable):

Rank rankA;
Rank rankB;

I would compare them like this:

if(rankA.compareTo(rankB) < 0) // rankA < rankB
    doSomething();

or

if(rankA.compareTo(rankB) == 0) // rankA == rankB
    doSomething();

or

if(rankA.compareTo(rankB) > 0) // rankA > rankB
    doSomething();

I find the above if statements hard to read, even with the added comments. Is there a more elegant, more readable solution? I am aware that I could implement a method for this, but I am looking for a solution that's preferably already implemented, so I don't have to create a util-method.

domisum
  • 531
  • 1
  • 7
  • 12
  • Actually, I don't find it so hard to read. Just imagine to replace the `compareTo` by the operator to the right. – Henry Oct 12 '17 at 14:26

3 Answers3

2

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.

jbx
  • 21,365
  • 18
  • 90
  • 144
0

Perhaps not the answer you want, but you could use Kotlin instead. Here you can write code similar to what you mention in your comments:

fun main(args: Array<String>) {
    val s1 = "aaaa"
    val s2 = "bbbb"
    val s3 = "cccc"
    println("s1 < s2: ${s1 < s2}")
    println("s1 == s2: ${s1 == s2}")
    println("s1 < s2: ${s1.compareTo(s2) < 0}")
    println("s1 > s2: ${s1 > s2}")
    println("s3 > s2: ${s3 > s2}")
}

s1 < s2 is effectively s1.compareTo(s2) < 0.

So Kotlin addressed the problem you mention.

Output of the code above:

s1 < s2: true
s1 == s2: false
s1 < s2: true
s1 > s2: false
s3 > s2: true
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
0

Your code is good. I would use it as below:

int result = rankA.compareTo(rankB);
if(result == 0) {
    //do sth
} else if (result <0) {
    //do sth
} else {
    //do sth
}
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59