Class Assert

java.lang.Object
org.axonframework.common.Assert

public final class Assert extends Object
Utility class (inspired by Springs Assert class) for doing assertions on parameters and object state.

To remove the need for explicit dependencies on Spring, the functionality of that class is migrated to this class.

Since:
0.3
Author:
Allard Buijze
  • Method Details

    • state

      public static void state(boolean state, Supplier<String> messageSupplier)
      Asserts that the value of state is true. If not, an IllegalStateException is thrown.
      Parameters:
      state - The state validation expression.
      messageSupplier - Supplier of the exception message if state evaluates to false.
    • isTrue

      public static void isTrue(boolean expression, Supplier<String> messageSupplier)
      Asserts that the given expression is true. If not, an IllegalArgumentException is thrown.
      Parameters:
      expression - The state validation expression.
      messageSupplier - Supplier of the exception message if the expression evaluates to false.
    • isFalse

      public static void isFalse(boolean expression, Supplier<String> messageSupplier)
      Asserts that the given expression is false. If not, an IllegalArgumentException is thrown.
      Parameters:
      expression - The state validation expression.
      messageSupplier - Supplier of the exception message if the expression evaluates to true.
    • notNull

      public static void notNull(Object value, Supplier<String> messageSupplier)
      Assert that the given value is not null. If not, an IllegalArgumentException is thrown.
      Parameters:
      value - The value not to be null.
      messageSupplier - Supplier of the exception message if the assertion fails.
    • nonNull

      public static <T> T nonNull(T value, Supplier<String> messageSupplier)
      Assert that the given value is not null. If not, an IllegalArgumentException is thrown.
      Parameters:
      value - The value not to be null.
      messageSupplier - Supplier of the exception message if the assertion fails.
      Returns:
      The provided value.
    • assertThat

      public static <T, X extends Throwable> void assertThat(T value, Predicate<T> assertion, Supplier<? extends X> exceptionSupplier) throws X
      Assert that the given value will result to true through the assertion Predicate. If not, the exceptionSupplier provides an exception to be thrown.
      Type Parameters:
      T - A generic specifying the type of the value, which is the input for the assertion.
      X - A generic extending Throwable which will be provided by the exceptionSupplier.
      Parameters:
      value - A T specifying the value to assert.
      assertion - A Predicate to test value against.
      exceptionSupplier - A Supplier of the exception X if assertion evaluates to false.
      Throws:
      X - If the value asserts to false by the assertion.
    • assertNonNull

      public static <T, X extends Throwable> void assertNonNull(T value, Supplier<? extends X> exceptionSupplier) throws X
      Assert that the given value is non-null. If not, the exceptionSupplier provides an exception to be thrown.
      Type Parameters:
      T - A generic specifying the type of the value, which is the input for the assertion.
      X - A generic extending Throwable which will be provided by the exceptionSupplier.
      Parameters:
      value - A T specifying the value to assert.
      exceptionSupplier - A Supplier of the exception X if value equals null.
      Throws:
      X - If the value equals null.
    • nonEmpty

      public static String nonEmpty(String string, String exceptionMessage)
      Assert that the given string is not null and does not equal an empty String.

      If not, an IllegalArgumentException is thrown containing the provided exceptionMessage.

      Parameters:
      string - The value to assert.
      exceptionMessage - The message for the exception.
      Returns:
      The given string when it was not null or empty.
    • assertStrictPositive

      public static void assertStrictPositive(int i, String exceptionMessage)
      Assert that the given value is strictly positive, meaning greater than zero.

      If not, an IllegalArgumentException is thrown containing the provided {code exceptionMessage}.

      Parameters:
      i - The value to assert to be strictly positive.
      exceptionMessage - The message for the exception.
    • assertStrictPositive

      public static void assertStrictPositive(long i, String exceptionMessage)
      Assert that the given value is strictly positive, meaning greater than zero.

      If not, an IllegalArgumentException is thrown containing the provided {code exceptionMessage}.

      Parameters:
      i - The value to assert to be strictly positive.
      exceptionMessage - The message for the exception.