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.
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 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) Deprecated.static <T> T
checkNotNull
(T object, String message) Deprecated.useObjects.requireNonNull(Object, String)
insteadstatic void
Alias forcheckNotBlank(String)
to be used in fluentReject.ifBlank
syntax.static void
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
Deprecated.Experience has shown thatReject.ifFalse
can be hard to read.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 thecondition
parameter is true.static void
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
Throws anIllegalArgumentException
with a custommessage
if thecondition
parameter is false.
-
Method Details
-
checkNotNull
Deprecated.useObjects.requireNonNull(Object)
} insteadThrows 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
Deprecated.useObjects.requireNonNull(Object, String)
insteadThrows 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
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
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
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
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.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.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
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
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
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 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
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
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
-
Objects.requireNonNull(Object)
} instead