Apache Commons Lang has a Range
class for doing arbitrary ranges.
Range<Integer> test = Range.between(1, 3);
System.out.println(test.contains(2));
System.out.println(test.contains(4));
Guava Range
has similar API.
If you are just wanting to check if a number fits into a long value or an int value, you could try using it through BigDecimal
. There are methods for longValueExact
and intValueExact
that throw exceptions if the value is too big for those precisions.