Package io.github.brantunger.unruly.api
Class Rule.RuleBuilder
java.lang.Object
io.github.brantunger.unruly.api.Rule.RuleBuilder
- Enclosing class:
Rule
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 Summary
ConstructorsConstructorDescriptionCreates a builder for an enabled rule with no validity window, no tags, and every other fieldnull. -
Method Summary
Modifier and TypeMethodDescriptionSets the action, run when the rule fires; it changes the output object, which a language may bind to a name, such asoutputin MVEL.build()Creates a rule from the fields set so far.Sets the condition, which must evaluate to a boolean and can't assign or declare anything.description(@Nullable String description) Sets the rule's description: free text for your own use, which the engine ignores but listeners receive.Sets whether runs use the rule.Sets the name of the expression language the condition and action are written in.Sets the rule's priority: higher values fire first, andnullsorts last.Sets the rule's name, used in error messages, exceptions and listener callbacks.tags(@Nullable Collection<String> tags) Sets the names that group the rule, replacing any set before.toString()Lists every field set so far, such asRule.RuleBuilder(ruleName=prime-rate, condition=null, ...).Sets when runs start to use the rule.Sets when runs stop using the rule.
-
Constructor Details
-
RuleBuilder
public RuleBuilder()Creates a builder for an enabled rule with no validity window, no tags, and every other fieldnull. In code, callRule.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
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
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
Sets the action, run when the rule fires; it changes the output object, which a language may bind to a name, such asoutputin MVEL. Required.- Parameters:
action- The action- Returns:
- This builder
-
priority
Sets the rule's priority: higher values fire first, andnullsorts last.- Parameters:
priority- The priority, ornull- Returns:
- This builder
-
description
Sets the rule's description: free text for your own use, which the engine ignores but listeners receive.- Parameters:
description- The description, ornull- Returns:
- This builder
-
language
Sets the name of the expression language the condition and action are written in.- Parameters:
language- The language's name, ornullfor the engine's default language- Returns:
- This builder
-
enabled
Sets whether runs use the rule. A run skips a disabled rule, but the engine still compiles it when it loads the rules.nullmeans the default,true, so rules read from data that holdsnullfor an unset value, such as a nullable database column, stay enabled.- Parameters:
enabled-falseto have runs skip the rule;trueornullto have them use it- Returns:
- This builder
-
validFrom
Sets when runs start to use the rule. A run that starts before it skips the rule.- Parameters:
validFrom- The start, inclusive, ornullfor no start- Returns:
- This builder
-
validTo
Sets when runs stop using the rule. A run that starts at or after it skips the rule.- Parameters:
validTo- The end, exclusive, ornullfor no end- Returns:
- This builder
-
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 intagsis kept once.- Parameters:
tags- The tags, none null or blank; copied.nullmeans no tags, the default.- Returns:
- This builder
-
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 isnullor blank, the condition or action isnull,validToisn't aftervalidFrom, or a tag isnullor blank. The message names the first such field, such asruleName must not be null.
-
toString
Lists every field set so far, such asRule.RuleBuilder(ruleName=prime-rate, condition=null, ...). The tags are listed as they were given, beforebuild()sorts them and drops repeats.
-