Package com.yahoo.fsa
Class FSA
- java.lang.Object
-
- com.yahoo.fsa.FSA
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class FSA extends java.lang.Object implements java.io.Closeable
Finite-State Automaton.- Author:
- Peter Boros
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FSA.Iterator
Class used to iterate over all accepted strings in the fsa.static class
FSA.State
Thread local state object used to traverse a Finite-State Automaton.
-
Constructor Summary
Constructors Constructor Description FSA(java.io.FileInputStream file)
Loads an FSA from a file input stream using utf-8 encodingFSA(java.io.FileInputStream file, java.lang.String charsetname)
FSA(java.lang.String filename)
Loads an FSA from a file using utf-8 encodingFSA(java.lang.String filename, java.lang.String charsetname)
Loads an FSA from a file using the specified character encoding.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
protected java.nio.ByteBuffer
data(int state)
Retrieves data for the given state using the underlying fsa data buffer.protected java.lang.String
dataString(int state)
Retrieves data for the given state using the underlying fsa data buffer.protected int
delta(int state, byte symbol)
FSA.State
getState()
protected int
hashDelta(int state, byte symbol)
boolean
hasPerfectHash()
protected boolean
isFinal(int state)
boolean
isOk()
FSA.Iterator
iterator()
Returns a new iterator to the start state.FSA.Iterator
iterator(FSA.State state)
Returns a new iterator to the given state.static FSA
loadFromResource(java.lang.String resourceFileName, java.lang.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.java.lang.String
lookup(java.lang.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.static void
main(java.lang.String[] args)
int
serial()
protected int
start()
int
version()
-
-
-
Constructor Detail
-
FSA
public FSA(java.lang.String filename)
Loads an FSA from a file using utf-8 encoding- Throws:
java.lang.IllegalArgumentException
- if the file is not found
-
FSA
public FSA(java.lang.String filename, java.lang.String charsetname)
Loads an FSA from a file using the specified character encoding.- Throws:
java.lang.IllegalArgumentException
- if the file is not found
-
FSA
public FSA(java.io.FileInputStream file)
Loads an FSA from a file input stream using utf-8 encoding
-
FSA
public FSA(java.io.FileInputStream file, java.lang.String charsetname)
-
-
Method Detail
-
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(java.lang.String resourceFileName, java.lang.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.fsaloadingClass
- 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:
java.lang.RuntimeException
- if the class could not be loaded
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.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 java.nio.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 java.lang.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 java.lang.String lookup(java.lang.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(java.lang.String[] args)
-
-