public class MapSymbolTable extends Object implements Serializable, SymbolTable
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.
UNKNOWN_SYMBOL_ID| Constructor and Description |
|---|
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.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clears all of the symbols from the symbol table.
|
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.
|
public MapSymbolTable()
0) and subsequent
symbols are assigned successive integer identifiers.public MapSymbolTable(int firstId)
firstId - Identifier of first symbol.public MapSymbolTable(Map<String,Integer> symbolToIdMap)
symbolToIdMap - A map from symbols to identifiers.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.public Set<Integer> idSet()
public Set<String> symbolSet()
public int numSymbols()
SymbolTablenumSymbols in interface SymbolTablepublic int symbolToID(String symbol)
SymbolTable-1 if the symbol does not exist. The constant
-1 is available as the value of SymbolTable.UNKNOWN_SYMBOL_ID.symbolToID in interface SymbolTablesymbol - Symbol whose identifier is returned.-1 if the symbol does not exist.public Integer symbolToIDInteger(String symbol)
symbol - Symbol whose identifer is returned.public String idToSymbol(Integer id)
id - Integer identifier.IndexOutOfBoundsException - If the symbol could
not be found in the symbol table.public String idToSymbol(int id)
SymbolTable0 and the
number of symbols in the table minus one, inclusive. Raises an
index out of bounds exception for identifiers out of range.idToSymbol in interface SymbolTableid - Identifier whose symbol is returned.public int removeSymbol(String symbol)
removeSymbol in interface SymbolTablesymbol - Symbol to remove.public void clear()
clear in interface SymbolTablepublic int getOrAddSymbol(String symbol)
getOrAddSymbol in interface SymbolTablesymbol - Symbol to get or add to the table.public Integer getOrAddSymbolInteger(String symbol)
symbol - Symbol to get or add to the table.public String toString()
public static SymbolTable unmodifiableView(SymbolTable table)
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.
table - Table a view of which is returned.Copyright © 2019 Alias-i, Inc.. All rights reserved.