jist.runtime
Class Util

java.lang.Object
  extended by jist.runtime.Util

public final class Util
extends java.lang.Object

Miscellaneous utilities used by JIST.

Since:
JIST1.0
Version:
$Id: Util.java,v 1.39 2004/05/22 19:04:53 barr Exp $
Author:
Rimon Barr <barr+jist@cs.cornell.edu>

Field Summary
static char IDENTIFIER_ESCAPE_CHAR
          Escape character used when converting String into Java-safe identifiers.
private static java.lang.Integer[] INTS
          An array of Integers for small primitive integers.
static java.lang.reflect.Method method_printType
          Method stub for printType method.
 
Constructor Summary
Util()
           
 
Method Summary
static void assertion(boolean cond)
          Validate condition.
static boolean contains(java.lang.Object[] set, java.lang.Object item)
          Return whether a given Objects exists within an Object array.
static java.lang.String escapeChar(java.lang.String s, char c)
          Escape character in string.
static java.lang.String escapeJavaIdent(java.lang.String s)
          Escape string so that it conforms to Java identifier rules.
static java.lang.reflect.Method findUniqueMethod(java.lang.Class c, java.lang.String name)
          Lookup method in a class by name.
static java.lang.String getHexCode(char c)
          Return hexadecimal representation of given character.
static java.lang.String getHMS(long second)
          Return seconds as string of hours, minutes and seconds.
static int[] getRange(int max)
          Return a range of integers in an array: [0, max).
static byte[] getResourceBytes(java.lang.String name)
          Get data of given named resource as byte array.
static java.lang.Integer getSmallInteger(int i)
          Return the Integer for a small integer, hopefully without a new allocation.
static long getUsedMemory()
          Return the number of memory bytes used, which is computed by subtracting the 'free' memory from 'total' memory.
static java.lang.Object printType(java.lang.Object o)
          Method that prints the type of the object passed to it.
static byte[] readAll(java.io.InputStream in)
          Read an entire stream and return byte[].
static double round(double num, int decimal)
          Round-off double to given number of decimal places.
static void showStack()
          Display a stack trace.
static void showThreads()
          Display the currently active threads.
static int sign(long l)
          Return the sign of a long.
static void startIdleThread()
          Start an idle thread.
static java.lang.String stringJoin(java.lang.String[] strings, java.lang.String delim)
          Concatenate array of Strings separated by given delimeter.
static java.lang.Object[] union(java.lang.Object[] set1, java.lang.Object[] set2)
          Return the set union of two array of objects.
static java.lang.String[] union(java.lang.String[] set1, java.lang.String[] set2)
          Same as union, but for Strings.
static java.lang.Object[] unique(java.lang.Object[] elements)
          Return a set (array of unique objects).
static java.lang.String[] unique(java.lang.String[] elements)
          Same as unique, but for Strings.
static java.lang.String unqualifiedName(java.lang.String s)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IDENTIFIER_ESCAPE_CHAR

public static final char IDENTIFIER_ESCAPE_CHAR
Escape character used when converting String into Java-safe identifiers.

See Also:
Constant Field Values

INTS

private static final java.lang.Integer[] INTS
An array of Integers for small primitive integers.


method_printType

public static java.lang.reflect.Method method_printType
Method stub for printType method.

Constructor Detail

Util

public Util()
Method Detail

contains

public static boolean contains(java.lang.Object[] set,
                               java.lang.Object item)
Return whether a given Objects exists within an Object array.

Parameters:
set - an array of objects to test for membership
item - object to test membership
Returns:
whether given item exists in the given set

escapeChar

public static java.lang.String escapeChar(java.lang.String s,
                                          char c)
Escape character in string. Given character is replaced with escape characters and hexadecimal representation.

Parameters:
s - string to escape
c - character to replace/escape
Returns:
escaped string

escapeJavaIdent

public static java.lang.String escapeJavaIdent(java.lang.String s)
Escape string so that it conforms to Java identifier rules.

Parameters:
s - string to escape
Returns:
escaped string: can be used as Java identifier

getHexCode

public static java.lang.String getHexCode(char c)
Return hexadecimal representation of given character.

Parameters:
c - character to convert
Returns:
hexadecimal representation of given character

unique

public static java.lang.Object[] unique(java.lang.Object[] elements)
Return a set (array of unique objects).

Parameters:
elements - array of objects, possibly with duplicates
Returns:
array of objects with duplicates removed; order is not preserved.

union

public static java.lang.Object[] union(java.lang.Object[] set1,
                                       java.lang.Object[] set2)
Return the set union of two array of objects.

Parameters:
set1 - first set of objects
set2 - second set of objects
Returns:
set union of set1 and set2

unique

public static java.lang.String[] unique(java.lang.String[] elements)
Same as unique, but for Strings.

Parameters:
elements - array of Strings, possibly with duplicates
Returns:
array of Strings with duplicates removed; order is not preserved.

union

public static java.lang.String[] union(java.lang.String[] set1,
                                       java.lang.String[] set2)
Same as union, but for Strings.

Parameters:
set1 - first set of Strings
set2 - second set of Strings
Returns:
set union of set1 and set2

startIdleThread

public static void startIdleThread()
Start an idle thread. Usually used to prevent JVM from exiting.


showThreads

public static void showThreads()
Display the currently active threads.


stringJoin

public static java.lang.String stringJoin(java.lang.String[] strings,
                                          java.lang.String delim)
Concatenate array of Strings separated by given delimeter.

Parameters:
strings - array of strings to concatenate
delim - delimeter to insert between each pair of strings
Returns:
delimited concatenation of strings

getRange

public static int[] getRange(int max)
Return a range of integers in an array: [0, max).

Parameters:
max - range maximum limit
Returns:
integer array containing [0, max)

printType

public static java.lang.Object printType(java.lang.Object o)
Method that prints the type of the object passed to it. Useful for rewritter debugging as in: DUP INVOKE Util.printType

Parameters:
o - some object
Returns:
object passed in (to keep stack unchanged)

round

public static double round(double num,
                           int decimal)
Round-off double to given number of decimal places.

Parameters:
num - number to round
decimal - decimal places
Returns:
rounded number

getUsedMemory

public static long getUsedMemory()
Return the number of memory bytes used, which is computed by subtracting the 'free' memory from 'total' memory.

Returns:
number of bytes of memory used

showStack

public static void showStack()
Display a stack trace.


readAll

public static byte[] readAll(java.io.InputStream in)
                      throws java.io.IOException
Read an entire stream and return byte[].

Parameters:
in - inputstream to devour
Returns:
byte[] of data read
Throws:
java.io.IOException - on error from given inputstream

getResourceBytes

public static byte[] getResourceBytes(java.lang.String name)
Get data of given named resource as byte array.

Parameters:
name - resource to retrieve
Returns:
resource data as byte array

getHMS

public static java.lang.String getHMS(long second)
Return seconds as string of hours, minutes and seconds.

Parameters:
second - number of seconds
Returns:
string in HHhMMmSSs format

assertion

public static void assertion(boolean cond)
Validate condition.

Parameters:
cond - condition to validate

findUniqueMethod

public static java.lang.reflect.Method findUniqueMethod(java.lang.Class c,
                                                        java.lang.String name)
                                                 throws java.lang.NoSuchMethodException
Lookup method in a class by name.

Parameters:
c - class to scan
name - method name
Returns:
method, if found and unique
Throws:
java.lang.NoSuchMethodException - if method not found or not unique

getSmallInteger

public static java.lang.Integer getSmallInteger(int i)
Return the Integer for a small integer, hopefully without a new allocation.

Parameters:
i - some integer to convert to an Integer
Returns:
Integer object corresponding integer primitive

sign

public static int sign(long l)
Return the sign of a long.

Returns:
sign of a long.

unqualifiedName

public static java.lang.String unqualifiedName(java.lang.String s)