uk.ac.man.cs.rainbow
Class Utils

java.lang.Object
  |
  +--uk.ac.man.cs.rainbow.Utils

public final class Utils
extends Object

Basic utility methods for Rainbow.


Method Summary
static String compoundList(String start, String sep, String end, Object[] items)
          Concatenate list of objects with a separator and bracketing strings.
static String compoundList(String start, String sep, String end, Object[] items1, String sep2, Object[] items2)
          Concatenate-merge two lists of objects with separators and bracketing strings.
static void DEBUG(String msg)
          Print a debugging string.
static Hashtable makeHash(String[] strings)
          Convert an array of strings into a hashtable.
static Hashtable makeHashInt(String[] strings)
          Convert an array of strings into a hashtable.
static String makeTclLine(String s)
          Make a Tcl-formatted line from a string.
static String makeTclLine(String[] strings)
          Make a Tcl-formatted line from a list of strings.
static String readTclLine(BufferedReader reader)
          Read a complete Tcl-formatted line.
static String[] splitTclLine(String line)
          Split a Tcl-formatted line up into words.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

compoundList

public static final String compoundList(String start,
                                        String sep,
                                        String end,
                                        Object[] items)
Concatenate list of objects with a separator and bracketing strings. Very useful for "pretty-printing" arrays of objects.

Thus:
Utils.compoundList(a, s, b, V[n])
Produces output:
a + V0 + s + V1 + s + ... + s + Vn-2 + s + Vn-1 + b
Example:
Utils.compoundList("[", ";", "]", {1,2,3}) produces "[1;2;3]"
Note that this relies massively on the toString() method of the objects in the array.
Parameters:
start - Leading string.
sep - Separating string.
end - Terminating string.
items - Array of objects to print prettily.

compoundList

public static final String compoundList(String start,
                                        String sep,
                                        String end,
                                        Object[] items1,
                                        String sep2,
                                        Object[] items2)
Concatenate-merge two lists of objects with separators and bracketing strings. Very useful for "pretty-printing" arrays of objects (especially with "type" annotations.)

Thus:
Utils.compoundList(a, s, b, V[n], t, W[n])
Produces output:
a + V0 + t + W0 + s + ... + s + Vn-1 + t + Wn-1 + b
Example:
Utils.compoundList("[", ";", "]", {1,2}, "x", {3,4}) produces "[1x3;2x4]"
Note that this relies massively on the toString() method of the objects in the arrays.
Parameters:
start - Leading string.
sep - Separating string.
end - Terminating string.
items1 - First array of objects to print prettily.
sep2 - Second separating string (between items1i and items2i)
items2 - Second array of objects to print prettily.

readTclLine

public static String readTclLine(BufferedReader reader)
                          throws IOException,
                                 TclFormatException
Read a complete Tcl-formatted line.
Parameters:
reader - The data source to read the line from.
Returns:
The line read from the data source. May be multiple physical lines.
Throws:
IOException - When something goes wrong with the data source.
TclFormatException - When there isn't a well-formatted Tcl line on the data source.

splitTclLine

public static String[] splitTclLine(String line)
                             throws TclFormatException
Split a Tcl-formatted line up into words.
Parameters:
line - The line to split.
Returns:
An array of words.
Throws:
TclFormatException - If the line isn't a Tcl-formatted list of words, or if it contains an unsupported escape sequence.
See Also:
readTclLine(BufferedReader)

makeTclLine

public static String makeTclLine(String s)
Make a Tcl-formatted line from a string. This is done by adding backslashes if necessary.
Parameters:
s - The string to convert to a Tcl-formatted line.
Returns:
A Tcl-formatted line (with no terminating newline.)
See Also:
makeTclLine(String[])

makeTclLine

public static String makeTclLine(String[] strings)
Make a Tcl-formatted line from a list of strings. This is done by adding backslashes if necessary, and by using braces otherwise.
Parameters:
strings - The array of strings to convert to a Tcl-formatted line.
Returns:
A Tcl-formatted line (with no terminating newline.)
See Also:
makeTclLine(String)

makeHash

public static Hashtable makeHash(String[] strings)
                          throws RainbowException
Convert an array of strings into a hashtable. This can be seen as an analog of Tcl's array set command.
Parameters:
strings - Array of keys and values.
Throws:
RainbowException - If there are an odd number of input strings.
See Also:
makeHashInt(String[])

makeHashInt

public static Hashtable makeHashInt(String[] strings)
                             throws RainbowException,
                                    NumberFormatException
Convert an array of strings into a hashtable. This can be seen as an analog of Tcl's array set command. The values in the returned hash will be instances of Integer.
Parameters:
strings - Array of keys and values.
Throws:
RainbowException - If there are an odd number of input strings.
NumberFormatException - If any of the values are not valid base-10 integers.
See Also:
makeHash(String[])

DEBUG

public static void DEBUG(String msg)
Print a debugging string. Use this so that all debugging can be turned on and off easily. The string DEBUG is also easier to search for in programs...
Parameters:
msg - String to print.