Class Rule.RuleBuilder

java.lang.Object
io.github.brantunger.unruly.api.Rule.RuleBuilder
Enclosing class:
Rule

public static final class Rule.RuleBuilder extends Object
Builds a Rule. Get one from Rule.builder(), or from Rule.toBuilder() to copy a rule. The rule is enabled, with no validity window and no tags, until those are set; every other field is null until it is set, and build() checks that the required ones are.
  • Constructor Details

    • RuleBuilder

      public RuleBuilder()
      Creates a builder for an enabled rule with no validity window, no tags, and every other field null. In code, call Rule.builder(). This constructor is public so that a binder can create the builder itself, such as Jackson through @JsonDeserialize(builder = Rule.RuleBuilder.class), also on the module path, where a non-public constructor isn't accessible.
  • Method Details

    • ruleName

      public Rule.RuleBuilder ruleName(String ruleName)
      Sets the rule's name, used in error messages, exceptions and listener callbacks. Required, not blank, and unique within a rule list.
      Parameters:
      ruleName - The name
      Returns:
      This builder
    • condition

      public Rule.RuleBuilder condition(String condition)
      Sets the condition, which must evaluate to a boolean and can't assign or declare anything. Required.
      Parameters:
      condition - The condition
      Returns:
      This builder
    • action

      public Rule.RuleBuilder action(String action)
      Sets the action, run when the rule fires; it changes the output object, which a language may bind to a name, such as output in MVEL. Required.
      Parameters:
      action - The action
      Returns:
      This builder
    • priority

      public Rule.RuleBuilder priority(@Nullable Integer priority)
      Sets the rule's priority: higher values fire first, and null sorts last.
      Parameters:
      priority - The priority, or null
      Returns:
      This builder
    • description

      public Rule.RuleBuilder description(@Nullable String description)
      Sets the rule's description: free text for your own use, which the engine ignores but listeners receive.
      Parameters:
      description - The description, or null
      Returns:
      This builder
    • language

      public Rule.RuleBuilder language(@Nullable String language)
      Sets the name of the expression language the condition and action are written in.
      Parameters:
      language - The language's name, or null for the engine's default language
      Returns:
      This builder
    • enabled

      public Rule.RuleBuilder enabled(@Nullable Boolean enabled)
      Sets whether runs use the rule. A run skips a disabled rule, but the engine still compiles it when it loads the rules. null means the default, true, so rules read from data that holds null for an unset value, such as a nullable database column, stay enabled.
      Parameters:
      enabled - false to have runs skip the rule; true or null to have them use it
      Returns:
      This builder
    • validFrom

      public Rule.RuleBuilder validFrom(@Nullable Instant validFrom)
      Sets when runs start to use the rule. A run that starts before it skips the rule.
      Parameters:
      validFrom - The start, inclusive, or null for no start
      Returns:
      This builder
    • validTo

      public Rule.RuleBuilder validTo(@Nullable Instant validTo)
      Sets when runs stop using the rule. A run that starts at or after it skips the rule.
      Parameters:
      validTo - The end, exclusive, or null for no end
      Returns:
      This builder
    • tags

      public Rule.RuleBuilder tags(@Nullable Collection<String> tags)
      Sets the names that group the rule, replacing any set before. A run given tags uses the rule only if it carries at least one of them. A tag repeated in tags is kept once.
      Parameters:
      tags - The tags, none null or blank; copied. null means no tags, the default.
      Returns:
      This builder
    • build

      public Rule build()
      Creates a rule from the fields set so far. The builder can be used again afterwards. A blank condition or action is allowed here; RulesEngine.load(java.util.List) rejects it, naming the rule.
      Returns:
      A new rule
      Throws:
      IllegalStateException - if the name is null or blank, the condition or action is null, validTo isn't after validFrom, or a tag is null or blank. The message names the first such field, such as ruleName must not be null.
    • toString

      public String toString()
      Lists every field set so far, such as Rule.RuleBuilder(ruleName=prime-rate, condition=null, ...). The tags are listed as they were given, before build() sorts them and drops repeats.
      Overrides:
      toString in class Object
      Returns:
      The builder's fields as text