Class MatchOnlyRunAutomaton

  • All Implemented Interfaces:
    java.io.Serializable

    public class MatchOnlyRunAutomaton
    extends java.lang.Object
    implements java.io.Serializable
    Supports an optimised version of substring matching an automaton against an input string in linear time with no backtracking.

    In essence we simply take an automaton "mypattern" and match against ".*mypattern". See the docs here from Russ Cox: Link.

    As such we do not currently support identifying the start, end position of a match, simply that a match exists.

    Execution time is linear to the length of the string O(n) for the method matches(CharSequence) below.

    See Also:
    Serialized Form
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static Automaton addWildcard​(Automaton automaton)  
      static MatchOnlyRunAutomaton load​(java.io.InputStream stream)
      Retrieves a serialized MatchOnlyRunAutomaton from a stream.
      boolean matches​(java.lang.CharSequence input)
      Search the input in linear time based on the size of the input to identify whether this automaton is contained anywhere in the input.
      boolean matches​(java.lang.CharSequence input, int pos)
      Search the input in linear time based on the size of the input to identify whether this pattern is contained anywhere in the input from a starting position.
      void store​(java.io.OutputStream stream)
      Writes this MatchOnlyRunAutomaton to the given stream.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • MatchOnlyRunAutomaton

        public MatchOnlyRunAutomaton​(Automaton a)
      • MatchOnlyRunAutomaton

        public MatchOnlyRunAutomaton​(Automaton a,
                                     boolean tableize)
    • Method Detail

      • matches

        public boolean matches​(java.lang.CharSequence input)
        Search the input in linear time based on the size of the input to identify whether this automaton is contained anywhere in the input.

        Has the following constraints:

        1. In the worst case, the execution is equivalent to the length of the input O(n) as we scan the whole input.
        2. There is no backtracking, we simply scan the input char by char until we either match or reach the end.
        3. Method returns immediately if it finds enough chars to complete a match of the pattern.
        4. The method and class has no ability to locate the position of the match, simply that is either exists or not.
        Parameters:
        input - the input to match a substring pattern against
        Returns:
        true if the pattern is found in a substring in the input, or false if the input is not matched. We also return false for null inputs.
      • matches

        public boolean matches​(java.lang.CharSequence input,
                               int pos)
        Search the input in linear time based on the size of the input to identify whether this pattern is contained anywhere in the input from a starting position.

        See matches(CharSequence) for more information.

        This version of the method searches from a specific starting position.

        Parameters:
        input - the input to match a substring pattern against
        pos - the position to start searching from
        Returns:
        true if the pattern is found in a substring in the input, or false if the input is not matched. We also return false for null inputs
        Throws:
        java.lang.IllegalArgumentException - if the pos is negative or larger than the input length.
      • load

        public static MatchOnlyRunAutomaton load​(java.io.InputStream stream)
                                          throws java.io.IOException,
                                                 java.lang.ClassCastException,
                                                 java.lang.ClassNotFoundException
        Retrieves a serialized MatchOnlyRunAutomaton from a stream.
        Parameters:
        stream - input stream with serialized automaton
        Throws:
        java.io.IOException - if input/output related exception occurs
        java.lang.ClassCastException - if the data is not a serialized MatchOnlyRunAutomaton
        java.lang.ClassNotFoundException - if the class of the serialized object cannot be found
      • store

        public void store​(java.io.OutputStream stream)
                   throws java.io.IOException
        Writes this MatchOnlyRunAutomaton to the given stream.
        Parameters:
        stream - output stream for serialized automaton
        Throws:
        java.io.IOException - if input/output related exception occurs
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object