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 anIllegalArgumentExceptionto 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, thecheckNotNullsyntax 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 StringcheckNotBlank(String str)Throws aNullPointerExceptionif thestrparameter isnull, throwsIllegalArgumentExceptionif empty or only contains whitespace, and returns the string otherwise.static StringcheckNotBlank(String str, String message)Throws aNullPointerExceptionif thestrparameter isnull, throwsIllegalArgumentExceptionif empty or only contains whitespace, and returns the string otherwise.static <T> TcheckNotNull(T object)Throws aNullPointerExceptionif theobjectparameter is null, returns the object otherwise.static <T> TcheckNotNull(T object, String message)Throws aNullPointerExceptionif theobjectparameter is null, returns the object otherwise.static voidifBlank(String str)Alias forcheckNotBlank(String)to be used in fluentReject.ifBlanksyntax.static voidifBlank(String str, String message)Alias forcheckNotBlank(String, String)to be used in fluentReject.ifBlanksyntax.static voidifFalse(boolean condition)Deprecated.Experience has shown thatReject.ifFalsecan be hard to read.static voidifFalse(boolean condition, String message)Deprecated.Experience has shown thatReject.ifFalsecan be hard to read.static voidifNull(Object object)Alias forcheckNotNullto be used in fluentReject.ifNullsyntax.static voidifNull(Object object, String message)Alias 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 theconditionparameter is true.static voidifTrue(boolean condition, String message)Throws anIllegalArgumentExceptionwith a custommessageif theconditionparameter is true.static voidrejectStateIfTrue(boolean condition, String message)Throws anIllegalStateExceptionwith a custommessageif theconditionparameter is true.static voidunless(boolean condition)Throws anIllegalArgumentExceptionif theconditionparameter is false.static voidunless(boolean condition, String message)Throws anIllegalArgumentExceptionwith a custommessageif theconditionparameter is false.
-
-
-
Method Detail
-
checkNotNull
public static <T> T checkNotNull(T object)
Throws aNullPointerExceptionif theobjectparameter 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
public static <T> T checkNotNull(T object, String message)Throws aNullPointerExceptionif theobjectparameter 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
-
checkNotBlank
public static String checkNotBlank(String str)
Throws aNullPointerExceptionif thestrparameter isnull, throwsIllegalArgumentExceptionif empty or only contains whitespace, and returns the string otherwise.- Parameters:
str- the string to check- Returns:
- the string
- Throws:
NullPointerException- ifstrisnullIllegalArgumentException- ifstris empty or only contains whitespace
-
checkNotBlank
public static String checkNotBlank(String str, String message)
Throws aNullPointerExceptionif thestrparameter isnull, throwsIllegalArgumentExceptionif 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- ifstrisnullIllegalArgumentException- ifstris empty or only contains whitespace
-
ifBlank
public static void ifBlank(String str)
Alias forcheckNotBlank(String)to be used in fluentReject.ifBlanksyntax. Throws aNullPointerExceptionif thestrparameter isnull, throwsIllegalArgumentExceptionif empty or only contains whitespace, and returns the string otherwise.- Parameters:
str- the string to check- Throws:
NullPointerException- ifstrisnullIllegalArgumentException- ifstris empty or only contains whitespace
-
ifBlank
public static void ifBlank(String str, String message)
Alias forcheckNotBlank(String, String)to be used in fluentReject.ifBlanksyntax. Throws aNullPointerExceptionif thestrparameter isnull, throwsIllegalArgumentExceptionif empty or only contains whitespace, and returns the string otherwise.- Parameters:
str- the string to checkmessage- a custom exception message to use- Throws:
NullPointerException- ifstrisnullIllegalArgumentException- ifstris empty or only contains whitespace
-
ifFalse
@Deprecated public static void ifFalse(boolean condition)
Deprecated.Experience has shown thatReject.ifFalsecan be hard to read. Prefer to useunless(boolean)(which works identically) or rewrite to useifTrue(boolean)instead.Throws anIllegalArgumentExceptionif theconditionparameter is false.- Parameters:
condition- the condition to test- Throws:
IllegalArgumentException- ifconditionis false
-
ifFalse
@Deprecated public static void ifFalse(boolean condition, String message)
Deprecated.Experience has shown thatReject.ifFalsecan be hard to read. Prefer to useunless(boolean, String)(which works identically) or rewrite to useifTrue(boolean, String)instead.Throws anIllegalArgumentExceptionwith a custommessageif theconditionparameter is false.- Parameters:
condition- the condition to testmessage- a custom exception message to use- Throws:
IllegalArgumentException- ifconditionis false
-
unless
public static void unless(boolean condition)
Throws anIllegalArgumentExceptionif theconditionparameter is false.- Parameters:
condition- the condition to test- Throws:
IllegalArgumentException- ifconditionis false
-
unless
public static void unless(boolean condition, String message)Throws anIllegalArgumentExceptionwith a custommessageif theconditionparameter is false.- Parameters:
condition- the condition to testmessage- a custom exception message to use- Throws:
IllegalArgumentException- ifconditionis false
-
ifNull
public static void ifNull(Object object)
Alias forcheckNotNullto be used in fluentReject.ifNullsyntax. Throws aNullPointerExceptionif theobjectparameter is null.- Parameters:
object- the object to test- Throws:
NullPointerException- ifobjectis null
-
ifNull
@SafeVarargs public static <T> void ifNull(T... objects)
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
public static void ifNull(Object object, String message)
Alias forcheckNotNullto be used in fluentReject.ifNullsyntax. Throws aNullPointerExceptionif theobjectparameter is null.- Parameters:
object- the object to testmessage- a custom exception message to use- Throws:
NullPointerException- ifobjectis null
-
ifTrue
public static void ifTrue(boolean condition)
Throws anIllegalArgumentExceptionif theconditionparameter is true.- Parameters:
condition- the condition to test- Throws:
IllegalArgumentException- ifconditionis true
-
ifTrue
public static void ifTrue(boolean condition, String message)Throws anIllegalArgumentExceptionwith a custommessageif theconditionparameter is true.- Parameters:
condition- the condition to testmessage- a custom exception message to use- Throws:
IllegalArgumentException- ifconditionis true
-
rejectStateIfTrue
public static void rejectStateIfTrue(boolean condition, String message)Throws anIllegalStateExceptionwith a custommessageif theconditionparameter is true.- Parameters:
condition- the condition to testmessage- a custom exception message to use- Throws:
IllegalStateException- ifconditionis true
-
-