public final class Reject extends Object
public int divide(int dividend, int divisor) { Reject.ifTrue(divisor == 0, "Division by zero not supported"); return dividend / divisor; }The example above will cause an
IllegalArgumentException
to be thrown
with the message given.
Another use case is validating constructor parameters:
public TokenManager(final TokenFactory factory) { Reject.ifNull(factory, "Cannot instantiate TokenManager with null TokenFactory"); }Sometimes, constructor parameters are passed to ancestor constructors which must be called first--thus, the
checkNotNull
syntax is available:
import static org.forgerock.util.Reject.checkNotNull; public TokenManager(final TokenFactory factory) { super(checkNotNull(factory)); }Note that the methods herein throw generic RuntimeExceptions as opposed to custom, application-specific error Exceptions. This class is intended for wide use among multiple projects whose Exception frameworks may differ. The implementer is encouraged to catch the generic exceptions thrown by this class and rethrow exceptions appropriate to the target application.
Modifier and Type | Method and Description |
---|---|
static String |
checkNotBlank(String str)
Throws a
NullPointerException if the str parameter is null , throws
IllegalArgumentException if empty or only contains whitespace, and returns the string otherwise. |
static String |
checkNotBlank(String str,
String message)
Throws a
NullPointerException if the str parameter is null , throws
IllegalArgumentException if empty or only contains whitespace, and returns the string otherwise. |
static <T> T |
checkNotNull(T object)
Throws a
NullPointerException if the object parameter is
null, returns the object otherwise. |
static <T> T |
checkNotNull(T object,
String message)
Throws a
NullPointerException if the object parameter is
null, returns the object otherwise. |
static void |
ifBlank(String str)
Alias for
Reject.checkNotBlank(String) to be used in fluent Reject.ifBlank
syntax. |
static void |
ifBlank(String str,
String message)
Alias for
Reject.checkNotBlank(String, String) to be used in fluent Reject.ifBlank
syntax. |
static void |
ifFalse(boolean condition)
Throws an
IllegalArgumentException if the condition
parameter is false. |
static void |
ifFalse(boolean condition,
String message)
Throws an
IllegalArgumentException with a custom message
if the condition parameter is false. |
static void |
ifNull(Object object)
Alias for
checkNotNull to be used in fluent Reject.ifNull
syntax. |
static void |
ifNull(Object object,
String message)
Alias for
checkNotNull to be used in fluent Reject.ifNull
syntax. |
static <T> void |
ifNull(T... objects)
Throws a
NullPointerException if any of the provided arguments
are null . |
static void |
ifTrue(boolean condition)
Throws an
IllegalArgumentException if the condition
parameter is true. |
static void |
ifTrue(boolean condition,
String message)
Throws an
IllegalArgumentException with a custom message
if the condition parameter is true. |
static void |
rejectStateIfTrue(boolean condition,
String message)
Throws an
IllegalStateException with a custom message
if the condition parameter is true. |
public static <T> T checkNotNull(T object)
NullPointerException
if the object parameter is
null, returns the object otherwise.T
- The type of object to test.object
- the object to testNullPointerException
- if object
is nullpublic static <T> T checkNotNull(T object, String message)
NullPointerException
if the object parameter is
null, returns the object otherwise.T
- The type of object to test.object
- the object to testmessage
- a custom exception message to useNullPointerException
- if object
is nullpublic static String checkNotBlank(String str)
NullPointerException
if the str
parameter is null
, throws
IllegalArgumentException
if empty or only contains whitespace, and returns the string otherwise.str
- the string to checkNullPointerException
- if str
is null
IllegalArgumentException
- if str
is empty or only contains whitespacepublic static String checkNotBlank(String str, String message)
NullPointerException
if the str
parameter is null
, throws
IllegalArgumentException
if empty or only contains whitespace, and returns the string otherwise.str
- the string to checkmessage
- a custom exception message to useNullPointerException
- if str
is null
IllegalArgumentException
- if str
is empty or only contains whitespacepublic static void ifBlank(String str)
Reject.checkNotBlank(String)
to be used in fluent Reject.ifBlank
syntax. Throws a NullPointerException
if the str
parameter is null
, throws
IllegalArgumentException
if empty or only contains whitespace, and returns the string otherwise.str
- the string to checkNullPointerException
- if str
is null
IllegalArgumentException
- if str
is empty or only contains whitespacepublic static void ifBlank(String str, String message)
Reject.checkNotBlank(String, String)
to be used in fluent Reject.ifBlank
syntax. Throws a NullPointerException
if the str
parameter is null
, throws
IllegalArgumentException
if empty or only contains whitespace, and returns the string otherwise.str
- the string to checkmessage
- a custom exception message to useNullPointerException
- if str
is null
IllegalArgumentException
- if str
is empty or only contains whitespacepublic static void ifFalse(boolean condition)
IllegalArgumentException
if the condition
parameter is false.condition
- the condition to testIllegalArgumentException
- if condition
is falsepublic static void ifFalse(boolean condition, String message)
IllegalArgumentException
with a custom message
if the condition parameter is false.condition
- the condition to testmessage
- a custom exception message to useIllegalArgumentException
- if condition
is falsepublic static void ifNull(Object object)
checkNotNull
to be used in fluent Reject.ifNull
syntax. Throws a NullPointerException
if the object
parameter is null.object
- the object to testNullPointerException
- if object
is null@SafeVarargs public static <T> void ifNull(T... objects)
NullPointerException
if any of the provided arguments
are null
.T
- The type of object to test.objects
- The objects to test.NullPointerException
- If any of the provided arguments are null
.public static void ifNull(Object object, String message)
checkNotNull
to be used in fluent Reject.ifNull
syntax. Throws a NullPointerException
if the object
parameter is null.object
- the object to testmessage
- a custom exception message to useNullPointerException
- if object
is nullpublic static void ifTrue(boolean condition)
IllegalArgumentException
if the condition
parameter is true.condition
- the condition to testIllegalArgumentException
- if condition
is truepublic static void ifTrue(boolean condition, String message)
IllegalArgumentException
with a custom message
if the condition parameter is true.condition
- the condition to testmessage
- a custom exception message to useIllegalArgumentException
- if condition
is truepublic static void rejectStateIfTrue(boolean condition, String message)
IllegalStateException
with a custom message
if the condition parameter is true.condition
- the condition to testmessage
- a custom exception message to useIllegalStateException
- if condition
is trueCopyright © 2010-2018, ForgeRock All Rights Reserved.