Package com.yahoo.fsa

Class FSA

java.lang.Object
com.yahoo.fsa.FSA
All Implemented Interfaces:
Closeable, AutoCloseable

public class FSA extends Object implements Closeable
Finite-State Automaton.
Author:
Peter Boros
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Class used to iterate over all accepted strings in the fsa.
    static class 
    Thread local state object used to traverse a Finite-State Automaton.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Loads an FSA from a file input stream using utf-8 encoding
    FSA(FileInputStream file, String charsetname)
     
    FSA(String filename)
    Loads an FSA from a file using utf-8 encoding
    FSA(String filename, String charsetname)
    Loads an FSA from a file using the specified character encoding.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    protected ByteBuffer
    data(int state)
    Retrieves data for the given state using the underlying fsa data buffer.
    protected String
    dataString(int state)
    Retrieves data for the given state using the underlying fsa data buffer.
    protected int
    delta(int state, byte symbol)
     
     
    protected int
    hashDelta(int state, byte symbol)
     
    boolean
     
    protected boolean
    isFinal(int state)
     
    boolean
     
    Returns a new iterator to the start state.
    Returns a new iterator to the given state.
    static FSA
    loadFromResource(String resourceFileName, Class<?> loadingClass)
    Loads an FSA from a resource file name, which is resolved from the class path of the class loader of the given class.
    Convenience method that returns the metadata string in the fsa for the input lookup String, or null if the input string does not exist in the fsa.
    static void
    main(String[] args)
     
    int
     
    protected int
     
    int
     

    Methods inherited from class java.lang.Object

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

  • Method Details

    • getState

      public FSA.State getState()
    • iterator

      public FSA.Iterator iterator()
      Returns a new iterator to the start state.
    • iterator

      public FSA.Iterator iterator(FSA.State state)
      Returns a new iterator to the given state.
      Parameters:
      state - the state to create the iterator from.
    • loadFromResource

      public static FSA loadFromResource(String resourceFileName, Class<?> loadingClass)
      Loads an FSA from a resource file name, which is resolved from the class path of the class loader of the given class.

      This is useful for loading fsa's deployed within OSGi bundles.

      Parameters:
      resourceFileName - the name of the file, relative to any element on the classpath. For example, if the classpath contains resources/ and the file is resources/myfsa.fsa this argument should be myfsa.fsa
      loadingClass - a class which provides the class loader to use for loading. Any class which is loaded from the same class path as the resource will do (e.g with OSGi - any class in the same bundle jar)
      Returns:
      the loaded FSA
      Throws:
      RuntimeException - if the class could not be loaded
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • isOk

      public boolean isOk()
    • hasPerfectHash

      public boolean hasPerfectHash()
    • version

      public int version()
    • serial

      public int serial()
    • start

      protected int start()
    • delta

      protected int delta(int state, byte symbol)
    • hashDelta

      protected int hashDelta(int state, byte symbol)
    • isFinal

      protected boolean isFinal(int state)
    • data

      protected ByteBuffer data(int state)
      Retrieves data for the given state using the underlying fsa data buffer.
      Parameters:
      state - The fsa state to retrieve data from.
      Returns:
      A new buffer containing the data for the given state.
    • dataString

      protected String dataString(int state)
      Retrieves data for the given state using the underlying fsa data buffer.
      Parameters:
      state - The fsa state to retrieve data from.
      Returns:
      A string representation of the data for the given state.
    • lookup

      public String lookup(String str)
      Convenience method that returns the metadata string in the fsa for the input lookup String, or null if the input string does not exist in the fsa.
      Parameters:
      str - The string to look up.
      Returns:
      Metadata string from the fsa.
    • main

      public static void main(String[] args)