Record Class RepeatedTokenRule

java.lang.Object
java.lang.Record
net.luis.utils.io.token.rule.rules.RepeatedTokenRule
Record Components:
tokenRule - The token rule to match
minOccurrences - The minimum number of occurrences
maxOccurrences - The maximum number of occurrences
All Implemented Interfaces:
TokenRule

public record RepeatedTokenRule(@NotNull TokenRule tokenRule, int minOccurrences, int maxOccurrences) extends Record implements TokenRule
A token rule that matches a token rule a number of times.
This rule is useful for creating complex matching logic by repeating a rule.
It will match the token rule a number of times between the min and max occurrences.
If the min and max occurrences are the same, it will match exactly that number of times.
API Note:
This class does not allow the creation of a optional token rule by setting the min and max occurrences to 0.
This is because the implementation of match(List, int) will return null if there is no match.
Use OptionalTokenRule for that purpose.
  • Field Details

    • tokenRule

      @NotNull private final @NotNull TokenRule tokenRule
      The field for the tokenRule record component.
    • minOccurrences

      private final int minOccurrences
      The field for the minOccurrences record component.
    • maxOccurrences

      private final int maxOccurrences
      The field for the maxOccurrences record component.
  • Constructor Details

    • RepeatedTokenRule

      public RepeatedTokenRule(@NotNull @NotNull TokenRule tokenRule, int occurrences)
      Constructs a new repeated token rule with the given token rule and exact number of occurrences.
      Parameters:
      tokenRule - The token rule to match
      occurrences - The exact number of occurrences
      Throws:
      NullPointerException - If the token rule is null
      IllegalArgumentException - If the number of occurrences is lower than 0
    • RepeatedTokenRule

      public RepeatedTokenRule(@NotNull @NotNull TokenRule tokenRule, int minOccurrences, int maxOccurrences)
      Constructs a new repeated token rule with the given token rule and min and max number of occurrences.
      Parameters:
      tokenRule - The token rule to match
      minOccurrences - The minimum number of occurrences
      maxOccurrences - The maximum number of occurrences
      Throws:
      NullPointerException - If the token rule is null
      IllegalArgumentException - If the min or max occurrences are lower than 0, or if the max occurrences are lower than the min occurrences, or if both are 0
  • 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. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      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.
    • tokenRule

      @NotNull public @NotNull TokenRule tokenRule()
      Returns the value of the tokenRule record component.
      Returns:
      the value of the tokenRule record component
    • minOccurrences

      public int minOccurrences()
      Returns the value of the minOccurrences record component.
      Returns:
      the value of the minOccurrences record component
    • maxOccurrences

      public int maxOccurrences()
      Returns the value of the maxOccurrences record component.
      Returns:
      the value of the maxOccurrences record component