Class AssertionUtils

java.lang.Object
org.apache.juneau.commons.utils.AssertionUtils

public class AssertionUtils extends Object
Utility methods for argument validation and assertion.

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:

import static org.apache.juneau.commons.utils.AssertionUtils.*; public void setFooBar(String foo, String bar) { // Validate multiple arguments at once assertArgsNotNull("foo", foo, "bar", bar); // Validate custom condition assertArg(foo.length() > 0, "Foo cannot be empty"); }

See Also:
  • Constructor Details

  • Method Details

    • assertArg

      public static final void assertArg(boolean expression, String msg, Object... args) throws IllegalArgumentException
      Throws an IllegalArgumentException if the specified expression is false.
      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 is true, throwing an IllegalStateException if it's not.

      This method is similar to assertArg(boolean, String, Object...) but throws an IllegalStateException instead of an IllegalArgumentException, 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 is false.
    • assertArgNotNull

      public static final <T> T assertArgNotNull(String name, T o) throws IllegalArgumentException
      Throws an IllegalArgumentException if the specified argument is null.
      Example:

      import static org.apache.juneau.commons.utils.AssertionUtils.*; public String setFoo(String foo) { 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 not null, throwing an IllegalStateException if it is.
      Type Parameters:
      T - The type of the object.
      Parameters:
      o - The object to check. Can be null.
      msg - The error message format string. Must not be null.
      args - The arguments for the message format string.
      Returns:
      The non-null object.
      Throws:
      IllegalStateException - If the object is null.
    • assertArgNotNullOrBlank

      public static final String assertArgNotNullOrBlank(String name, String o) throws IllegalArgumentException
      Throws an IllegalArgumentException if the specified string is null or blank.
      Parameters:
      name - The argument name.
      o - The object to check.
      Returns:
      The same object.
      Throws:
      IllegalArgumentException - Thrown if the specified string is null or blank.
    • assertArgsNotNull

      public static final void assertArgsNotNull(String name1, Object o1, String name2, Object o2) throws IllegalArgumentException
      Throws an IllegalArgumentException if any of the specified arguments are null.
      Example:

      import static org.apache.juneau.commons.utils.AssertionUtils.*; public String setFooBar(String foo, String bar) { 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 an IllegalArgumentException if any of the specified arguments are null.
      Example:

      import static org.apache.juneau.commons.utils.AssertionUtils.*; public String setFooBarBaz(String foo, String bar, String baz) { 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 an IllegalArgumentException if any of the specified arguments are null.
      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 an IllegalArgumentException if any of the specified arguments are null.
      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

      public static final <T> T assertType(Class<T> type, Object o) throws IllegalArgumentException
      Throws an IllegalArgumentException if the specified object is not an instance of the specified type.
      Example:

      import static org.apache.juneau.commons.utils.AssertionUtils.*; public void setValue(Object value) { // 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(Object value) { // 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 an IllegalArgumentException if 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

      @SafeVarargs public static final <T> T assertOneOf(T actual, T... expected)
      Throws an AssertionError if 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

      public static final <T> T[] assertArgNoNulls(String name, T[] o) throws IllegalArgumentException
      Throws an IllegalArgumentException if the specified varargs array or any of its elements are null.
      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 are null.
    • assertArgNoNulls

      public static final <T, C extends Collection<T>> C assertArgNoNulls(String name, C collection) throws IllegalArgumentException
      Throws an IllegalArgumentException if the specified collection or any of its elements are null.
      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 are null.