edu.ksu.cis.projects.bogor.util
Class Util

java.lang.Object
  extended by edu.ksu.cis.projects.bogor.util.Util

public final class Util
extends Object

This class provides miscellaneous helper methods.

Version:
CVS $Revision: 1.15 $ $Date: 2005/05/26 18:23:12 $
Author:
Robby , Matt Hoosier

Constructor Summary
Util()
           
 
Method Summary
static boolean assertInstanceof(Collection<?> collection, Class c)
          Asserts elements of a collection as instanceof a class.
static boolean assertNonNull(Object[] array)
          Asserts non-null.
static boolean assertNonNull(Object[][] array)
          Asserts non-null.
static boolean checkInstanceof(Collection<?> collection, Class c)
          Deterines whether all elements of a collection belong to a certain class.
static boolean checkInstanceof(Object[] collection, Class c)
          Deterines whether all elements of an array belong to a certain class.
static int compare(double d1, double d2)
          Compare two doubles.
static int compare(float f1, float f2)
          Compare two floats.
static int compare(int i1, int i2)
          Compare two integers.
static int compare(long l1, long l2)
          Compare two longs.
static String decodeString(String s)
           
static String diff(String file1, String file2)
          Compare two files using diff.
static String encodeString(String s)
           
static String encodeXML(String s)
          Encode a string to XML string.
static int fact(int x)
           
static Class[] getInterfaces(Class c)
          Calculate the set of all interfaces extended by a particular interface.
static int search(int[] array, int key)
           
static
<T> T[]
sort(T[] elements)
           
static List<String> splitCommaSeparatedList(String input)
           
static String toString(Collection<?> collection, String indent)
          Pretty print a collection.
static String toString(int[] items)
          Pretty print a sorted sequence of integers.
static String toString(IntIntTable map, String indent)
          Pretty print an int to int map.
static
<V> String
toString(IntObjectTable<V> map, String indent)
          Pretty print an int to object map.
static String toString(Map<?,?> map, String indent)
          Pretty print a map.
static String toString(Object[] map, String indent)
          Pretty print an int to object map.
static
<K> String
toString(ObjectIntTable<K> map, Class<K> keyClass, String indent)
          Pretty print an object to int map.
static boolean warnAssert(PrintWriter pw, String text)
           
static int widthInBits(int n)
          Calculate how many bits wide a number is, i.e.
static int widthInBits(long n)
          Calculate how many bits wide a number is, i.e.
 
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

getInterfaces

public static Class[] getInterfaces(Class c)
Calculate the set of all interfaces extended by a particular interface.

Parameters:
c - class object for the leaf-level interface type
Returns:
the reflexive transitive closure of interfaces extended by c

assertInstanceof

public static boolean assertInstanceof(Collection<?> collection,
                                       Class c)
Asserts elements of a collection as instanceof a class.

Parameters:
collection - The collection whose elements to be asserted. Must be non-null.
c - The class/type of instanceof. Must be non-null.
Returns:
True.

assertNonNull

public static boolean assertNonNull(Object[] array)
Asserts non-null.

Parameters:
array - The array. Must be non-null. Each element must be non-null.
Returns:
True.

assertNonNull

public static boolean assertNonNull(Object[][] array)
Asserts non-null.

Parameters:
array - The array. Must be non-null. Each element must be non-null.
Returns:
True.

checkInstanceof

public static boolean checkInstanceof(Collection<?> collection,
                                      Class c)
Deterines whether all elements of a collection belong to a certain class.

Parameters:
collection - The collection whose elements should be tested. Must be non-null.
c - The class/type of instanceof. Must be non-null.
Returns:
True.

checkInstanceof

public static boolean checkInstanceof(Object[] collection,
                                      Class c)
Deterines whether all elements of an array belong to a certain class.

Parameters:
collection - The collection whose elements should be tested. Must be non-null.
c - The class/type of instanceof. Must be non-null.
Returns:
True.

compare

public static int compare(int i1,
                          int i2)
Compare two integers.

Parameters:
i1 - An integer.
i2 - Another integer.
Returns:
-1, if i1 < i2. 0, if i1 == i2. 1, otherwise.

compare

public static int compare(long l1,
                          long l2)
Compare two longs.

Parameters:
l1 - A long.
l2 - Another long.
Returns:
-1, if l1 < l2. 0, if l1 == l2. 1, otherwise.

compare

public static int compare(double d1,
                          double d2)
Compare two doubles.

Parameters:
d1 - A double.
d2 - Another double.
Returns:
0, if d1 <= d2 && d1 >= d2. -1, if d1 <= d2. 1, otherwise.

compare

public static int compare(float f1,
                          float f2)
Compare two floats.

Parameters:
f1 - A float.
f2 - Another float.
Returns:
0, if f1 <= f2 && f1 >= f2. -1, if f1 <= f2. 1, otherwise.

decodeString

public static String decodeString(String s)

diff

public static String diff(String file1,
                          String file2)
Compare two files using diff.

Parameters:
file1 - The first file. Non-null.
file2 - The second file. Non-null.
Returns:
Null, if they are equal. Otherwise, the result of diff.

encodeString

public static String encodeString(String s)

encodeXML

public static String encodeXML(String s)
Encode a string to XML string.

Parameters:
s - The string to be encoded.
Returns:
XML string.

fact

public static int fact(int x)

search

public static int search(int[] array,
                         int key)

sort

public static <T> T[] sort(T[] elements)

splitCommaSeparatedList

public static List<String> splitCommaSeparatedList(String input)

toString

public static String toString(Map<?,?> map,
                              String indent)
Pretty print a map.

Parameters:
map - The map to be pretty printed.
Returns:
The pretty string of the map.

toString

public static <K> String toString(ObjectIntTable<K> map,
                                  Class<K> keyClass,
                                  String indent)
Pretty print an object to int map.

Parameters:
map - The map to be pretty printed.
Returns:
The pretty string of the map.

toString

public static <V> String toString(IntObjectTable<V> map,
                                  String indent)
Pretty print an int to object map.

Parameters:
map - The map to be pretty printed.
Returns:
The pretty string of the map.

toString

public static String toString(IntIntTable map,
                              String indent)
Pretty print an int to int map.

Parameters:
map - The map to be pretty printed.
Returns:
The pretty string of the map.

toString

public static String toString(Object[] map,
                              String indent)
Pretty print an int to object map.

Parameters:
map - The map to be pretty printed.
Returns:
The pretty string of the map.

toString

public static String toString(int[] items)
Pretty print a sorted sequence of integers.

Parameters:
map - The map to be pretty printed.
Returns:
The pretty string of the map.

toString

public static String toString(Collection<?> collection,
                              String indent)
Pretty print a collection.

Parameters:
collection - The collection to be pretty printed.
Returns:
The pretty string of the collection.

warnAssert

public static boolean warnAssert(PrintWriter pw,
                                 String text)

widthInBits

public static int widthInBits(int n)
Calculate how many bits wide a number is, i.e. position of highest 1 bit. Fully unraveled binary search method.

Parameters:
n - The number to check bits width on. (author Dirk Bosmans Dirk.Bosmans@tijd.com)
Returns:
p, where 2**p is first power of two >= n. e.g. binary 0001_0101 -> 5, 0xffffffff -> 32, 0 -> 0, 1 -> 1, 2 -> 2, 3 -> 2, 4 -> 3

widthInBits

public static int widthInBits(long n)
Calculate how many bits wide a number is, i.e. position of highest 1 bit. Fully unraveled binary search method.

Parameters:
n - The number to check bits width on.
Returns:
p, where 2**p is first power of two >= n. e.g. binary 0001_0101 -> 5, 0xffffffff -> 32, 0 -> 0, 1 -> 1, 2 -> 2, 3 -> 2, 4 -> 3