Package io.vertigo.core.lang
Class Assertion
java.lang.Object
io.vertigo.core.lang.Assertion
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 Summary
Modifier and TypeMethodDescriptionstatic Assertioncheck()Creates a new assertion chain.Validates that a condition is false.isNotBlank(String str) Validates that a string is not blank.isNotBlank(String str, String msg, Object... params) Validates that a string is not blank with custom message.Validates that an object is not null.Validates that an object is not null with custom message.Validates that an object is null.Validates that an object is null with custom message.Validates that a condition is true.Validates a supplied assertion.Evaluates an assertion when a condition is met.
-
Method Details
-
check
Creates a new assertion chain. Starting point for fluent assertion API.- Returns:
- Assertion instance for chaining
-
when
Evaluates an assertion when a condition is met. Allows conditional validation without breaking the chain.- Parameters:
condition- Condition to evaluateassertionSupplier- Assertion to execute if condition is true- Returns:
- Current assertion for chaining
-
isNotNull
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
Validates that an object is not null with custom message. Supports message formatting with parameters.- Parameters:
o- Object to checkmsg- Error message templateparams- Message parameters- Returns:
- Current assertion for chaining
- Throws:
NullPointerException- if object is null
-
isNull
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
Validates that an object is null with custom message. Supports message formatting with parameters.- Parameters:
o- Object to checkmsg- Error message templateparams- Message parameters- Returns:
- Current assertion for chaining
- Throws:
IllegalArgumentException- if object is not null
-
isTrue
Validates that a condition is true. Supports message formatting with parameters.- Parameters:
test- Condition to evaluatemsg- Error message templateparams- Message parameters- Returns:
- Current assertion for chaining
- Throws:
IllegalStateException- if condition is false
-
isFalse
Validates that a condition is false. Supports message formatting with parameters.- Parameters:
test- Condition to evaluatemsg- Error message templateparams- Message parameters- Returns:
- Current assertion for chaining
- Throws:
IllegalStateException- if condition is true
-
isNotBlank
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
Validates that a string is not blank with custom message. Supports message formatting with parameters.- Parameters:
str- String to checkmsg- Error message templateparams- Message parameters- Returns:
- Current assertion for chaining
- Throws:
IllegalArgumentException- if string is blank
-
isValid
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
-