public final class InternalPreconditions extends Object
Following table summarizes the grouping of the checks:
┌────────┬─────────────────────────────────────────────────────┬───────────────────────────────┐ │Group │Description │Common Exception Types │ ├────────┼─────────────────────────────────────────────────────┼───────────────────────────────┤ │BOUNDS │Checks related to the bound checking in collections. │IndexOutBoundsException │ │ │ │ArrayIndexOutOfBoundsException │ ├────────┼─────────────────────────────────────────────────────┼───────────────────────────────┤ │API │Checks related to the correct usage of APIs. │IllegalStateException │ │ │ │NoSuchElementException │ │ │ │NullPointerException │ │ │ │IllegalArgumentException │ │ │ │ConcurrentModificationException│ ├────────┼─────────────────────────────────────────────────────┼───────────────────────────────┤ │NUMERIC │Checks related to numeric operations. │ArithmeticException │ ├────────┼─────────────────────────────────────────────────────┼───────────────────────────────┤ │TYPE │Checks related to java type system. │ClassCastException │ │ │ │ArrayStoreException │ ├────────┼─────────────────────────────────────────────────────┼───────────────────────────────┤ │CRITICAL│Checks for cases where not failing-fast will keep │IllegalArgumentException │ │ │the object in an inconsistent state and/or degrade │ │ │ │debugging significantly. Currently disabling these │ │ │ │checks is not supported. │ │ └────────┴─────────────────────────────────────────────────────┴───────────────────────────────┘
Following table summarizes predefined check levels:
┌────────────────┬──────────┬─────────┬─────────┬─────────┬─────────┐ │Check level │ BOUNDS │ API │ NUMERIC | TYPE │CRITICAL │ ├────────────────┼──────────┼─────────┼─────────┼─────────┼─────────┤ │Normal (default)│ X │ X │ X │ X │ X │ ├────────────────┼──────────┼─────────┼─────────┼─────────┼─────────┤ │Optimized │ │ │ │ X │ X │ ├────────────────┼──────────┼─────────┼─────────┼─────────┼─────────┤ │Minimal │ │ │ │ │ X │ ├────────────────┼──────────┼─────────┼─────────┼─────────┼─────────┤ │None (N/A yet) │ │ │ │ │ │ └────────────────┴──────────┴─────────┴─────────┴─────────┴─────────┘
Please note that, in development mode (jre.checkedMode=ENABLED), these checks will always be performed regardless of configuration but will be converted to AssertionError if check is disabled. This so that any reliance on related exceptions could be detected early on. For this detection to work properly; it is important for apps to share the same config in all environments.
Modifier and Type | Method and Description |
---|---|
static void |
checkArgument(boolean expression)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkArgument(boolean expression,
Object errorMessage)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkArgument(boolean expression,
String errorMessageTemplate,
Object... errorMessageArgs)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkArithmetic(boolean expression) |
static void |
checkArraySize(int size)
Ensures that
size specifies a valid array size (i.e. |
static void |
checkArrayType(boolean expression)
Ensures the truth of an expression that verifies array type.
|
static void |
checkArrayType(boolean expression,
Object errorMessage)
Ensures the truth of an expression that verifies array type.
|
static void |
checkCriticalArgument(boolean expression)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkCriticalArgument(boolean expression,
Object errorMessage)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkCriticalArgument(boolean expression,
String errorMessageTemplate,
Object... errorMessageArgs)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkCriticalArithmetic(boolean expression) |
static void |
checkCriticalArrayBounds(int start,
int end,
int length)
Checks that array bounds are correct.
|
static void |
checkCriticalArraySize(int size) |
static void |
checkCriticalArrayType(boolean expression) |
static void |
checkCriticalArrayType(boolean expression,
Object errorMessage) |
static void |
checkCriticalElement(boolean expression)
Ensures the truth of an expression involving existence of an element.
|
static void |
checkCriticalElement(boolean expression,
Object errorMessage)
Ensures the truth of an expression involving existence of an element.
|
static void |
checkCriticalElementIndex(int index,
int size) |
static void |
checkCriticalNotNull(Object reference,
Object errorMessage) |
static <T> T |
checkCriticalNotNull(T reference) |
static void |
checkCriticalPositionIndex(int index,
int size) |
static void |
checkCriticalPositionIndexes(int start,
int end,
int size)
Ensures that
start and end specify a valid positions in a list
of size size , and are in order. |
static void |
checkCriticalState(boolean expression)
Ensures the truth of an expression involving the state of the calling instance, but not
involving any parameters to the calling method.
|
static void |
checkCriticalState(boolean expression,
Object errorMessage)
Ensures the truth of an expression involving the state of the calling instance, but not
involving any parameters to the calling method.
|
static void |
checkCriticalStringBounds(int start,
int end,
int length)
Checks that string bounds are correct.
|
static void |
checkCriticalStringElementIndex(int index,
int size) |
static void |
checkCriticalType(boolean expression) |
static void |
checkCriticalType(boolean expression,
String message) |
static void |
checkElement(boolean expression)
Ensures the truth of an expression involving existence of an element.
|
static void |
checkElement(boolean expression,
Object errorMessage)
Ensures the truth of an expression involving existence of an element.
|
static void |
checkElementIndex(int index,
int size)
Ensures that
index specifies a valid element in a list or string of size
size . |
static void |
checkNotNull(Object reference,
Object errorMessage)
Ensures that an object reference passed as a parameter to the calling method is not null.
|
static <T> T |
checkNotNull(T reference)
Ensures that an object reference passed as a parameter to the calling method is not null.
|
static void |
checkPositionIndex(int index,
int size)
Ensures that
index specifies a valid position in a list of
size size . |
static void |
checkPositionIndexes(int start,
int end,
int size)
Ensures that
start and end specify a valid positions in a list
of size size , and are in order. |
static void |
checkState(boolean expression)
Ensures the truth of an expression involving the state of the calling instance, but not
involving any parameters to the calling method.
|
static void |
checkState(boolean expression,
Object errorMessage)
Ensures the truth of an expression involving the state of the calling instance, but not
involving any parameters to the calling method.
|
static void |
checkStringElementIndex(int index,
int size) |
static void |
checkType(boolean expression) |
static void |
checkType(boolean expression,
String message) |
static boolean |
isApiChecked()
This method reports if the code is compiled with api checks.
|
static boolean |
isTypeChecked()
This method reports if the code is compiled with type checks.
|
public static boolean isTypeChecked()
System.arraycopy(Object, int, Object, int, int)
for example.
Please note that checkType(boolean)
should be preferred where feasible.public static boolean isApiChecked()
#checkXXX(boolean)
should be preferred where feasible.public static void checkType(boolean expression)
public static void checkType(boolean expression, String message)
public static void checkCriticalType(boolean expression)
public static void checkCriticalType(boolean expression, String message)
public static void checkArrayType(boolean expression)
public static void checkCriticalArrayType(boolean expression)
public static void checkArrayType(boolean expression, Object errorMessage)
public static void checkCriticalArrayType(boolean expression, Object errorMessage)
public static void checkArithmetic(boolean expression)
public static void checkCriticalArithmetic(boolean expression)
public static void checkElement(boolean expression)
public static void checkCriticalElement(boolean expression)
For cases where failing fast is pretty important and not failing early could cause bugs that are much harder to debug.
public static void checkElement(boolean expression, Object errorMessage)
public static void checkCriticalElement(boolean expression, Object errorMessage)
For cases where failing fast is pretty important and not failing early could cause bugs that are much harder to debug.
public static void checkArgument(boolean expression)
public static void checkCriticalArgument(boolean expression)
For cases where failing fast is pretty important and not failing early could cause bugs that are much harder to debug.
public static void checkArgument(boolean expression, Object errorMessage)
public static void checkCriticalArgument(boolean expression, Object errorMessage)
For cases where failing fast is pretty important and not failing early could cause bugs that are much harder to debug.
public static void checkArgument(boolean expression, String errorMessageTemplate, Object... errorMessageArgs)
public static void checkCriticalArgument(boolean expression, String errorMessageTemplate, Object... errorMessageArgs)
For cases where failing fast is pretty important and not failing early could cause bugs that are much harder to debug.
public static void checkState(boolean expression)
expression
- a boolean expressionIllegalStateException
- if expression
is falsepublic static void checkCriticalState(boolean expression)
For cases where failing fast is pretty important and not failing early could cause bugs that are much harder to debug.
public static void checkState(boolean expression, Object errorMessage)
public static void checkCriticalState(boolean expression, Object errorMessage)
public static <T> T checkNotNull(T reference)
public static <T> T checkCriticalNotNull(T reference)
public static void checkNotNull(Object reference, Object errorMessage)
public static void checkArraySize(int size)
size
specifies a valid array size (i.e. non-negative).public static void checkCriticalArraySize(int size)
public static void checkElementIndex(int index, int size)
index
specifies a valid element in a list or string of size
size
. An element index may range from zero, inclusive, to size
, exclusive.public static void checkCriticalElementIndex(int index, int size)
public static void checkStringElementIndex(int index, int size)
public static void checkCriticalStringElementIndex(int index, int size)
public static void checkPositionIndex(int index, int size)
index
specifies a valid position in a list of
size size
. A position index may range from zero to size
, inclusive.public static void checkCriticalPositionIndex(int index, int size)
public static void checkPositionIndexes(int start, int end, int size)
start
and end
specify a valid positions in a list
of size size
, and are in order. A position index may range from zero to
size
, inclusive.public static void checkCriticalPositionIndexes(int start, int end, int size)
start
and end
specify a valid positions in a list
of size size
, and are in order. A position index may range from zero to
size
, inclusive.public static void checkCriticalArrayBounds(int start, int end, int length)
IllegalArgumentException
- if start > end
ArrayIndexOutOfBoundsException
- if the range is not legalpublic static void checkCriticalStringBounds(int start, int end, int length)
StringIndexOutOfBoundsException
- if the range is not legalCopyright © 2018. All rights reserved.