Hacker News new | ask | show | jobs
by tomfitz 3312 days ago
An alternative approach is Guava's Range class:

Range.closed(1,5) == [1,5]

Range.open(1,5) == (1,5)

Range.openClosed(1,5) == (1,5]

Range.closedOpen(1,5) == [1,5)

Range.greaterThan(1) == (1,infinity)

Range.atLeast(1) == [1,infinity)

That class always strikes me as having high power-to-weight. It has methods like encloses(anotherRange), contains(aValue), and others.

https://google.github.io/guava/releases/19.0/api/docs/com/go...

3 comments

Should the second "closed" be "closed Open" (with the corresponding change to the rest of the line)?
Fixed, thanks.
What does a Guava two-Integer-element array literal look like?
Sorry, I didn't explain myself well.

Range.closed(1,5) is the syntax used to refer to what a mathematician would call [1,5].

Guava is just a Java library. In Java 9, a two-Integer-element list literal will be List.of(9000, 9001).

Too verbose for me. I always prefer short syntactic sugar.