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> TcheckNotNull(T object) Throws aNullPointerExceptionif the object parameter is null, returns the object otherwise.static <T> TcheckNotNull(T object, String message) Throws aNullPointerExceptionif the object parameter is null, returns the object otherwise.static voidifFalse(boolean condition) Throws anIllegalArgumentExceptionif the condition parameter is false.static voidThrows anIllegalArgumentExceptionwith a custommessageif the condition parameter is false.static voidAlias forcheckNotNullto be used in fluentReject.ifNullsyntax.static voidAlias forcheckNotNullto be used in fluentReject.ifNullsyntax.static <T> voidifNull(T... objects) Throws aNullPointerExceptionif any of the provided arguments arenull.static voidifTrue(boolean condition) Throws anIllegalArgumentExceptionif the condition parameter is true.static voidThrows anIllegalArgumentExceptionwith a custommessageif the condition parameter is true.static voidrejectStateIfTrue(boolean condition, String message) Throws anIllegalStateExceptionwith a custommessageif the condition parameter is true.
-
Method Details
-
checkNotNull
Throws aNullPointerExceptionif 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- ifobjectis null
-
checkNotNull
Throws aNullPointerExceptionif 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- ifobjectis null
-
ifFalse
Throws anIllegalArgumentExceptionif the condition parameter is false.- Parameters:
condition- the condition to test- Throws:
IllegalArgumentException- ifconditionis false
-
ifFalse
Throws anIllegalArgumentExceptionwith a custommessageif the condition parameter is false.- Parameters:
condition- the condition to testmessage- a custom exception message to use- Throws:
IllegalArgumentException- ifconditionis false
-
ifNull
Alias forcheckNotNullto be used in fluentReject.ifNullsyntax. Throws aNullPointerExceptionif the object parameter is null.- Parameters:
object- the object to test- Throws:
NullPointerException- ifobjectis null
-
ifNull
Throws aNullPointerExceptionif 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 forcheckNotNullto be used in fluentReject.ifNullsyntax. Throws aNullPointerExceptionif the object parameter is null.- Parameters:
object- the object to testmessage- a custom exception message to use- Throws:
NullPointerException- ifobjectis null
-
ifTrue
Throws anIllegalArgumentExceptionif the condition parameter is true.- Parameters:
condition- the condition to test- Throws:
IllegalArgumentException- ifconditionis true
-
ifTrue
Throws anIllegalArgumentExceptionwith a custommessageif the condition parameter is true.- Parameters:
condition- the condition to testmessage- a custom exception message to use- Throws:
IllegalArgumentException- ifconditionis true
-
rejectStateIfTrue
Throws anIllegalStateExceptionwith a custommessageif the condition parameter is true.- Parameters:
condition- the condition to testmessage- a custom exception message to use- Throws:
IllegalStateException- ifconditionis true
-