Class AssertionUtils
This class provides static utility methods for validating method arguments and throwing
IllegalArgumentException when validation fails. These methods are designed to
simplify common parameter validation patterns and provide consistent error messages.
Features:
- Null checking: Validate single or multiple arguments are not null
- Boolean expressions: Assert arbitrary boolean conditions with custom messages
- String validation: Check for null or blank strings
- Type checking: Validate class array types
Usage:
See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final voidThrows anIllegalArgumentExceptionif the specified expression isfalse .static final <T,C extends Collection<T>>
CassertArgNoNulls(String name, C collection) Throws anIllegalArgumentExceptionif the specified collection or any of its elements arenull .static final <T> T[]assertArgNoNulls(String name, T[] o) Throws anIllegalArgumentExceptionif the specified varargs array or any of its elements arenull .static final <T> TassertArgNotNull(String name, T o) Throws anIllegalArgumentExceptionif the specified argument isnull .static final StringassertArgNotNullOrBlank(String name, String o) Throws anIllegalArgumentExceptionif the specified string isnull or blank.static final voidassertArgsNotNull(String name1, Object o1, String name2, Object o2) Throws anIllegalArgumentExceptionif any of the specified arguments arenull .static final voidThrows anIllegalArgumentExceptionif any of the specified arguments arenull .static final voidassertArgsNotNull(String name1, Object o1, String name2, Object o2, String name3, Object o3, String name4, Object o4) Throws anIllegalArgumentExceptionif any of the specified arguments arenull .static final voidassertArgsNotNull(String name1, Object o1, String name2, Object o2, String name3, Object o3, String name4, Object o4, String name5, Object o5) Throws anIllegalArgumentExceptionif any of the specified arguments arenull .static final <E> Class<E>[]assertClassArrayArgIsType(String name, Class<E> type, Class<?>[] value) Throws anIllegalArgumentExceptionif the specified value doesn't have all subclasses of the specified type.static final <T> TassertNotNull(T o, String msg, Object... args) Asserts that the specified object is notnull , throwing anIllegalStateExceptionif it is.static final <T> TassertOneOf(T actual, T... expected) Throws anAssertionErrorif the specified actual value is not one of the expected values.static final voidassertState(boolean expression, String msg, Object... args) Asserts that the given expression istrue , throwing anIllegalStateExceptionif it's not.static final <T> TassertType(Class<T> type, Object o) Throws anIllegalArgumentExceptionif the specified object is not an instance of the specified type.static final <T> TassertType(Class<T> type, Object o, Supplier<? extends RuntimeException> exceptionSupplier) Throws the exception provided by the supplier if the specified object is not an instance of the specified type.
-
Constructor Details
-
AssertionUtils
public AssertionUtils()
-
-
Method Details
-
assertArg
public static final void assertArg(boolean expression, String msg, Object... args) throws IllegalArgumentException Throws anIllegalArgumentExceptionif the specified expression isfalse .Example:
import static org.apache.juneau.commons.utils.AssertionUtils.*;public String setFoo(List<String>foo ) {assertArg (foo !=null && !foo .isEmpty(),"'foo' cannot be null or empty." ); ... }- Parameters:
expression- The boolean expression to check.msg- The exception message.args- The exception message args.- Throws:
IllegalArgumentException- Constructed exception.
-
assertState
public static final void assertState(boolean expression, String msg, Object... args) throws IllegalStateException Asserts that the given expression istrue , throwing anIllegalStateExceptionif it's not.This method is similar to
assertArg(boolean, String, Object...)but throws anIllegalStateExceptioninstead of anIllegalArgumentException, making it suitable for state validation rather than argument validation.- Parameters:
expression- The expression to test.msg- The error message format string.args- The arguments for the error message format string.- Throws:
IllegalStateException- if the expression isfalse .
-
assertArgNotNull
Throws anIllegalArgumentExceptionif the specified argument isnull .Example:
import static org.apache.juneau.commons.utils.AssertionUtils.*;public String setFoo(Stringfoo ) {assertArgNotNull ("foo" ,foo ); ... }- Type Parameters:
T- The argument data type.- Parameters:
name- The argument name.o- The object to check.- Returns:
- The same argument.
- Throws:
IllegalArgumentException- Constructed exception.
-
assertNotNull
public static final <T> T assertNotNull(T o, String msg, Object... args) throws IllegalStateException Asserts that the specified object is notnull , throwing anIllegalStateExceptionif it is.- Type Parameters:
T- The type of the object.- Parameters:
o- The object to check. Can benull .msg- The error message format string. Must not benull .args- The arguments for the message format string.- Returns:
- The non-null object.
- Throws:
IllegalStateException- If the object isnull .
-
assertArgNotNullOrBlank
public static final String assertArgNotNullOrBlank(String name, String o) throws IllegalArgumentException Throws anIllegalArgumentExceptionif the specified string isnull or blank.- Parameters:
name- The argument name.o- The object to check.- Returns:
- The same object.
- Throws:
IllegalArgumentException- Thrown if the specified string isnull or blank.
-
assertArgsNotNull
public static final void assertArgsNotNull(String name1, Object o1, String name2, Object o2) throws IllegalArgumentException Throws anIllegalArgumentExceptionif any of the specified arguments arenull .Example:
import static org.apache.juneau.commons.utils.AssertionUtils.*;public String setFooBar(Stringfoo , Stringbar ) {assertArgsNotNull ("foo" ,foo ,"bar" ,bar ); ... }- Parameters:
name1- The first argument name.o1- The first object to check.name2- The second argument name.o2- The second object to check.- Throws:
IllegalArgumentException- Constructed exception.
-
assertArgsNotNull
public static final void assertArgsNotNull(String name1, Object o1, String name2, Object o2, String name3, Object o3) throws IllegalArgumentException Throws anIllegalArgumentExceptionif any of the specified arguments arenull .Example:
import static org.apache.juneau.commons.utils.AssertionUtils.*;public String setFooBarBaz(Stringfoo , Stringbar , Stringbaz ) {assertArgsNotNull ("foo" ,foo ,"bar" ,bar ,"baz" ,baz ); ... }- Parameters:
name1- The first argument name.o1- The first object to check.name2- The second argument name.o2- The second object to check.name3- The third argument name.o3- The third object to check.- Throws:
IllegalArgumentException- Constructed exception.
-
assertArgsNotNull
public static final void assertArgsNotNull(String name1, Object o1, String name2, Object o2, String name3, Object o3, String name4, Object o4) throws IllegalArgumentException Throws anIllegalArgumentExceptionif any of the specified arguments arenull .- Parameters:
name1- The first argument name.o1- The first object to check.name2- The second argument name.o2- The second object to check.name3- The third argument name.o3- The third object to check.name4- The fourth argument name.o4- The fourth object to check.- Throws:
IllegalArgumentException- Constructed exception.
-
assertArgsNotNull
public static final void assertArgsNotNull(String name1, Object o1, String name2, Object o2, String name3, Object o3, String name4, Object o4, String name5, Object o5) throws IllegalArgumentException Throws anIllegalArgumentExceptionif any of the specified arguments arenull .- Parameters:
name1- The first argument name.o1- The first object to check.name2- The second argument name.o2- The second object to check.name3- The third argument name.o3- The third object to check.name4- The fourth argument name.o4- The fourth object to check.name5- The fifth argument name.o5- The fifth object to check.- Throws:
IllegalArgumentException- Constructed exception.
-
assertType
Throws anIllegalArgumentExceptionif the specified object is not an instance of the specified type.Example:
import static org.apache.juneau.commons.utils.AssertionUtils.*;public void setValue(Objectvalue ) {// Validate that value is a String assertType (String.class ,value ); ... }- Type Parameters:
T- The expected type.- Parameters:
type- The expected class type.o- The object to check.- Returns:
- The object cast to the specified type.
- Throws:
IllegalArgumentException- Thrown if the object is not an instance of the specified type.
-
assertType
public static final <T> T assertType(Class<T> type, Object o, Supplier<? extends RuntimeException> exceptionSupplier) throws RuntimeException Throws the exception provided by the supplier if the specified object is not an instance of the specified type.Example:
import static org.apache.juneau.commons.utils.AssertionUtils.*;public void setValue(Objectvalue ) {// Validate that value is a String, throw custom exception assertType (String.class ,value , () ->new IllegalStateException("Invalid value type" )); ... }- Type Parameters:
T- The expected type.- Parameters:
type- The expected class type.o- The object to check.exceptionSupplier- The supplier that provides the exception to throw if validation fails.- Returns:
- The object cast to the specified type.
- Throws:
RuntimeException- Thrown if the object is not an instance of the specified type (the exception is provided by the supplier).
-
assertClassArrayArgIsType
public static final <E> Class<E>[] assertClassArrayArgIsType(String name, Class<E> type, Class<?>[] value) throws IllegalArgumentException Throws anIllegalArgumentExceptionif the specified value doesn't have all subclasses of the specified type.- Type Parameters:
E- The element type.- Parameters:
name- The argument name.type- The expected parent class.value- The array value being checked.- Returns:
- The value cast to the specified array type.
- Throws:
IllegalArgumentException- Constructed exception.
-
assertOneOf
Throws anAssertionErrorif the specified actual value is not one of the expected values.- Type Parameters:
T- The value type.- Parameters:
actual- The actual value.expected- The expected values.- Returns:
- The actual value if it matches one of the expected values.
- Throws:
AssertionError- if the value is not one of the expected values.
-
assertArgNoNulls
Throws anIllegalArgumentExceptionif the specified varargs array or any of its elements arenull .- Type Parameters:
T- The element type.- Parameters:
name- The argument name.o- The object to check.- Returns:
- The same object.
- Throws:
IllegalArgumentException- Thrown if the specified varargs array or any of its elements arenull .
-
assertArgNoNulls
public static final <T,C extends Collection<T>> C assertArgNoNulls(String name, C collection) throws IllegalArgumentException Throws anIllegalArgumentExceptionif the specified collection or any of its elements arenull .Example:
import static org.apache.juneau.commons.utils.AssertionUtils.*;public void setValues(List<String>values ) {assertCollectionArgNotNull ("values" ,values ); ... }- Type Parameters:
T- The element type.C- The collection type.- Parameters:
name- The argument name.collection- The collection to check.- Returns:
- The same collection.
- Throws:
IllegalArgumentException- Thrown if the specified collection or any of its elements arenull .
-