2 * SPDX-FileCopyrightText: The reguloj Authors
3 * SPDX-License-Identifier: 0BSD
5 package wtf
.metio
.reguloj
;
7 import java
.util
.function
.Consumer
;
8 import java
.util
.function
.Predicate
;
12 * A {@link Rule} combines {@link java.util.function.Predicate} and {@link java.util.function.Consumer} interfaces and
13 * can be evaluated with a {@link RuleEngine} using a {@link Context}.
19 * Check whether a rule would fire inside a given context:
23 * Context<X> context = ...;
24 * Rule<Context<X>> rule = ...;
26 * boolean canFire = rule.fires(context);
32 * Run a rule inside a given context:
35 * Context<X> context = ...;
36 * Rule<Context<X>> rule = ...;
43 * @param <CONTEXT> The context type.
48 public interface Rule
<CONTEXT
extends Context
<?
>> {
51 * Start building a new Rule by specifying a Predicate.
53 * @param <CONTEXT> The context type.
54 * @return A new builder to construct rules.
56 static <CONTEXT
extends Context
<?
>> RuleBuilder
<CONTEXT
> when(final Predicate
<CONTEXT
> predicate
) {
57 return new FluentRuleBuilder
<CONTEXT
>().when(predicate
);
61 * Create a new Rule that always fires/runs and calls the given Consumer.
63 * @param <CONTEXT> The context type.
64 * @return A new builder to construct rules.
66 static <CONTEXT
extends Context
<?
>> Rule
<CONTEXT
> always(final Consumer
<CONTEXT
> consumer
) {
67 return new FluentRuleBuilder
<CONTEXT
>().when(c
-> true).then(consumer
);
71 * Checks whether this rule would fire for a given context.
73 * @param context The context to check.
74 * @return <code>true</code> if this rule would fire, <code>false</code> otherwise.
76 boolean fires(CONTEXT context
);
79 * Runs this rule inside a given context. A rule should only run iff {@link #fires(Context)} returns
82 * @param context The context to use.
84 void run(CONTEXT context
);