Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8241056

Add default methods to java.lang.Comparable interface

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      Dealing with the Comparable interface is such a pain.
      If you need to write some checks like
      if( a > b and b != c or b <= d )
      you have to write something like:
      if( ( a.compareTo(b) > 0 && b.compareTo(c) != 0 || b.compareTo(d) <= 0 )
      This is really annoying to write and quite hard to understand.
      If you need to make a lot of checks it could be prone of errors.
      This can be solved quite easily by adding some convenience default methods like:
      default boolean lt(T o)
      {
        return compareTo(o) < 0;
      }

      I suggest the following methods:
      lt: for "less than"
      le: for "less or equal"
      eq: for "is equal to"
      ne: for "is not equal to"
      ge: for "greater or equal"
      gt: for "greater than"

      This way the previous example will become:
      if( a.gt(b) && b.ne(c) || b.le(d) )
      That is much similar to the mathematical representation and therefore more readable.
      Moreover, adding default methods will not break any existing code, so it is a safe improvement.


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: