default methods for java.lang.Comparable: isGreaterThan, ...

jimmy.praet at telenet.be jimmy.praet at telenet.be
Sat Nov 12 11:26:21 UTC 2016


Hi, 

What are your thoughts on adding these default convenience methods to the java.lang.Comparable interface? 

public interface Comparable<T> { 
int compareTo(T o); 
default boolean isGreaterThan(T o) { 
return compareTo(o) > 0; 
} 
default boolean isGreaterThanOrEqualTo(T o) { 
return compareTo(o) >= 0; 
} 
default boolean isLessThan(T o) { 
return compareTo(o) < 0; 
} 
default boolean isLessThanOrEqualTo(T o) { 
return compareTo(o) <= 0; 
} 
} 

code like "if (x.isGreaterThan(y))" is a lot more readable and expressive than "if (x.compareTo(y) > 0)". 

Kind regards, 
Jimmy 


More information about the core-libs-dev mailing list