Class TokenRuleEngine

java.lang.Object
net.luis.utils.io.token.rule.TokenRuleEngine

public class TokenRuleEngine extends Object
A rule engine for processing tokens based on defined rules.
A rule can either validate or transform tokens.
The engine applies the rules in the order they were added.
If a rule matches, it replaces the matched tokens with the result of the action.
If no rule matches, the engine moves to the next token.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static final record 
    A record that holds a token rule and its associated action.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    List of rule actions to be applied to the tokens.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addRule(@NotNull TokenRule tokenRule)
    Adds a validation rule to the engine.
    The rule is only used to validate tokens and does not modify them.
    void
    addRule(@NotNull TokenRule tokenRule, @NotNull TokenAction action)
    Adds a transformation rule to the engine.
    The rule is used to transform tokens and can modify them.
    The rule is applied to the tokens and the result is used to replace the matched tokens.
    @NotNull @Unmodifiable List<Token>
    process(@NotNull List<Token> tokens)
    Processes the given list of tokens using the defined rules.
    The rules are applied in the order they were added.
    If a rule matches, the matched tokens are replaced with the result of the action.
    If no rule matches, the engine moves to the next token.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • TokenRuleEngine

      public TokenRuleEngine()
  • Method Details

    • addRule

      public void addRule(@NotNull @NotNull TokenRule tokenRule)
      Adds a validation rule to the engine.
      The rule is only used to validate tokens and does not modify them.
      Parameters:
      tokenRule - The rule to be added
      Throws:
      NullPointerException - If the rule is null
    • addRule

      public void addRule(@NotNull @NotNull TokenRule tokenRule, @NotNull @NotNull TokenAction action)
      Adds a transformation rule to the engine.
      The rule is used to transform tokens and can modify them.
      The rule is applied to the tokens and the result is used to replace the matched tokens.
      Parameters:
      tokenRule - The rule to be added
      action - The action to be applied to the matched tokens
      Throws:
      NullPointerException - If the rule or action is null
    • process

      @NotNull public @NotNull @Unmodifiable List<Token> process(@NotNull @NotNull List<Token> tokens)
      Processes the given list of tokens using the defined rules.
      The rules are applied in the order they were added.
      If a rule matches, the matched tokens are replaced with the result of the action.
      If no rule matches, the engine moves to the next token.
      Parameters:
      tokens - The list of tokens to be processed
      Returns:
      A new unmodifiable list of tokens after processing
      Throws:
      NullPointerException - If the list of tokens is null