public final class InvariantChecks
extends java.lang.Object
InvariantChecks
class provides static methods for checking different
kinds of invariants. If a check fails a corresponding exception is thrown.Modifier and Type | Method and Description |
---|---|
static void |
checkBounds(int index,
int length)
Checks the invariant "
0 <= index < length " and
throws an exception if it is violated. |
static void |
checkBoundsInclusive(int index,
int length)
Checks the invariant "
0 <= index <= length " and
throws an exception if it is violated. |
static void |
checkFalse(boolean condition)
Checks the invariant "Condition is false" and throws an exception
if it is violated.
|
static void |
checkFalse(boolean condition,
java.lang.String message)
Checks the invariant "Condition is false" and throws an exception
if it is violated.
|
static <T extends java.lang.Number & java.lang.Comparable<T>> |
checkGreaterOrEq(T first,
T second)
Checks the invariant "Numeric value
first is greater than or equal to second "
and throws an exception if it is violated. |
static void |
checkGreaterOrEqZero(int num)
Checks the invariant "Integer value is greater or equal
0 " and
throws an exception if it is violated. |
static <T extends java.lang.Number & java.lang.Comparable<T>> |
checkGreaterThan(T first,
T second)
Checks the invariant "Numeric value
first is greater than second "
and throws an exception if it is violated. |
static void |
checkGreaterThanZero(int num)
Checks the invariant "Integer value is greater than
0 " and
throws an exception if it is violated. |
static <T> void |
checkNotEmpty(java.util.Collection<T> collection)
Checks the invariants "Object reference is not null" and "Collection is not empty"
throws an exception if they are violated.
|
static <T> void |
checkNotEmpty(T[] array)
Checks the invariants "Object reference is not null" and "Array is not empty"
throws an exception if they are violated.
|
static <T> void |
checkNotNull(T objType)
Checks the invariant "Object reference is not null" and
throws an exception if it is violated.
|
static <T> void |
checkNotNull(T obj,
java.lang.String message)
Checks the invariant "Object reference is not null" and
throws an exception if it is violated.
|
static void |
checkTrue(boolean condition)
Checks the invariant "Condition is true" and throws an exception
if it is violated.
|
static void |
checkTrue(boolean condition,
java.lang.String message)
Checks the invariant "Condition is true" and throws an exception
if it is violated.
|
public static void checkTrue(boolean condition)
condition
- Condition to be checked.java.lang.IllegalArgumentException
- if the invariant is violated
(condition
is false
).public static void checkTrue(boolean condition, java.lang.String message)
condition
- Condition to be checked.message
- Exception message.java.lang.IllegalArgumentException
- if the invariant is violated
(condition
is false
).public static void checkFalse(boolean condition)
condition
- Condition to be checked.java.lang.IllegalArgumentException
- if the invariant is violated
(condition
is true
).public static void checkFalse(boolean condition, java.lang.String message)
condition
- Condition to be checked.message
- Exception message.java.lang.IllegalArgumentException
- if the invariant is violated
(condition
is true
).public static <T> void checkNotNull(T objType)
T
- Object type.objType
- Object reference to be checked.java.lang.IllegalArgumentException
- if the invariant is violated (o
is null
).public static <T> void checkNotNull(T obj, java.lang.String message)
T
- Object type.obj
- Object reference to be checked.message
- Exception message.java.lang.IllegalArgumentException
- if the invariant is violated (o
is null
).public static <T> void checkNotEmpty(java.util.Collection<T> collection)
T
- Collection item type.collection
- Collection to be checked.java.lang.IllegalArgumentException
- if the invariant is violated (o
is null
);
if the invariant is violated (o.isEmpty
).public static <T> void checkNotEmpty(T[] array)
T
- Array element type.array
- Array to be checked.java.lang.IllegalArgumentException
- if the invariant is violated (o
is null
);
if the invariant is violated (o.length
is 0).public static void checkGreaterThanZero(int num)
0
" and
throws an exception if it is violated.num
- Integer value to be checked.java.lang.IllegalArgumentException
- if the invariant is violated (n <= 0
).public static void checkGreaterOrEqZero(int num)
0
" and
throws an exception if it is violated.num
- Integer value to be checked.java.lang.IllegalArgumentException
- if the invariant is violated (n < 0
).public static void checkBounds(int index, int length)
0 <= index < length
" and
throws an exception if it is violated.index
- Index to be checked.length
- Length of the allowed range.java.lang.IndexOutOfBoundsException
- if the invariant is violated
(index
is not within range [0..length)
).public static void checkBoundsInclusive(int index, int length)
0 <= index <= length
" and
throws an exception if it is violated.index
- Index to be checked.length
- Length of the allowed range.java.lang.IndexOutOfBoundsException
- if the invariant is violated
(index
is not within range [0..length]
).public static <T extends java.lang.Number & java.lang.Comparable<T>> void checkGreaterThan(T first, T second)
first
is greater than second
"
and throws an exception if it is violated.T
- Value type.first
- Numeric value to be checked.second
- Numeric value to be checked.java.lang.IllegalArgumentException
- if the invariant is violated (a <= b
).public static <T extends java.lang.Number & java.lang.Comparable<T>> void checkGreaterOrEq(T first, T second)
first
is greater than or equal to second
"
and throws an exception if it is violated.T
- Value type.first
- Numeric value to be checked.second
- Numeric value to be checked.java.lang.IllegalArgumentException
- if the invariant is violated (a < b
).