com.aliasi.symbol
Class MapSymbolTable

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

public class MapSymbolTable
extends Object
implements Compilable, Serializable, SymbolTable

A MapSymbolTable is a dynamic symbol table based on a pair of underlying maps. After creating a map symbol table, new symbols may be added using getOrAddSymbol(String).

Map symbol tables are serializable. The result of writing to an object output stream and reading back in through an object input stream produces an instance of this same class, MapSymbolTable, with the same behavior as the instance serialized.

Implementation Note: This table uses a pair of maps, one in each direction between symbols represented by instances of String and identifiers represented by instance of Integer.

Since:
LingPipe2.0
Version:
3.9
Author:
Bob Carpenter, Mike Ross
See Also:
Serialized Form

Field Summary
 
Fields inherited from interface com.aliasi.symbol.SymbolTable
UNKNOWN_SYMBOL_ID
 
Constructor Summary
MapSymbolTable()
          Construct an empty map symbol table.
MapSymbolTable(int firstId)
          Construct an empty map symbol table that begins allocating identifiers at the specified value.
MapSymbolTable(Map<String,Integer> symbolToIdMap)
          Construct a map symbol which associates symbols to identifiers based on the specified map.
 
Method Summary
 void clear()
          Clears all of the symbols from the symbol table.
 void compileTo(ObjectOutput objOut)
          Deprecated. As of LingPipe 3.1, use objOut.writeObject(this).
 int getOrAddSymbol(String symbol)
          Returns the identifier for the specified symbol, adding it to the symbol table if necessary.
 Integer getOrAddSymbolInteger(String symbol)
          Returns the integer identifier for the specified symbol, adding it to the symbol table if necessary.
 Set<Integer> idSet()
          Returns the complete set of ids in this symbol table.
 String idToSymbol(int id)
          Return the symbol corresponding to the specified identifier.
 String idToSymbol(Integer id)
          Returns the symbol for the specified identifier.
 int numSymbols()
          Returns the number of symbols in this symbol table.
 int removeSymbol(String symbol)
          Removes the specified symbol from the symbol table.
 Set<String> symbolSet()
          Returns the complete set of symbols in this symbol table.
 int symbolToID(String symbol)
          Return the identifier corresponding to the specified symbol, -1 if the symbol does not exist.
 Integer symbolToIDInteger(String symbol)
          Returns an Integer representation of the symbol if it exists in the table or null if it does not.
 String toString()
          Returns a string-based representation of this symbol table by printing the underlying identifier to symbol mapping.
static SymbolTable unmodifiableView(SymbolTable table)
          Returns a view of the specified symbol table that cannot be modified.
 void writeTo(ObjectOutput objOut)
          Deprecated. As of LingPipe 3.1, use objOut.writeObject(this).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MapSymbolTable

public MapSymbolTable()
Construct an empty map symbol table. The default first symbol identifier is zero (0) and subsequent symbols are assigned successive integer identifiers.


MapSymbolTable

public MapSymbolTable(int firstId)
Construct an empty map symbol table that begins allocating identifiers at the specified value. Subsequent symbols will be assigned identifiers to successive identifiers.

Parameters:
firstId - Identifier of first symbol.

MapSymbolTable

public MapSymbolTable(Map<String,Integer> symbolToIdMap)
Construct a map symbol which associates symbols to identifiers based on the specified map.

Parameters:
symbolToIdMap - A map from symbols to identifiers.
Throws:
IllegalArgumentException - If two distinct symbols map to the same identifier, or if the unknown symbol identifier, SymbolTable.UNKNOWN_SYMBOL_ID, is used as an identifier.
Method Detail

idSet

public Set<Integer> idSet()
Returns the complete set of ids in this symbol table.

Returns:
The set of ids for this symbol table.

symbolSet

public Set<String> symbolSet()
Returns the complete set of symbols in this symbol table.

Returns:
The set of symbols for this symbol table.

writeTo

@Deprecated
public void writeTo(ObjectOutput objOut)
             throws IOException
Deprecated. As of LingPipe 3.1, use objOut.writeObject(this).

Writes this map symbol table to the specified object output. The object that will be read back in will be an instance of MapSymbolTable with the same entries as this symbol table.

Calling this method, writeTo(objOut) produces the same result as standard serialization, objOut.writeObject(this). This method has been deprecated in favor of standard serialization.

Parameters:
objOut - Object output to which to write this symbol table.
Throws:
IOException - If there is an I/O error writing.

compileTo

@Deprecated
public void compileTo(ObjectOutput objOut)
               throws IOException
Deprecated. As of LingPipe 3.1, use objOut.writeObject(this).

Compiles this map symbol table to the specified object output. The object that will be read back in will be an instance of MapSymbolTable with the same entries as this symbol table, so this is in some sense a no-op compilation.

Calling this method, compileTo(objOut) produces the same result as standard serialization, objOut.writeObject(this). This method has been deprecated in favor of standard serialization.

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

numSymbols

public int numSymbols()
Description copied from interface: SymbolTable
Returns the number of symbols in this symbol table.

Specified by:
numSymbols in interface SymbolTable
Returns:
Number of symbols in this table.

symbolToID

public int symbolToID(String symbol)
Description copied from interface: SymbolTable
Return the identifier corresponding to the specified symbol, -1 if the symbol does not exist. The constant -1 is available as the value of SymbolTable.UNKNOWN_SYMBOL_ID.

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.

symbolToIDInteger

public Integer symbolToIDInteger(String symbol)
Returns an Integer representation of the symbol if it exists in the table or null if it does not.

Parameters:
symbol - Symbol whose identifer is returned.
Returns:
Integer identifier for symbol, or null if it does not exist.

idToSymbol

public String idToSymbol(Integer id)
Returns the symbol for the specified identifier. If the identifier has no defined symbol, an exception is thrown.

Parameters:
id - Integer identifier.
Returns:
The symbol for the identifier.
Throws:
IndexOutOfBoundsException - If the symbol could not be found in the symbol table.

idToSymbol

public String idToSymbol(int id)
Description copied from interface: SymbolTable
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.

Specified by:
idToSymbol in interface SymbolTable
Parameters:
id - Identifier whose symbol is returned.
Returns:
Symbol corresponding to the specified identifier.

removeSymbol

public int removeSymbol(String symbol)
Removes the specified symbol from the symbol table. After the symbol is removed, its identifier will not be assigned to another symbol.

Specified by:
removeSymbol in interface SymbolTable
Parameters:
symbol - Symbol to remove.
Returns:
The previous id of the symbol if it was in the table, or -1 if it was not.

clear

public void clear()
Clears all of the symbols from the symbol table. It does not reset the symbol counter, so the removed identifiers will not be reused.

Specified by:
clear in interface SymbolTable

getOrAddSymbol

public int getOrAddSymbol(String symbol)
Returns the identifier for the specified symbol, adding it to the symbol table if necessary.

Specified by:
getOrAddSymbol in interface SymbolTable
Parameters:
symbol - Symbol to get or add to the table.
Returns:
Identifier for specified symbol.

getOrAddSymbolInteger

public Integer getOrAddSymbolInteger(String symbol)
Returns the integer identifier for the specified symbol, adding it to the symbol table if necessary.

Parameters:
symbol - Symbol to get or add to the table.
Returns:
Identifier for specified symbol.

toString

public String toString()
Returns a string-based representation of this symbol table by printing the underlying identifier to symbol mapping.

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

unmodifiableView

public static SymbolTable unmodifiableView(SymbolTable table)
Returns a view of the specified symbol table that cannot be modified. The methods clear(), getOrAddSymbol(String), and removeSymbol(String) will throw unsupported operation exceptions when called. The remaining methods will delegate to the specified table, to which the returned view holds a reference.

The unmodifiable view is serializable if the underlying symbol table is serializable. The symbol table read back in will also be unmodifiable.

Parameters:
table - Table a view of which is returned.
Returns:
An unmodifiable view of the specified table.