Java Utililty Methods Number Range Check

List of utility methods to do Number Range Check

Description

The list of methods to do Number Range Check are organized into topic(s).

Method

doublerangeCheck(double value)
Checks 0..1 range
if ((value < 0.0) || (value > 1.0)) {
    throw new IllegalArgumentException("Humanity should been between 0.0 and 1.0!");
} else {
    return value;
voidrangeCheck(final C start, final C end, final C step)
range Check
if (step.doubleValue() == 0) {
    throw new IllegalArgumentException("step is 0.");
if (start.compareTo(end) < 0 && step.doubleValue() < 0) {
    throw new IllegalArgumentException("start < end, but step is negative");
intrangeCheck(final int value, final int min, final int max)
range Check
if (value >= min && value <= max) {
    return value;
if (min == Integer.MIN_VALUE) {
    throw new IllegalArgumentException(
            String.format("Value must be less than or equals to %,d: %,d", max, value));
if (max == Integer.MAX_VALUE) {
...
voidrangeCheck(int arrayLen, int fromIndex, int toIndex)
range Check
if (fromIndex > toIndex)
    throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
if (fromIndex < 0)
    throw new ArrayIndexOutOfBoundsException(fromIndex);
if (toIndex > arrayLen)
    throw new ArrayIndexOutOfBoundsException(toIndex);
booleanrangeCheck(int arrayLength, int offset, int length)
range Check
return offset >= 0 && offset <= arrayLength && length >= 0 && length <= arrayLength - offset;
voidrangeCheck(int index, int size)
Checks if the given index is in range.
if (index >= size) {
    throw new IndexOutOfBoundsException(outOfBoundsMsg(index, size));
voidrangeCheck(int length, int fromIndex, int toIndex)
Checks that fromIndex and toIndex are in the range and throws an appropriate exception, if they aren't.
if (fromIndex > toIndex) {
    throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
if (fromIndex < 0) {
    throw new ArrayIndexOutOfBoundsException(fromIndex);
if (toIndex > length) {
    throw new ArrayIndexOutOfBoundsException(toIndex);
...
intrangeCheck(int value, int begin, int end)
range Check
if (value >= begin && value <= end) { 
    return value;
throw new IllegalArgumentException("Value [" + value + "] not in range [" + begin + "," + end + "]");
voidrangeCheck(int value, int min, int max)
Throws an exception whenever a value falls outside the given range.
if (value < min || value > max)
    throw new IllegalArgumentException("Value falls out of required range [" + min + "," + max + "].");
voidrangeCheckForAdd(int index, int size)
A version of rangeCheck used by add and addAll.
if (index > size || index < 0) {
    throw new IndexOutOfBoundsException(outOfBoundsMsg(index, size));