Package org.apache.flink.util
Class Preconditions
- java.lang.Object
-
- org.apache.flink.util.Preconditions
-
@Internal public final class Preconditions extends Object
A collection of static utility methods to validate input.This class is modelled after Google Guava's Preconditions class, and partly takes code from that class. We add this code to the Flink code base in order to reduce external dependencies.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
checkArgument(boolean condition)
Checks the given boolean condition, and throws anIllegalArgumentException
if the condition is not met (evaluates tofalse
).static void
checkArgument(boolean condition, Object errorMessage)
Checks the given boolean condition, and throws anIllegalArgumentException
if the condition is not met (evaluates tofalse
).static void
checkArgument(boolean condition, String errorMessageTemplate, Object... errorMessageArgs)
Checks the given boolean condition, and throws anIllegalArgumentException
if the condition is not met (evaluates tofalse
).static void
checkCompletedNormally(CompletableFuture<?> future)
Ensures that future has completed normally.static void
checkElementIndex(int index, int size)
Ensures that the given index is valid for an array, list or string of the given size.static void
checkElementIndex(int index, int size, String errorMessage)
Ensures that the given index is valid for an array, list or string of the given size.static <T> T
checkNotNull(T reference)
Ensures that the given object reference is not null.static <T> T
checkNotNull(T reference, String errorMessage)
Ensures that the given object reference is not null.static <T> T
checkNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs)
Ensures that the given object reference is not null.static void
checkState(boolean condition)
Checks the given boolean condition, and throws anIllegalStateException
if the condition is not met (evaluates tofalse
).static void
checkState(boolean condition, Object errorMessage)
Checks the given boolean condition, and throws anIllegalStateException
if the condition is not met (evaluates tofalse
).static void
checkState(boolean condition, String errorMessageTemplate, Object... errorMessageArgs)
Checks the given boolean condition, and throws anIllegalStateException
if the condition is not met (evaluates tofalse
).
-
-
-
Method Detail
-
checkNotNull
public static <T> T checkNotNull(@Nullable T reference)
Ensures that the given object reference is not null. Upon violation, aNullPointerException
with no message is thrown.- Parameters:
reference
- The object reference- Returns:
- The object reference itself (generically typed).
- Throws:
NullPointerException
- Thrown, if the passed reference was null.
-
checkNotNull
public static <T> T checkNotNull(@Nullable T reference, @Nullable String errorMessage)
Ensures that the given object reference is not null. Upon violation, aNullPointerException
with the given message is thrown.- Parameters:
reference
- The object referenceerrorMessage
- The message for theNullPointerException
that is thrown if the check fails.- Returns:
- The object reference itself (generically typed).
- Throws:
NullPointerException
- Thrown, if the passed reference was null.
-
checkNotNull
public static <T> T checkNotNull(T reference, @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs)
Ensures that the given object reference is not null. Upon violation, aNullPointerException
with the given message is thrown.The error message is constructed from a template and an arguments array, after a similar fashion as
String.format(String, Object...)
, but supporting only%s
as a placeholder.- Parameters:
reference
- The object referenceerrorMessageTemplate
- The message template for theNullPointerException
that is thrown if the check fails. The template substitutes its%s
placeholders with the error message arguments.errorMessageArgs
- The arguments for the error message, to be inserted into the message template for the%s
placeholders.- Returns:
- The object reference itself (generically typed).
- Throws:
NullPointerException
- Thrown, if the passed reference was null.
-
checkArgument
public static void checkArgument(boolean condition)
Checks the given boolean condition, and throws anIllegalArgumentException
if the condition is not met (evaluates tofalse
).- Parameters:
condition
- The condition to check- Throws:
IllegalArgumentException
- Thrown, if the condition is violated.
-
checkArgument
public static void checkArgument(boolean condition, @Nullable Object errorMessage)
Checks the given boolean condition, and throws anIllegalArgumentException
if the condition is not met (evaluates tofalse
). The exception will have the given error message.- Parameters:
condition
- The condition to checkerrorMessage
- The message for theIllegalArgumentException
that is thrown if the check fails.- Throws:
IllegalArgumentException
- Thrown, if the condition is violated.
-
checkArgument
public static void checkArgument(boolean condition, @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs)
Checks the given boolean condition, and throws anIllegalArgumentException
if the condition is not met (evaluates tofalse
).- Parameters:
condition
- The condition to checkerrorMessageTemplate
- The message template for theIllegalArgumentException
that is thrown if the check fails. The template substitutes its%s
placeholders with the error message arguments.errorMessageArgs
- The arguments for the error message, to be inserted into the message template for the%s
placeholders.- Throws:
IllegalArgumentException
- Thrown, if the condition is violated.
-
checkState
public static void checkState(boolean condition)
Checks the given boolean condition, and throws anIllegalStateException
if the condition is not met (evaluates tofalse
).- Parameters:
condition
- The condition to check- Throws:
IllegalStateException
- Thrown, if the condition is violated.
-
checkState
public static void checkState(boolean condition, @Nullable Object errorMessage)
Checks the given boolean condition, and throws anIllegalStateException
if the condition is not met (evaluates tofalse
). The exception will have the given error message.- Parameters:
condition
- The condition to checkerrorMessage
- The message for theIllegalStateException
that is thrown if the check fails.- Throws:
IllegalStateException
- Thrown, if the condition is violated.
-
checkState
public static void checkState(boolean condition, @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs)
Checks the given boolean condition, and throws anIllegalStateException
if the condition is not met (evaluates tofalse
).- Parameters:
condition
- The condition to checkerrorMessageTemplate
- The message template for theIllegalStateException
that is thrown if the check fails. The template substitutes its%s
placeholders with the error message arguments.errorMessageArgs
- The arguments for the error message, to be inserted into the message template for the%s
placeholders.- Throws:
IllegalStateException
- Thrown, if the condition is violated.
-
checkElementIndex
public static void checkElementIndex(int index, int size)
Ensures that the given index is valid for an array, list or string of the given size.- Parameters:
index
- index to checksize
- size of the array, list or string- Throws:
IllegalArgumentException
- Thrown, if size is negative.IndexOutOfBoundsException
- Thrown, if the index negative or greater than or equal to size
-
checkElementIndex
public static void checkElementIndex(int index, int size, @Nullable String errorMessage)
Ensures that the given index is valid for an array, list or string of the given size.- Parameters:
index
- index to checksize
- size of the array, list or stringerrorMessage
- The message for theIndexOutOfBoundsException
that is thrown if the check fails.- Throws:
IllegalArgumentException
- Thrown, if size is negative.IndexOutOfBoundsException
- Thrown, if the index negative or greater than or equal to size
-
checkCompletedNormally
public static void checkCompletedNormally(CompletableFuture<?> future)
Ensures that future has completed normally.- Throws:
IllegalStateException
- Thrown, if future has not completed or it has completed exceptionally.
-
-