Record Class PatternTokenRule

java.lang.Object
java.lang.Record
net.luis.utils.io.token.rule.rules.PatternTokenRule
Record Components:
pattern - The pattern to match against
All Implemented Interfaces:
TokenRule

public record PatternTokenRule(@NotNull Pattern pattern) extends Record implements TokenRule
A token rule that matches the value of a single token against a regular expression pattern.
This rule is useful for creating complex matching logic by using regular expressions.
It will match the token if its value matches the pattern, otherwise it will return null.
  • Field Details

    • pattern

      @NotNull private final @NotNull Pattern pattern
      The field for the pattern record component.
  • Constructor Details

    • PatternTokenRule

      public PatternTokenRule(@Language("RegExp") @NotNull @NotNull String regex)
      Constructs a new pattern token rule from the given pattern in string format.
      Parameters:
      regex - The regex pattern to match against
      Throws:
      NullPointerException - If the regex pattern is null
    • PatternTokenRule

      public PatternTokenRule(@NotNull @NotNull Pattern pattern)
      Constructs a new pattern token rule from the given pattern.
      Parameters:
      pattern - The pattern to match against
      Throws:
      NullPointerException - If the pattern is null
  • Method Details

    • match

      @Nullable public @Nullable TokenRuleMatch match(@NotNull @NotNull List<Token> tokens, int startIndex)
      Description copied from interface: TokenRule
      Checks if the given tokens match this rule starting from the specified index.
      If the match is successful, a TokenRuleMatch is returned, otherwise null.
      Specified by:
      match in interface TokenRule
      Parameters:
      tokens - The list of tokens to match against
      startIndex - The index to start matching from
      Returns:
      A token rule match if successful, otherwise null
    • toString

      public final String 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.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • 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.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • pattern

      @NotNull public @NotNull Pattern pattern()
      Returns the value of the pattern record component.
      Returns:
      the value of the pattern record component