Package ru.ispras.fortress.util
Class InvariantChecks
- java.lang.Object
-
- ru.ispras.fortress.util.InvariantChecks
-
public final class InvariantChecks extends java.lang.Object
TheInvariantChecks
class provides static methods for checking different kinds of invariants. If a check fails a corresponding exception is thrown.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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>>
voidcheckGreaterOrEq(T first, T second)
Checks the invariant "Numeric valuefirst
is greater than or equal tosecond
" and throws an exception if it is violated.static void
checkGreaterOrEqZero(int num)
Checks the invariant "Integer value is greater or equal0
" and throws an exception if it is violated.static <T extends java.lang.Number & java.lang.Comparable<T>>
voidcheckGreaterThan(T first, T second)
Checks the invariant "Numeric valuefirst
is greater thansecond
" and throws an exception if it is violated.static void
checkGreaterThanZero(int num)
Checks the invariant "Integer value is greater than0
" 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.
-
-
-
Method Detail
-
checkTrue
public static void checkTrue(boolean condition)
Checks the invariant "Condition is true" and throws an exception if it is violated.- Parameters:
condition
- Condition to be checked.- Throws:
java.lang.IllegalArgumentException
- if the invariant is violated (condition
isfalse
).
-
checkTrue
public static void checkTrue(boolean condition, java.lang.String message)
Checks the invariant "Condition is true" and throws an exception if it is violated.- Parameters:
condition
- Condition to be checked.message
- Exception message.- Throws:
java.lang.IllegalArgumentException
- if the invariant is violated (condition
isfalse
).
-
checkFalse
public static void checkFalse(boolean condition)
Checks the invariant "Condition is false" and throws an exception if it is violated.- Parameters:
condition
- Condition to be checked.- Throws:
java.lang.IllegalArgumentException
- if the invariant is violated (condition
istrue
).
-
checkFalse
public static void checkFalse(boolean condition, java.lang.String message)
Checks the invariant "Condition is false" and throws an exception if it is violated.- Parameters:
condition
- Condition to be checked.message
- Exception message.- Throws:
java.lang.IllegalArgumentException
- if the invariant is violated (condition
istrue
).
-
checkNotNull
public static <T> void checkNotNull(T objType)
Checks the invariant "Object reference is not null" and throws an exception if it is violated.- Type Parameters:
T
- Object type.- Parameters:
objType
- Object reference to be checked.- Throws:
java.lang.IllegalArgumentException
- if the invariant is violated (o
isnull
).
-
checkNotNull
public 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.- Type Parameters:
T
- Object type.- Parameters:
obj
- Object reference to be checked.message
- Exception message.- Throws:
java.lang.IllegalArgumentException
- if the invariant is violated (o
isnull
).
-
checkNotEmpty
public 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.- Type Parameters:
T
- Collection item type.- Parameters:
collection
- Collection to be checked.- Throws:
java.lang.IllegalArgumentException
- if the invariant is violated (o
isnull
); if the invariant is violated (o.isEmpty
).
-
checkNotEmpty
public 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.- Type Parameters:
T
- Array element type.- Parameters:
array
- Array to be checked.- Throws:
java.lang.IllegalArgumentException
- if the invariant is violated (o
isnull
); if the invariant is violated (o.length
is 0).
-
checkGreaterThanZero
public static void checkGreaterThanZero(int num)
Checks the invariant "Integer value is greater than0
" and throws an exception if it is violated.- Parameters:
num
- Integer value to be checked.- Throws:
java.lang.IllegalArgumentException
- if the invariant is violated (n <= 0
).
-
checkGreaterOrEqZero
public static void checkGreaterOrEqZero(int num)
Checks the invariant "Integer value is greater or equal0
" and throws an exception if it is violated.- Parameters:
num
- Integer value to be checked.- Throws:
java.lang.IllegalArgumentException
- if the invariant is violated (n < 0
).
-
checkBounds
public static void checkBounds(int index, int length)
Checks the invariant "0 <= index < length
" and throws an exception if it is violated.- Parameters:
index
- Index to be checked.length
- Length of the allowed range.- Throws:
java.lang.IndexOutOfBoundsException
- if the invariant is violated (index
is not within range[0..length)
).
-
checkBoundsInclusive
public static void checkBoundsInclusive(int index, int length)
Checks the invariant "0 <= index <= length
" and throws an exception if it is violated.- Parameters:
index
- Index to be checked.length
- Length of the allowed range.- Throws:
java.lang.IndexOutOfBoundsException
- if the invariant is violated (index
is not within range[0..length]
).
-
checkGreaterThan
public static <T extends java.lang.Number & java.lang.Comparable<T>> void checkGreaterThan(T first, T second)
Checks the invariant "Numeric valuefirst
is greater thansecond
" and throws an exception if it is violated.- Type Parameters:
T
- Value type.- Parameters:
first
- Numeric value to be checked.second
- Numeric value to be checked.- Throws:
java.lang.IllegalArgumentException
- if the invariant is violated (a <= b
).
-
checkGreaterOrEq
public static <T extends java.lang.Number & java.lang.Comparable<T>> void checkGreaterOrEq(T first, T second)
Checks the invariant "Numeric valuefirst
is greater than or equal tosecond
" and throws an exception if it is violated.- Type Parameters:
T
- Value type.- Parameters:
first
- Numeric value to be checked.second
- Numeric value to be checked.- Throws:
java.lang.IllegalArgumentException
- if the invariant is violated (a < b
).
-
-