Class Checks
Checks should be enabled or not.
It is sometimes useful to disable preconditions checks at runtime environment
to squeeze out more performance in a tight loop.
The common pattern is having a
private static final boolean CHECK = Check.isEnabled(MyClass.class);
constant in your class. When using Checks just wrap the call in if-statement like so:
if (CHECK) Preconditions.checkNotNull(value);
By default, all checks are disabled (like java asserts).
To enable all checks you can run application with system property -Dchk=on
You can enable or disable checks for the whole package (with its subpackages) or for individual classes
like so: -Dchk:io.activej.eventloop=on -Dchk:io.activej.promise.Promise=off.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckArgument(boolean expression) Checks a validity of an argumentstatic voidcheckArgument(boolean expression, Object message) Checks a validity of an argumentstatic voidcheckArgument(boolean expression, String template, Object... args) Checks a validity of an argumentstatic voidcheckArgument(boolean expression, Supplier<String> message) Checks a validity of an argumentstatic <T> TcheckArgument(T argument, Predicate<T> predicate) Checks a validity of a given argumentstatic <T> TcheckArgument(T argument, Predicate<T> predicate, Object message) Checks a validity of a given argumentstatic <T> TcheckArgument(T argument, Predicate<T> predicate, String template, Object... args) Checks a validity of a given argumentstatic <T> TcheckArgument(T argument, Predicate<T> predicate, Function<T, String> message) Checks a validity of a given argumentstatic <T> TcheckArgument(T argument, Predicate<T> predicate, Supplier<String> message) Checks a validity of a given argumentstatic <T> TcheckNotNull(T reference) Checks an object for nullnessstatic <T> TcheckNotNull(T reference, Object message) Checks an object for nullnessstatic <T> TcheckNotNull(T reference, String template, Object... args) Checks an object for nullnessstatic <T> TcheckNotNull(T reference, Supplier<String> message) Checks an object for nullnessstatic voidcheckState(boolean expression) Checks a validity of a statestatic voidcheckState(boolean expression, Object message) Checks a validity of a statestatic voidcheckState(boolean expression, String template, Object... args) Checks a validity of a statestatic voidcheckState(boolean expression, Supplier<String> message) Checks a validity of a statestatic booleanIndicates whether checks are enabled or disabled for the specified classstatic booleanReturns a boolean that indicates whether checks are enabled by default
-
Constructor Details
-
Checks
public Checks()
-
-
Method Details
-
isEnabled
Indicates whether checks are enabled or disabled for the specified class- Parameters:
cls- class to be checked- Returns:
trueif checks are enabled for the given class,falseotherwise
-
isEnabledByDefault
public static boolean isEnabledByDefault()Returns a boolean that indicates whether checks are enabled by default -
checkNotNull
@NotNull public static <T> T checkNotNull(@Nullable T reference) Checks an object for nullnessIf an object is not
nullthen it is returned as-is. Otherwise, aNullPointerExceptionis thrown- Type Parameters:
T- a type of object to be checked- Parameters:
reference- an object to be checked for nullness- Returns:
- a checked object
- Throws:
NullPointerException- if an object is null
-
checkNotNull
Checks an object for nullnessIf an object is not
nullthen it is returned as-is. Otherwise, aNullPointerExceptionis thrown with a specified message- Type Parameters:
T- a type of object to be checked- Parameters:
reference- an object to be checked for nullnessmessage- an object that represents a message to be used in thrownNullPointerException- Returns:
- a checked object
- Throws:
NullPointerException- if an object is null
-
checkNotNull
Checks an object for nullnessIf an object is not
nullthen it is returned as-is. Otherwise, aNullPointerExceptionis thrown with a supplied message- Type Parameters:
T- a type of object to be checked- Parameters:
reference- an object to be checked for nullnessmessage- a supplier of message to be used in thrownNullPointerException- Returns:
- a checked object
- Throws:
NullPointerException- if an object is null
-
checkNotNull
Checks an object for nullnessIf an object is not
nullthen it is returned as-is. Otherwise, aNullPointerExceptionis thrown with a templated message- Type Parameters:
T- a type of object to be checked- Parameters:
reference- an object to be checked for nullnesstemplate- a template of message to be used in thrownNullPointerExceptionargs- arguments to an error message template- Returns:
- a checked object
- Throws:
NullPointerException- if an object is null- See Also:
-
checkState
public static void checkState(boolean expression) Checks a validity of a stateIf a given expression is
truethen a method finishes successfully. Otherwise, anIllegalStateExceptionis thrown- Parameters:
expression- a boolean that represents a validity of a state- Throws:
IllegalStateException- if a state is not valid
-
checkState
Checks a validity of a stateIf a given expression is
truethen a method finishes successfully. Otherwise, anIllegalStateExceptionis thrown with a specified message- Parameters:
expression- a boolean that represents a validity of a statemessage- an object that represents a message to be used in thrownIllegalStateException- Throws:
IllegalStateException- if a state is not valid
-
checkState
Checks a validity of a stateIf a given expression is
truethen a method finishes successfully. Otherwise, anIllegalStateExceptionis thrown with a supplied message- Parameters:
expression- a boolean that represents a validity of a statemessage- a supplier of message to be used in thrownIllegalStateException- Throws:
IllegalStateException- if a state is not valid
-
checkState
Checks a validity of a stateIf a given expression is
truethen a method finishes successfully. Otherwise, anIllegalStateExceptionis thrown with a supplied message- Parameters:
expression- a boolean that represents a validity of a statetemplate- a template of message to be used in thrownIllegalStateExceptionargs- arguments to an error message template- Throws:
IllegalStateException- if a state is not valid- See Also:
-
checkArgument
public static void checkArgument(boolean expression) Checks a validity of an argumentIf a given expression is
truethen a method finishes successfully. Otherwise, anIllegalArgumentExceptionis thrown- Parameters:
expression- a boolean that represents a validity of an argument- Throws:
IllegalArgumentException- if an argument is not valid
-
checkArgument
Checks a validity of an argumentIf a given expression is
truethen a method finishes successfully. Otherwise, anIllegalArgumentExceptionis thrown with a specified message- Parameters:
expression- a boolean that represents a validity of an argumentmessage- an object that represents a message to be used in thrownIllegalArgumentException- Throws:
IllegalArgumentException- if an argument is not valid
-
checkArgument
Checks a validity of an argumentIf a given expression is
truethen a method finishes successfully. Otherwise, anIllegalArgumentExceptionis thrown with a supplied message- Parameters:
expression- a boolean that represents a validity of an argumentmessage- a supplier of message to be used in thrownIllegalArgumentException- Throws:
IllegalArgumentException- if an argument is not valid
-
checkArgument
Checks a validity of an argumentIf a given expression is
truethen a method finishes successfully. Otherwise, anIllegalArgumentExceptionis thrown with a supplied message- Parameters:
expression- a boolean that represents a validity of an argumenttemplate- a template of message to be used in thrownIllegalArgumentExceptionargs- arguments to an error message template- Throws:
IllegalArgumentException- if an argument is not valid- See Also:
-
checkArgument
Checks a validity of a given argumentIf an argument is valid then an argument is returned as-is. Otherwise, an
IllegalArgumentExceptionis thrown- Parameters:
argument- an argument to be checked for validitypredicate- a predicate that checks an argument for validity- Throws:
IllegalArgumentException- if an argument is not valid
-
checkArgument
Checks a validity of a given argumentIf an argument is valid then an argument is returned as-is. Otherwise, an
IllegalArgumentExceptionis thrown with a specified message- Parameters:
argument- an argument to be checked for validitypredicate- a predicate that checks an argument for validitymessage- an object that represents a message to be used in thrownIllegalArgumentException- Throws:
IllegalArgumentException- if an argument is not valid
-
checkArgument
Checks a validity of a given argumentIf an argument is valid then an argument is returned as-is. Otherwise, an
IllegalArgumentExceptionis thrown with a supplied message- Parameters:
argument- an argument to be checked for validitypredicate- a predicate that checks an argument for validitymessage- a supplier of message to be used in thrownIllegalArgumentException- Throws:
IllegalArgumentException- if an argument is not valid
-
checkArgument
Checks a validity of a given argumentIf an argument is valid then an argument is returned as-is. Otherwise, an
IllegalArgumentExceptionis thrown with a supplied message- Parameters:
argument- an argument to be checked for validitypredicate- a predicate that checks an argument for validitymessage- a function that transforms an argument into an error message to be used in thrownIllegalArgumentException- Throws:
IllegalArgumentException- if an argument is not valid
-
checkArgument
public static <T> T checkArgument(T argument, Predicate<T> predicate, String template, Object... args) Checks a validity of a given argumentIf an argument is valid then an argument is returned as-is. Otherwise, an
IllegalArgumentExceptionis thrown with a supplied message- Parameters:
argument- an argument to be checked for validitypredicate- a predicate that checks an argument for validitytemplate- a template of message to be used in thrownIllegalArgumentExceptionargs- arguments to an error message template- Throws:
IllegalArgumentException- if an argument is not valid
-