-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
1.2.0
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.
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.
- relates to
-
JDK-6321015 BigDecimal should have comparison methods that return a boolean
-
- Closed
-