Package au.csiro.pathling.utilities
Class Preconditions
- java.lang.Object
-
- au.csiro.pathling.utilities.Preconditions
-
public abstract class Preconditions extends Object
Static convenience methods for checking the validity of inputs.- Author:
- John Grimes
-
-
Constructor Summary
Constructors Constructor Description Preconditions()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcheck(boolean expression)Ensures the truth of an expression, throwing anAssertionErrorwith the supplied message if it does not evaluate as true.static voidcheck(boolean expression, String messageTemplate, Object... params)Ensures the truth of an expression, throwing anAssertionErrorwith the supplied message if it does not evaluate as true.static voidcheckArgument(boolean expression, String errorMessage)Ensures the truth of an expression, throwing anIllegalArgumentExceptionwith the supplied message if it does not evaluate as true.static <T> TcheckNotNull(T object)Ensures that an object is not null, throwing aNullPointerExceptionif it is.static <T> TcheckPresent(Optional<T> object)Ensures that anOptionalvalue is present, throwing aAssertionErrorif it is not.static <T> TcheckPresent(Optional<T> object, String message)Ensures that anOptionalvalue is present, throwing aAssertionErrorif it is not.static voidcheckResponse(boolean expression, String messageTemplate, Object... params)Ensures the truth of an expression, throwing anUnexpectedResponseExceptionwith the supplied formatted message if it does not evaluate as true.static voidcheckState(boolean expression, String errorMessage)Ensures the truth of an expression, throwing anIllegalStateExceptionwith the supplied message if it does not evaluate as true.static voidcheckUserInput(boolean expression, String errorMessage)Ensures the truth of an expression, throwing anInvalidUserInputErrorwith the supplied message if it does not evaluate as true.static <T,R>
Function<T,R>wrapInUserInputError(Function<T,R> func)Converts a function which throws aFHIRExceptionto a function that throws anInvalidUserInputErrorin the same situation.
-
-
-
Method Detail
-
check
public static void check(boolean expression)
Ensures the truth of an expression, throwing anAssertionErrorwith the supplied message if it does not evaluate as true.- Parameters:
expression- the expression that should be true
-
check
public static void check(boolean expression, @Nonnull String messageTemplate, @Nonnull Object... params)Ensures the truth of an expression, throwing anAssertionErrorwith the supplied message if it does not evaluate as true.- Parameters:
expression- the expression that should be truemessageTemplate- the message template in theString.format(java.lang.String, java.lang.Object...)formatparams- the parameters to the message template
-
checkArgument
public static void checkArgument(boolean expression, @Nonnull String errorMessage)Ensures the truth of an expression, throwing anIllegalArgumentExceptionwith the supplied message if it does not evaluate as true.- Parameters:
expression- the expression that should be trueerrorMessage- the message to use if an error is thrown
-
checkNotNull
@Nonnull public static <T> T checkNotNull(@Nullable T object)Ensures that an object is not null, throwing aNullPointerExceptionif it is.- Type Parameters:
T- the type of the object- Parameters:
object- the object to check- Returns:
- the non-null object
-
checkPresent
@Nonnull public static <T> T checkPresent(@Nonnull Optional<T> object)Ensures that anOptionalvalue is present, throwing aAssertionErrorif it is not.- Type Parameters:
T- the type of the object within the Optional- Parameters:
object- the object to check- Returns:
- the unwrapped object
-
checkPresent
@Nonnull public static <T> T checkPresent(@Nonnull Optional<T> object, @Nullable String message)Ensures that anOptionalvalue is present, throwing aAssertionErrorif it is not.- Type Parameters:
T- the type of the object within the Optional- Parameters:
object- the object to checkmessage- an error message- Returns:
- the unwrapped object
-
checkUserInput
public static void checkUserInput(boolean expression, @Nonnull String errorMessage)Ensures the truth of an expression, throwing anInvalidUserInputErrorwith the supplied message if it does not evaluate as true.- Parameters:
expression- the expression that should be trueerrorMessage- the message to use if an error is thrown
-
checkState
public static void checkState(boolean expression, @Nonnull String errorMessage)Ensures the truth of an expression, throwing anIllegalStateExceptionwith the supplied message if it does not evaluate as true.- Parameters:
expression- the expression that should be trueerrorMessage- the message to use if an error is thrown
-
checkResponse
public static void checkResponse(boolean expression, @Nonnull String messageTemplate, @Nonnull Object... params)Ensures the truth of an expression, throwing anUnexpectedResponseExceptionwith the supplied formatted message if it does not evaluate as true.- Parameters:
expression- the expression that should be truemessageTemplate- the message template in theString.format(java.lang.String, java.lang.Object...)formatparams- the parameters to the message template
-
wrapInUserInputError
public static <T,R> Function<T,R> wrapInUserInputError(@Nonnull Function<T,R> func)
Converts a function which throws aFHIRExceptionto a function that throws anInvalidUserInputErrorin the same situation.- Type Parameters:
T- the type of the function argument.R- the type of the function result.- Parameters:
func- the function throwingFHIRException- Returns:
- the wrapped function.
-
-