Package org.forgerock.util
Class Reject
java.lang.Object
org.forgerock.util.Reject
A input parameter-validating utility class using fluent invocation:
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.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
checkNotNull
(T object) Throws aNullPointerException
if the object parameter is null, returns the object otherwise.static <T> T
checkNotNull
(T object, String message) Throws aNullPointerException
if the object parameter is null, returns the object otherwise.static void
ifFalse
(boolean condition) Throws anIllegalArgumentException
if the condition parameter is false.static void
Throws anIllegalArgumentException
with a custommessage
if the condition parameter is false.static void
Alias forcheckNotNull
to be used in fluentReject.ifNull
syntax.static void
Alias forcheckNotNull
to be used in fluentReject.ifNull
syntax.static <T> void
ifNull
(T... objects) Throws aNullPointerException
if any of the provided arguments arenull
.static void
ifTrue
(boolean condition) Throws anIllegalArgumentException
if the condition parameter is true.static void
Throws anIllegalArgumentException
with a custommessage
if the condition parameter is true.static void
rejectStateIfTrue
(boolean condition, String message) Throws anIllegalStateException
with a custommessage
if the condition parameter is true.
-
Method Details
-
checkNotNull
Throws aNullPointerException
if the object parameter is null, returns the object otherwise.- Type Parameters:
T
- The type of object to test.- Parameters:
object
- the object to test- Returns:
- the object
- Throws:
NullPointerException
- ifobject
is null
-
checkNotNull
Throws aNullPointerException
if the object parameter is null, returns the object otherwise.- Type Parameters:
T
- The type of object to test.- Parameters:
object
- the object to testmessage
- a custom exception message to use- Returns:
- the object
- Throws:
NullPointerException
- ifobject
is null
-
ifFalse
Throws anIllegalArgumentException
if the condition parameter is false.- Parameters:
condition
- the condition to test- Throws:
IllegalArgumentException
- ifcondition
is false
-
ifFalse
Throws anIllegalArgumentException
with a custommessage
if the condition parameter is false.- Parameters:
condition
- the condition to testmessage
- a custom exception message to use- Throws:
IllegalArgumentException
- ifcondition
is false
-
ifNull
Alias forcheckNotNull
to be used in fluentReject.ifNull
syntax. Throws aNullPointerException
if the object parameter is null.- Parameters:
object
- the object to test- Throws:
NullPointerException
- ifobject
is null
-
ifNull
Throws aNullPointerException
if any of the provided arguments arenull
.- Type Parameters:
T
- The type of object to test.- Parameters:
objects
- The objects to test.- Throws:
NullPointerException
- If any of the provided arguments arenull
.
-
ifNull
Alias forcheckNotNull
to be used in fluentReject.ifNull
syntax. Throws aNullPointerException
if the object parameter is null.- Parameters:
object
- the object to testmessage
- a custom exception message to use- Throws:
NullPointerException
- ifobject
is null
-
ifTrue
Throws anIllegalArgumentException
if the condition parameter is true.- Parameters:
condition
- the condition to test- Throws:
IllegalArgumentException
- ifcondition
is true
-
ifTrue
Throws anIllegalArgumentException
with a custommessage
if the condition parameter is true.- Parameters:
condition
- the condition to testmessage
- a custom exception message to use- Throws:
IllegalArgumentException
- ifcondition
is true
-
rejectStateIfTrue
Throws anIllegalStateException
with a custommessage
if the condition parameter is true.- Parameters:
condition
- the condition to testmessage
- a custom exception message to use- Throws:
IllegalStateException
- ifcondition
is true
-