com.aliasi.symbol
Class SymbolTableCompiler

java.lang.Object
  extended by com.aliasi.symbol.SymbolTableCompiler
All Implemented Interfaces:
SymbolTable, Compilable

public class SymbolTableCompiler
extends Object
implements Compilable, SymbolTable

A SymbolTableCompiler collects symbols represented as String instances and compiles them into a SymbolTable. At any point after adding symbols, the symbol table can be written to a data output stream. After being written, the symbol table compiler properly implements the SymbolTable interface through the result of compilation. A static method, read, is provided to read a symbol table from a data input stream. It works as a symbol table after a call to compileTo(DataOutputStream).

Since:
LingPipe1.0
Version:
3.9
Author:
Bob Carpenter

Field Summary
 
Fields inherited from interface com.aliasi.symbol.SymbolTable
UNKNOWN_SYMBOL_ID
 
Constructor Summary
SymbolTableCompiler()
          Construct a fresh symbol table compiler.
 
Method Summary
 boolean addSymbol(String symbol)
          Add a symbol to the symbol table to be compiled.
static SymbolTable asSymbolTable(String[] symbols)
          Returns a compiled symbol table constructed from the specified list of symbols with symbol identifiers determined by array position.
 void clear()
          Throws an unsupported operation exception.
 void compileTo(DataOutputStream out)
          Deprecated. As of LingPipe 2.3, use compileTo(ObjectOutput).
 void compileTo(ObjectOutput objOut)
          Compiles the symbol table to the specified object output.
 int getOrAddSymbol(String symbol)
          Throws an unsupported operation exception.
 String idToSymbol(int id)
          Return the symbol corresponding to the specified identifier.
 int numSymbols()
          Returns the number of symbols in this symbol table.
static SymbolTable read(DataInputStream in)
          Deprecated. As of LingPipe 2.3, use compileTo(ObjectOutput) to write and then read in generally from an object input stream.
 int removeSymbol(String symbol)
          Throws an unsupported operation exception.
 String[] symbols()
          Returns the symbols in this symbol table.
 int symbolToID(String symbol)
          Return the identifier corresponding to the specified symbol, or -1 if the symbol does not exist.
 String toString()
          Returns a string-based representation of this symbol table.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SymbolTableCompiler

public SymbolTableCompiler()
Construct a fresh symbol table compiler.

Method Detail

asSymbolTable

public static SymbolTable asSymbolTable(String[] symbols)
Returns a compiled symbol table constructed from the specified list of symbols with symbol identifiers determined by array position. The array of symbols must not contain duplicate symbols, but may be in any order.

The array of symbols is copied so that subsequent changes to the symbol array will not affect the returned symbol table.

The returned symbol table may be serialized.

Parameters:
symbols - Array of symbols to back the symbol table.
Returns:
Compiled symbol table corresponding to the array of symbols.
Throws:
IllegalArgumentException - If there are duplicate symbols in the array.

compileTo

@Deprecated
public void compileTo(DataOutputStream out)
               throws IOException
Deprecated. As of LingPipe 2.3, use compileTo(ObjectOutput).

Compiles this symbol table to the specified data output stream. The static method read(DataInputStream) may be used to read in a compiled model. The compiled model will implement SymbolTable.

Parameters:
out - Data output stream to which symbol table is written.
Throws:
IOException - If there is an underlying I/O error.

compileTo

public void compileTo(ObjectOutput objOut)
               throws IOException
Compiles the symbol table to the specified object output.

Specified by:
compileTo in interface Compilable
Parameters:
objOut - Object output to which symbol table is written.
Throws:
IOException - If there is an underlying I/O error.

read

@Deprecated
public static SymbolTable read(DataInputStream in)
                        throws IOException
Deprecated. As of LingPipe 2.3, use compileTo(ObjectOutput) to write and then read in generally from an object input stream.

Return a compiled symbol table read from the specified data input stream.

Parameters:
in - Data input stream from which to read the symbol table.
Throws:
IOException - If there is an exception reading from the underlying stream.

symbolToID

public int symbolToID(String symbol)
Return the identifier corresponding to the specified symbol, or -1 if the symbol does not exist. This method should only be called after compilation; calling it before will throw an illegal state exception.

Specified by:
symbolToID in interface SymbolTable
Parameters:
symbol - Symbol whose identifier is returned.
Returns:
Identifier corresponding to specified symbol or -1 if the symbol does not exist.
Throws:
IllegalStateException - If this method is called before compilation.

symbols

public String[] symbols()
Returns the symbols in this symbol table.

Returns:
The symbols in this symbol table.

idToSymbol

public String idToSymbol(int id)
Return the symbol corresponding to the specified identifier. Symbols exist for identifiers between 0 and the number of symbols in the table minus one, inclusive. Raises an index out of bounds exception for identifiers out of range. This method should only be called after compilation; calling it before will throw an illegal state exception.

Specified by:
idToSymbol in interface SymbolTable
Parameters:
id - Identifier whose symbol is returned.
Returns:
Symbol corresponding to the specified identifier.
Throws:
IndexOutOfBoundsException - If there is no symbol for the specified identifier.
IllegalStateException - If this method is called before compilation.

numSymbols

public int numSymbols()
Returns the number of symbols in this symbol table.

Specified by:
numSymbols in interface SymbolTable
Returns:
Number of symbols in this table.
Throws:
IllegalStateException - If this method is called before compilation.

clear

public void clear()
Throws an unsupported operation exception.

Specified by:
clear in interface SymbolTable
Throws:
UnsupportedOperationException - Always.

getOrAddSymbol

public int getOrAddSymbol(String symbol)
Throws an unsupported operation exception.

Specified by:
getOrAddSymbol in interface SymbolTable
Parameters:
symbol - Symbol is ignored.
Returns:
Always throws an exception before returning a value.
Throws:
UnsupportedOperationException - Always.

addSymbol

public boolean addSymbol(String symbol)
Add a symbol to the symbol table to be compiled. Symbols are restricted to a maximum length of Short.MAX_VALUE; attempts to add longer symbols will throw an IllegalArgumentException.

Parameters:
symbol - Symbol to add.
Returns:
true if the symbol was not already in the table.
Throws:
IllegalArgumentException - If the symbol is longer than Short.MAX_VALUE.

removeSymbol

public int removeSymbol(String symbol)
Throws an unsupported operation exception.

Specified by:
removeSymbol in interface SymbolTable
Parameters:
symbol - Symbol is ignored.
Returns:
Always throws an exception before returning a value.
Throws:
UnsupportedOperationException - Always.

toString

public String toString()
Returns a string-based representation of this symbol table.

Overrides:
toString in class Object
Returns:
A string-based representation of this symbol table.