I’ve never really been a big fan of the syntax of java.lang.Comparable.compareTo()
. For some reason I find myself constantly referring to the JavaDocs to remind me whether < 0 or > 0 means the argument is less vs more than the callee.
So I decided to do something about it, and created a minimal API with the sole purpose of improving readability of compareTo()
.
The API supplies methods in two flavors, expressive or abbreviated. The expressive version looks like this:
import static se.jiderhamn.CompareTo.is; ... boolean oneIsZero = is(1).equalTo(0); boolean aIsNotZero = is(a).notEqualTo(0); boolean bIsZero = is(b).zero(); boolean value1LessThanValue2 = is(value1).lessThan(value2); if(is(a).lessThanOrEqualTo(b)) { ... } boolean date1AfterDate2 = is(date1).greaterThan(date2); if(is(a).greaterThanOrEqualTo(b)) { ... }
The abbreviated syntax looks like this:
import static se.jiderhamn.CompareTo.is; ... boolean oneIsZero = is(1).eq(0); boolean aIsNotZero = is(a).ne(0); boolean value1LessThanValue2 = is(value1).lt(value2); if(is(a).le(b)) { ... } boolean date1AfterDate2 = is(date1).gt(date2); if(is(a).ge(b)) { ... }
The API is available on Maven Central:
<dependency> <groupId>se.jiderhamn</groupId> <artifactId>compare-to</artifactId> <version>1.1.1</version> </dependency>
Source are on GitHub.
Manual download here.