Sharkysoft home

lava.util
Class NumberedStrings

java.lang.Object
  |
  +--lava.util.NumberedStrings

public class NumberedStrings
extends java.lang.Object

An instance of NumberedStrings is a mapping between integers and strings. After construction, the mapping is initially empty. Pairs are registered into the mapping by repeated calls to register.

After all pairs have been registered, the mapping can be queried by calls to toInt(String) and toString(int).

If register is called using integer and string values that have already been used in previous calls to register, then the last association takes priority. For example, if you register (3, "strikes") and (3, "wishes"), in that order, then 3 will map to "wishes" and not "strikes". However, both "strikes" and "wishes" will still map to 3.


Field Summary
protected  java.lang.Integer failure_value
          The default Integer object to return when a String lookup fails.
protected  java.util.Hashtable table
          Storage for number-string pairs.
 
Constructor Summary
NumberedStrings()
          Creates an empty assocation.
 
Method Summary
 void register(int i, java.lang.String s)
          Registers an integer-string pair into this instance.
 int toInt(java.lang.String s)
          Returns the integer that has been registered in association with the given string.
 java.lang.String toString(int i)
          Returns the string that has been registered in association with the given integer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

failure_value

protected java.lang.Integer failure_value
The default Integer object to return when a String lookup fails.

Details: Normally, when a call to toInt fails to find an int associated with the supplied string, a NoSuchElementException is thrown. However, if this member is set (to non-null), the int value of this Integer object will be returned instead, and no exception will be thrown. This makes it possible to supply a default value for failed lookups.

Since:
1998.10.11

table

protected java.util.Hashtable table
Storage for number-string pairs. Each pair is stored in the table twice so that both members of the pair can act as keys.
Constructor Detail

NumberedStrings

public NumberedStrings()
Creates an empty assocation. This instance must be populated with calls to register.
Method Detail

register

public void register(int i,
                     java.lang.String s)
Registers an integer-string pair into this instance. If s==null then i is registered as the default value for failed toInt calls. Call this method with s set to null to register default return values for toInt and toString.
Parameters:
i - the integer
s - the string
Since:
1998.??.??

toInt

public int toInt(java.lang.String s)
          throws java.util.NoSuchElementException
Returns the integer that has been registered in association with the given string. If no integer has been bound to the string, a NoSuchElementException is thrown, unless a default value has been registered.
Parameters:
s - the string
Throws:
java.util.NoSuchElementException - if the string has not been registered with an integer

toString

public java.lang.String toString(int i)
                          throws java.util.NoSuchElementException
Returns the string that has been registered in association with the given integer. If no string has been bound to the given integer, and failure_value is not set, then a NoSuchElementException is thrown. Otherwise, null is returned.
Parameters:
i - the integer
Throws:
java.util.NoSuchElementException - if the integer has not been registered with a string

Sharkysoft home