Class NfaMatcher<T>

  • Type Parameters:
    T - the type of objects to match against the pattern

    public class NfaMatcher<T>
    extends java.lang.Object
    Non-deterministic Finite Automaton (NFA) implementation for pattern matching.

    This class implements a Thompson NFA for regular expression matching. It converts a regular expression to postfix notation, builds an NFA, and uses it to match sequences of objects against the pattern.

    The implementation is based on the algorithm described in Russ Cox's article: https://swtch.com/~rsc/regexp/regexp1.html

    • Constructor Summary

      Constructors 
      Constructor Description
      NfaMatcher​(java.lang.String regexp, java.util.function.BiFunction<T,​java.lang.String,​java.lang.Boolean> matcher)
      Creates a new NfaMatcher with the specified regular expression and matcher function.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void compile()
      Compiles the regular expression into an NFA.
      boolean match​(java.util.List<T> args)
      Matches a list of arguments against the pattern.
      java.util.Set<java.lang.String> matchPartial​(java.util.List<T> args)
      Returns the list of possible matcher names for the next object
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • NfaMatcher

        public NfaMatcher​(java.lang.String regexp,
                          java.util.function.BiFunction<T,​java.lang.String,​java.lang.Boolean> matcher)
        Creates a new NfaMatcher with the specified regular expression and matcher function.
        Parameters:
        regexp - the regular expression pattern
        matcher - the function to match an input against a pattern
    • Method Detail

      • compile

        public void compile()
        Compiles the regular expression into an NFA.

        This method is called automatically when needed, but can be called explicitly to precompile the pattern.

      • match

        public boolean match​(java.util.List<T> args)
        Matches a list of arguments against the pattern.

        This method uses the NFA to determine if the sequence of arguments matches the regular expression pattern.

        Parameters:
        args - the list of arguments to match
        Returns:
        true if the arguments match the pattern, false otherwise
      • matchPartial

        public java.util.Set<java.lang.String> matchPartial​(java.util.List<T> args)
        Returns the list of possible matcher names for the next object
        Parameters:
        args - input list
        Returns:
        the list of possible matcher names for the next object