Package com.onec.rules
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 Summary
ConstructorsConstructorDescriptionBusinessRule(String name, String field, String message, BooleanSupplier condition) Creates an instance of aBusinessRulerecord class.BusinessRule(String name, String message, BooleanSupplier condition) A cross-field (form-level) rule — no specific field. -
Method Summary
Modifier and TypeMethodDescriptionReturns the value of theconditionrecord component.final booleanIndicates whether some other object is "equal to" this one.field()Returns the value of thefieldrecord component.final inthashCode()Returns a hash code value for this object.booleanholds()Evaluate the rule against the current entity state.message()Returns the value of themessagerecord component.name()Returns the value of thenamerecord component.static BusinessRuleonField(String field, String message, BooleanSupplier condition) A rule scoped to a single field — its message shows inline on that field.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
BusinessRule
A cross-field (form-level) rule — no specific field. -
BusinessRule
Creates an instance of aBusinessRulerecord class.- Parameters:
name- the value for thenamerecord componentfield- the value for thefieldrecord componentmessage- the value for themessagerecord componentcondition- the value for theconditionrecord component
-
-
Method Details
-
onField
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
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. -
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. -
equals
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 withObjects::equals(Object,Object). -
name
Returns the value of thenamerecord component.- Returns:
- the value of the
namerecord component
-
field
Returns the value of thefieldrecord component.- Returns:
- the value of the
fieldrecord component
-
message
Returns the value of themessagerecord component.- Returns:
- the value of the
messagerecord component
-
condition
Returns the value of theconditionrecord component.- Returns:
- the value of the
conditionrecord component
-