harpoon.Util
Class Util

java.lang.Object
  extended by harpoon.Util.Util

public abstract class Util
extends Object

Miscellaneous static utility functions.

Version:
$Id: Util.java,v 1.22 2005/08/09 20:55:05 salcianu Exp $
Author:
C. Scott Ananian <cananian@alumni.princeton.edu>

Constructor Summary
Util()
           
 
Method Summary
static String adjust_quotes(String str)
          Returns a string that is identical to str, except that every " character has been replaced with the sequence \".
static String code2str(HCodeElement q)
          Returns the string representation of the code instruction q in the formay: source_file:line_number instruction.
static Object convert(Object obj, Map map)
          Converts an objects using the reflexive closure of a map.
static
<T> T[]
copy(ArrayFactory<T> factory, T[] src)
          Deprecated. The clone() method on arrays works much better, and should be used instead of this method.
static String doubleRep(double d, int decimals)
          Returns the string representation of the double d, with a certain number of decimals.
static String doubleRep(double d, int digits, int decimals)
          Returns the string representation of the double d, with a certain number of decimals.
static String escape(String str)
          Escape the contents of a String so they are safe to print.
static ArrayFactory genericFactory(Object[] src)
          Deprecated. requires java language reflection.
static String getLine(HCodeElement q)
          Returns the line of the instruction q in the format source_file:line_number.
static
<T> T[]
grow(ArrayFactory<T> factory, T[] src, T o, int n)
          Insert element o before src[n].
static String jasminEscape(String str)
          Escape the contents of a String so they are safe to print.
static String longProportion(long a, long b, int digits, int decimals)
          Convenient version of longProportion that uses null for the memSizeFormat.
static String longProportion(long a, long b, int digits, int decimals, boolean memSizeFormat)
          Pretty printer for a proportion: " a out of total = proportion" (where total=a+b).
static String memSizeFormat(long size)
          Nicely formats the memory size size, using normal units for memory size.
static String percentage(double a, double total)
          Returns a string representing the proportion a/total (in percents).
static void print_collection(Collection c, String c_name)
          Even more simplified version of print_collection.
static void print_collection(Collection c, String c_name, String indent)
          Simplified version of print_collection.
static void print_collection(Collection c, String c_name, String indent, PrintWriter pw)
          Pretty printer for collections.
static String print(Collection collection)
          Return a String representing the elements of collection in a human readable format.
static String proportion2str(long a, long b)
          Returns the string representation of the proportion a/b.
static HMethod quad2method(Quad q)
           
static
<T> T[]
safeCopy(ArrayFactory<T> factory, T[] src)
          Copy an array type to prevent modification.
static
<A,B> Set<A>
set_diff(Set<A> a, Set<B> b)
          Computes the difference of two sets: a-b.
static
<T> T[]
shrink(ArrayFactory<T> factory, T[] src, int n)
          Remove element 'n' from array 'src'.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Util

public Util()
Method Detail

genericFactory

public static final ArrayFactory genericFactory(Object[] src)
Deprecated. requires java language reflection.

Returns an ArrayFactory which makes an array of the same type as the src parameter (which should be an array of some object type). This method uses run-time type information and thus should not be used.


safeCopy

public static final <T> T[] safeCopy(ArrayFactory<T> factory,
                                     T[] src)
Copy an array type to prevent modification. Does not bother to copy array types of length 0, because they're already immutable. After code has been debugged, this method can skip the copy for efficiency.


copy

public static final <T> T[] copy(ArrayFactory<T> factory,
                                 T[] src)
Deprecated. The clone() method on arrays works much better, and should be used instead of this method.

Copy an array type to prevent modification.


shrink

public static final <T> T[] shrink(ArrayFactory<T> factory,
                                   T[] src,
                                   int n)
Remove element 'n' from array 'src'.


grow

public static final <T> T[] grow(ArrayFactory<T> factory,
                                 T[] src,
                                 T o,
                                 int n)
Insert element o before src[n].

After return, src[n]==o.


escape

public static final String escape(String str)
Escape the contents of a String so they are safe to print.


jasminEscape

public static final String jasminEscape(String str)
Escape the contents of a String so they are safe to print.


print

public static final String print(Collection collection)
Return a String representing the elements of collection in a human readable format.
effects: Iterates over collection, calling toString() on each element and appending the result to a String. The format of the returned String is { elem1, elem2, ... , elemN }

Returns:
A String representing collection.

print_collection

public static final void print_collection(Collection c,
                                          String c_name,
                                          String indent,
                                          PrintWriter pw)
Pretty printer for collections.

Parameters:
c - The Collection to be printed.
c_name - The name of the collection.
indent - String used for indentation (eg "\t".
pw - The PrintWriter used to do the printing itself. The collection is printed in the following format: the name of the collection followed by the collection size (in parans) and one opening curly bracet on the first line, the elements of the collection, one by one, each on a separate line and indented with a tab; on the last line, one closing curly bracet. In addition, each line is prefixed with the string indent.

print_collection

public static final void print_collection(Collection c,
                                          String c_name,
                                          String indent)
Simplified version of print_collection. Uses the default value new java.io.PrintWriter(System.out, true) for the pw parameter.


print_collection

public static final void print_collection(Collection c,
                                          String c_name)
Even more simplified version of print_collection. Uses the default value "" for indent and new java.io.PrintWriter(System.out, true) for the pw parameter.


set_diff

public static final <A,B> Set<A> set_diff(Set<A> a,
                                          Set<B> b)
Computes the difference of two sets: a-b.


adjust_quotes

public static final String adjust_quotes(String str)
Returns a string that is identical to str, except that every " character has been replaced with the sequence \". This is useful when we generate output for tools like VCG which expects the value of some attributes to be put between quotes (and of course any quote inside has to be given using an escape sequence).


convert

public static Object convert(Object obj,
                             Map map)
Converts an objects using the reflexive closure of a map.

Parameters:
obj - object to be converted
map - conversion map
Returns:
if obj is in the key set of map, returns map.get(obj). Otherwise, the method simply returns obj.

doubleRep

public static String doubleRep(double d,
                               int decimals)
Returns the string representation of the double d, with a certain number of decimals. Let me know if you know something better.


doubleRep

public static String doubleRep(double d,
                               int digits,
                               int decimals)
Returns the string representation of the double d, with a certain number of decimals. Let me know if you know something better.


percentage

public static String percentage(double a,
                                double total)
Returns a string representing the proportion a/total (in percents).


getLine

public static String getLine(HCodeElement q)
Returns the line of the instruction q in the format source_file:line_number.


code2str

public static String code2str(HCodeElement q)
Returns the string representation of the code instruction q in the formay: source_file:line_number instruction.


quad2method

public static HMethod quad2method(Quad q)

proportion2str

public static String proportion2str(long a,
                                    long b)
Returns the string representation of the proportion a/b. The proportion is printed as a percentage, with 2 decimals. Eg, 1/2 is presented as 50.00%.


longProportion

public static String longProportion(long a,
                                    long b,
                                    int digits,
                                    int decimals,
                                    boolean memSizeFormat)
Pretty printer for a proportion: " a out of total = proportion" (where total=a+b). If memSizeFormat is true, a and total are displayed as memory size (i.e., 2048 is 2K). digits and decimals indicates the formatting for the proportion.


longProportion

public static String longProportion(long a,
                                    long b,
                                    int digits,
                                    int decimals)
Convenient version of longProportion that uses null for the memSizeFormat.


memSizeFormat

public static String memSizeFormat(long size)
Nicely formats the memory size size, using normal units for memory size. For example, 2048 is printed as 2.00K. The biggest unit we support now is Tera.