Interface Invariants


public interface Invariants
Utility interface providing static checks for domain invariants.

Invariants are business rules that must hold true throughout the lifetime of an aggregate. Use requireNull(Object, String) to assert that a field has not yet been assigned a value when a one-time, immutable assignment is required.

See Also:
  • Method Summary

    Static Methods
    Modifier and Type
    Method
    Description
    static <T> void
    requireNull(T object, String message)
    Asserts that object is null, throwing an IllegalStateException with the given message if it is not.
  • Method Details

    • requireNull

      static <T> void requireNull(T object, String message)
      Asserts that object is null, throwing an IllegalStateException with the given message if it is not.

      Typical use is to guard a field that must only be set once:

      
       Invariants.requireNull(this.publicId, "publicId must not change once set");
       this.publicId = publicId;
       
      Type Parameters:
      T - the type of the object being checked
      Parameters:
      object - the value to check; the assertion passes only when this is null
      message - the detail message for the exception if the assertion fails
      Throws:
      IllegalStateException - if object is not null