Class Assertion

java.lang.Object
io.vertigo.core.lang.Assertion

public final class Assertion extends Object
Design by Contract implementation through assertions. Provides a fluent API for runtime validation of program invariants. Features: - Null checks (isNotNull, isNull) - String validation (isNotBlank) - Boolean conditions (isTrue, isFalse) - Conditional assertions (when) - Formatted error messages with parameters Based on B.Meyer's Design by Contract principles from Eiffel language. Throws specific exceptions when assertions fail. Usage example: Assertion.check() .isNotNull(object, "Object {0} required", objectName) .isTrue(value > 0, "Positive value required");
Author:
pchretien, fconstantin
  • Method Details

    • check

      public static Assertion check()
      Creates a new assertion chain. Starting point for fluent assertion API.
      Returns:
      Assertion instance for chaining
    • when

      public Assertion when(boolean condition, Supplier<Assertion> assertionSupplier)
      Evaluates an assertion when a condition is met. Allows conditional validation without breaking the chain.
      Parameters:
      condition - Condition to evaluate
      assertionSupplier - Assertion to execute if condition is true
      Returns:
      Current assertion for chaining
    • isNotNull

      public Assertion isNotNull(Object o)
      Validates that an object is not null. Standard null check without custom message.
      Parameters:
      o - Object to check
      Returns:
      Current assertion for chaining
      Throws:
      NullPointerException - if object is null
    • isNotNull

      public Assertion isNotNull(Object o, String msg, Object... params)
      Validates that an object is not null with custom message. Supports message formatting with parameters.
      Parameters:
      o - Object to check
      msg - Error message template
      params - Message parameters
      Returns:
      Current assertion for chaining
      Throws:
      NullPointerException - if object is null
    • isNull

      public Assertion isNull(Object o)
      Validates that an object is null. Uses default error message.
      Parameters:
      o - Object to check
      Returns:
      Current assertion for chaining
      Throws:
      IllegalArgumentException - if object is not null
    • isNull

      public Assertion isNull(Object o, String msg, Object... params)
      Validates that an object is null with custom message. Supports message formatting with parameters.
      Parameters:
      o - Object to check
      msg - Error message template
      params - Message parameters
      Returns:
      Current assertion for chaining
      Throws:
      IllegalArgumentException - if object is not null
    • isTrue

      public Assertion isTrue(boolean test, String msg, Object... params)
      Validates that a condition is true. Supports message formatting with parameters.
      Parameters:
      test - Condition to evaluate
      msg - Error message template
      params - Message parameters
      Returns:
      Current assertion for chaining
      Throws:
      IllegalStateException - if condition is false
    • isFalse

      public Assertion isFalse(boolean test, String msg, Object... params)
      Validates that a condition is false. Supports message formatting with parameters.
      Parameters:
      test - Condition to evaluate
      msg - Error message template
      params - Message parameters
      Returns:
      Current assertion for chaining
      Throws:
      IllegalStateException - if condition is true
    • isNotBlank

      public Assertion isNotBlank(String str)
      Validates that a string is not blank. Uses default error message.
      Parameters:
      str - String to check
      Returns:
      Current assertion for chaining
      Throws:
      IllegalArgumentException - if string is blank
    • isNotBlank

      public Assertion isNotBlank(String str, String msg, Object... params)
      Validates that a string is not blank with custom message. Supports message formatting with parameters.
      Parameters:
      str - String to check
      msg - Error message template
      params - Message parameters
      Returns:
      Current assertion for chaining
      Throws:
      IllegalArgumentException - if string is blank
    • isValid

      public Assertion isValid(Supplier<Assertion> assertionSupplier)
      Validates a supplied assertion. Allows composition of multiple assertions.
      Parameters:
      assertionSupplier - Assertion to validate
      Returns:
      Current assertion for chaining
      Throws:
      Exception - from supplied assertion if validation fails