Record Class BusinessRule

java.lang.Object
java.lang.Record
com.onec.rules.BusinessRule

public record BusinessRule(String name, String field, String message, BooleanSupplier condition) extends Record
A named validation rule expressed as typed Java — the replacement for the old @BusinessRule(expression = "...") string mini-language. The condition is a BooleanSupplier closing over the entity's fields, so it's checked by the compiler, refactorable, and debuggable; the name/message keep it enumerable for manifests and error reporting.
 new BusinessRule("gross-positive", "Gross must be positive",
         () -> gross != null && gross.signum() > 0)
 

field optionally names the form field this rule guards, so a failure surfaces as an inline error on that input rather than a form-level message. Omit it (or use the 3-arg form) for a cross-field rule, whose message attaches to the form as a whole.

  • Constructor Details

    • BusinessRule

      public BusinessRule(String name, String message, BooleanSupplier condition)
      A cross-field (form-level) rule — no specific field.
    • BusinessRule

      public BusinessRule(String name, String field, String message, BooleanSupplier condition)
      Creates an instance of a BusinessRule record class.
      Parameters:
      name - the value for the name record component
      field - the value for the field record component
      message - the value for the message record component
      condition - the value for the condition record component
  • Method Details

    • onField

      public static BusinessRule onField(String field, String message, BooleanSupplier condition)
      A rule scoped to a single field — its message shows inline on that field. The rule name defaults to the field name.
      BusinessRule.onField("client", "Client is required", () -> client != null)
    • holds

      public boolean holds()
      Evaluate the rule against the current entity state.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • name

      public String name()
      Returns the value of the name record component.
      Returns:
      the value of the name record component
    • field

      public String field()
      Returns the value of the field record component.
      Returns:
      the value of the field record component
    • message

      public String message()
      Returns the value of the message record component.
      Returns:
      the value of the message record component
    • condition

      public BooleanSupplier condition()
      Returns the value of the condition record component.
      Returns:
      the value of the condition record component