Package org.forgerock.util
Class Reject
- java.lang.Object
-
- org.forgerock.util.Reject
-
public final class Reject extends Object
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 anIllegalArgumentException
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, thecheckNotNull
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
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static String
checkNotBlank(String str)
Throws aNullPointerException
if thestr
parameter isnull
, throwsIllegalArgumentException
if empty or only contains whitespace, and returns the string otherwise.static String
checkNotBlank(String str, String message)
Throws aNullPointerException
if thestr
parameter isnull
, throwsIllegalArgumentException
if empty or only contains whitespace, and returns the string otherwise.static <T> T
checkNotNull(T object)
Throws aNullPointerException
if theobject
parameter is null, returns the object otherwise.static <T> T
checkNotNull(T object, String message)
Throws aNullPointerException
if theobject
parameter is null, returns the object otherwise.static void
ifBlank(String str)
Alias forcheckNotBlank(String)
to be used in fluentReject.ifBlank
syntax.static void
ifBlank(String str, String message)
Alias forcheckNotBlank(String, String)
to be used in fluentReject.ifBlank
syntax.static void
ifFalse(boolean condition)
Deprecated.Experience has shown thatReject.ifFalse
can be hard to read.static void
ifFalse(boolean condition, String message)
Deprecated.Experience has shown thatReject.ifFalse
can be hard to read.static void
ifNull(Object object)
Alias forcheckNotNull
to be used in fluentReject.ifNull
syntax.static void
ifNull(Object object, String message)
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 thecondition
parameter is true.static void
ifTrue(boolean condition, String message)
Throws anIllegalArgumentException
with a custommessage
if thecondition
parameter is true.static void
rejectStateIfTrue(boolean condition, String message)
Throws anIllegalStateException
with a custommessage
if thecondition
parameter is true.static void
unless(boolean condition)
Throws anIllegalArgumentException
if thecondition
parameter is false.static void
unless(boolean condition, String message)
Throws anIllegalArgumentException
with a custommessage
if thecondition
parameter is false.
-
-
-
Method Detail
-
checkNotNull
public static <T> T checkNotNull(T object)
Throws aNullPointerException
if theobject
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
public static <T> T checkNotNull(T object, String message)
Throws aNullPointerException
if theobject
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
-
checkNotBlank
public static String checkNotBlank(String str)
Throws aNullPointerException
if thestr
parameter isnull
, throwsIllegalArgumentException
if empty or only contains whitespace, and returns the string otherwise.- Parameters:
str
- the string to check- Returns:
- the string
- Throws:
NullPointerException
- ifstr
isnull
IllegalArgumentException
- ifstr
is empty or only contains whitespace
-
checkNotBlank
public static String checkNotBlank(String str, String message)
Throws aNullPointerException
if thestr
parameter isnull
, throwsIllegalArgumentException
if empty or only contains whitespace, and returns the string otherwise.- Parameters:
str
- the string to checkmessage
- a custom exception message to use- Returns:
- the string
- Throws:
NullPointerException
- ifstr
isnull
IllegalArgumentException
- ifstr
is empty or only contains whitespace
-
ifBlank
public static void ifBlank(String str)
Alias forcheckNotBlank(String)
to be used in fluentReject.ifBlank
syntax. Throws aNullPointerException
if thestr
parameter isnull
, throwsIllegalArgumentException
if empty or only contains whitespace, and returns the string otherwise.- Parameters:
str
- the string to check- Throws:
NullPointerException
- ifstr
isnull
IllegalArgumentException
- ifstr
is empty or only contains whitespace
-
ifBlank
public static void ifBlank(String str, String message)
Alias forcheckNotBlank(String, String)
to be used in fluentReject.ifBlank
syntax. Throws aNullPointerException
if thestr
parameter isnull
, throwsIllegalArgumentException
if empty or only contains whitespace, and returns the string otherwise.- Parameters:
str
- the string to checkmessage
- a custom exception message to use- Throws:
NullPointerException
- ifstr
isnull
IllegalArgumentException
- ifstr
is empty or only contains whitespace
-
ifFalse
@Deprecated public static void ifFalse(boolean condition)
Deprecated.Experience has shown thatReject.ifFalse
can be hard to read. Prefer to useunless(boolean)
(which works identically) or rewrite to useifTrue(boolean)
instead.Throws anIllegalArgumentException
if thecondition
parameter is false.- Parameters:
condition
- the condition to test- Throws:
IllegalArgumentException
- ifcondition
is false
-
ifFalse
@Deprecated public static void ifFalse(boolean condition, String message)
Deprecated.Experience has shown thatReject.ifFalse
can be hard to read. Prefer to useunless(boolean, String)
(which works identically) or rewrite to useifTrue(boolean, String)
instead.Throws anIllegalArgumentException
with a custommessage
if thecondition
parameter is false.- Parameters:
condition
- the condition to testmessage
- a custom exception message to use- Throws:
IllegalArgumentException
- ifcondition
is false
-
unless
public static void unless(boolean condition)
Throws anIllegalArgumentException
if thecondition
parameter is false.- Parameters:
condition
- the condition to test- Throws:
IllegalArgumentException
- ifcondition
is false
-
unless
public static void unless(boolean condition, String message)
Throws anIllegalArgumentException
with a custommessage
if thecondition
parameter is false.- Parameters:
condition
- the condition to testmessage
- a custom exception message to use- Throws:
IllegalArgumentException
- ifcondition
is false
-
ifNull
public static void ifNull(Object object)
Alias forcheckNotNull
to be used in fluentReject.ifNull
syntax. Throws aNullPointerException
if theobject
parameter is null.- Parameters:
object
- the object to test- Throws:
NullPointerException
- ifobject
is null
-
ifNull
@SafeVarargs public static <T> void ifNull(T... objects)
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
public static void ifNull(Object object, String message)
Alias forcheckNotNull
to be used in fluentReject.ifNull
syntax. Throws aNullPointerException
if theobject
parameter is null.- Parameters:
object
- the object to testmessage
- a custom exception message to use- Throws:
NullPointerException
- ifobject
is null
-
ifTrue
public static void ifTrue(boolean condition)
Throws anIllegalArgumentException
if thecondition
parameter is true.- Parameters:
condition
- the condition to test- Throws:
IllegalArgumentException
- ifcondition
is true
-
ifTrue
public static void ifTrue(boolean condition, String message)
Throws anIllegalArgumentException
with a custommessage
if thecondition
parameter is true.- Parameters:
condition
- the condition to testmessage
- a custom exception message to use- Throws:
IllegalArgumentException
- ifcondition
is true
-
rejectStateIfTrue
public static void rejectStateIfTrue(boolean condition, String message)
Throws anIllegalStateException
with a custommessage
if thecondition
parameter is true.- Parameters:
condition
- the condition to testmessage
- a custom exception message to use- Throws:
IllegalStateException
- ifcondition
is true
-
-