chord.util
Class Utils

java.lang.Object
  extended by chord.util.Utils

public final class Utils
extends java.lang.Object

Commonly-used utilities.

Author:
Mayur Naik (mhn@cs.stanford.edu), Percy Liang (pliang@cs.berkeley.edu)

Field Summary
static java.lang.String LIST_SEPARATOR
           
static java.lang.String PATH_SEPARATOR
           
 
Method Summary
static
<S,T> void
add(java.util.Map<S,java.util.List<T>> map, S key1, T key2)
           
static boolean areEqual(java.lang.Object x, java.lang.Object y)
          Returns true if the given objects are equal, namely, they are both null or they are equal by the equals() method.
static java.lang.String[] concat(java.lang.String[] a, java.lang.String[] b)
          Create an array of strings by concatenating two given arrays of strings.
static java.lang.String concat(java.lang.String s1, java.lang.String sep, java.lang.String s2)
           
static
<T> boolean
contains(T[] array, T s)
          Determines whether a given array contains a given value.
static void copy(java.lang.String fromFileName, java.lang.String toFileName)
           
static void deleteFile(java.io.File file)
           
static void deleteFile(java.lang.String fileName)
           
static boolean exists(java.lang.String fileName)
           
static java.lang.String getAbsolutePath(java.lang.String parent, java.lang.String child)
           
static java.util.Set<java.lang.String> getClassNames(java.lang.String classPath)
           
static java.io.BufferedReader getResourceAsReader(java.lang.String resName)
           
static java.io.InputStream getResourceAsStream(java.lang.String resName)
           
static
<T> boolean
hasDuplicates(java.util.List<T> elements)
          Determines whether a given collection contains duplicate values.
static
<T> boolean
hasDuplicates(T[] array)
          Determines whether a given array contains duplicate values.
static boolean isSubclass(java.lang.Class subclass, java.lang.Class superclass)
          Determines whether a given class is a subclass of another.
static
<T> java.lang.String
join(java.util.List<T> objs, java.lang.String delim)
           
static
<T> java.lang.String
join(java.util.List<T> objs, java.lang.String delim, int start, int end)
           
static boolean mkdirs(java.io.File file)
           
static boolean mkdirs(java.lang.String dirName)
           
static boolean mkdirs(java.lang.String parentName, java.lang.String childName)
           
static java.io.PrintWriter openOut(java.lang.String path)
           
static java.io.PrintWriter openOutAppend(java.lang.String path)
           
static boolean prefixMatch(java.lang.String str, java.lang.String[] prefixes)
          Checks a string against a set of prefixes.
static java.util.List<java.lang.String> readFileToList(java.io.File file)
           
static void readFileToList(java.io.File file, java.util.List<java.lang.String> list)
           
static java.util.List<java.lang.String> readFileToList(java.lang.String fileName)
           
static void readFileToList(java.lang.String fileName, java.util.List<java.lang.String> list)
           
static IndexMap<java.lang.String> readFileToMap(java.io.File file)
           
static IndexMap<java.lang.String> readFileToMap(java.lang.String fileName)
           
static java.lang.Object readSerialFile(java.lang.String serialFileName)
           
static int[] samplePermutation(java.util.Random random, int n)
           
static java.lang.String[] toArray(java.lang.String str)
           
static java.util.List<java.lang.String> tokenize(java.lang.String s)
           
static
<T> java.lang.String
toString(java.util.Collection<T> a)
           
static
<T> java.lang.String
toString(java.util.Collection<T> c, java.lang.String prefix, java.lang.String sep, java.lang.String suffix)
          Provides a string representation of the elements in the given collection.
static
<T> java.lang.String
toString(T[] array)
          Provides a string representation of the elements in the given array.
static
<T> java.lang.String
toString(T[] array, java.lang.String prefix, java.lang.String sep, java.lang.String suffix)
          Provides a string representation of the elements in the given array.
static java.lang.String trimNumSuffix(java.lang.String s)
          Trim the numerical suffix of the given string.
static void writeListToFile(java.util.List<java.lang.String> list, java.io.File file)
           
static void writeListToFile(java.util.List<java.lang.String> list, java.lang.String fileName)
           
static void writeMapToFile(IndexMap<java.lang.String> map, java.io.File file)
           
static void writeMapToFile(IndexMap<java.lang.String> map, java.lang.String fileName)
           
static void writeSerialFile(java.lang.Object o, java.lang.String serialFileName)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LIST_SEPARATOR

public static final java.lang.String LIST_SEPARATOR
See Also:
Constant Field Values

PATH_SEPARATOR

public static final java.lang.String PATH_SEPARATOR
Method Detail

areEqual

public static boolean areEqual(java.lang.Object x,
                               java.lang.Object y)
Returns true if the given objects are equal, namely, they are both null or they are equal by the equals() method.

Parameters:
x - the first compared object.
y - the second compared object.
Returns:
true if the given objects are equal.

hasDuplicates

public static <T> boolean hasDuplicates(java.util.List<T> elements)
Determines whether a given collection contains duplicate values.

Type Parameters:
T - The type of the collection elements.
Parameters:
elements - A collection.
Returns:
true if the given collection contains duplicate values.

toString

public static <T> java.lang.String toString(java.util.Collection<T> c,
                                            java.lang.String prefix,
                                            java.lang.String sep,
                                            java.lang.String suffix)
Provides a string representation of the elements in the given collection.

Type Parameters:
T - The type of elements in the collection.
Parameters:
c - A collection of elements. It may be null.
prefix - String to be used as prefix.
sep - String to be used to separate elements in the collection.
suffix - String to be used as suffix.
Returns:
String representation of the elements in the collection.

toString

public static <T> java.lang.String toString(java.util.Collection<T> a)

toString

public static <T> java.lang.String toString(T[] array,
                                            java.lang.String prefix,
                                            java.lang.String sep,
                                            java.lang.String suffix)
Provides a string representation of the elements in the given array.

Type Parameters:
T - The type of array elements.
Parameters:
array - An array of elements. It may be null.
prefix - String to be used as prefix.
sep - String to be used to separate array elements.
suffix - String to be used as suffix.
Returns:
String representation of the elements in the array.

toString

public static <T> java.lang.String toString(T[] array)
Provides a string representation of the elements in the given array.

Type Parameters:
T - The type of the array elements.
Parameters:
array - an array of elements.
Returns:
string representation of elements in given array.

join

public static <T> java.lang.String join(java.util.List<T> objs,
                                        java.lang.String delim)

join

public static <T> java.lang.String join(java.util.List<T> objs,
                                        java.lang.String delim,
                                        int start,
                                        int end)

toArray

public static java.lang.String[] toArray(java.lang.String str)

concat

public static java.lang.String concat(java.lang.String s1,
                                      java.lang.String sep,
                                      java.lang.String s2)

samplePermutation

public static int[] samplePermutation(java.util.Random random,
                                      int n)

add

public static <S,T> void add(java.util.Map<S,java.util.List<T>> map,
                             S key1,
                             T key2)

prefixMatch

public static boolean prefixMatch(java.lang.String str,
                                  java.lang.String[] prefixes)
Checks a string against a set of prefixes. Returns true iff the string starts with one of the prefixes. Return false if prefixes is empty. Returns true if prefixes contains the empty string.

Parameters:
str - a string
prefixes - an array of prefixes
Returns:
true iff the string starts with one of the prefixes

isSubclass

public static boolean isSubclass(java.lang.Class subclass,
                                 java.lang.Class superclass)
Determines whether a given class is a subclass of another.

Parameters:
subclass - An intended subclass.
superclass - An intended superclass.
Returns:
true iff class subclass is a subclass of class superclass.

getResourceAsStream

public static java.io.InputStream getResourceAsStream(java.lang.String resName)

getResourceAsReader

public static java.io.BufferedReader getResourceAsReader(java.lang.String resName)

getClassNames

public static java.util.Set<java.lang.String> getClassNames(java.lang.String classPath)

trimNumSuffix

public static java.lang.String trimNumSuffix(java.lang.String s)
Trim the numerical suffix of the given string. For instance, converts "abc123xyz456" to "abc123xyz". If given string is empty, also returns an empty string.

Parameters:
s - The string whose numerical suffix is to be trimmed.
Returns:
A copy of the given string without any numerical suffix.
Throws:
java.lang.IllegalArgumentException - if s is null.

concat

public static java.lang.String[] concat(java.lang.String[] a,
                                        java.lang.String[] b)
Create an array of strings by concatenating two given arrays of strings.

Parameters:
a - the first array of strings.
b - the second array of strings.
Returns:
A new array of strings containing those in a followed by those in b.
Throws:
java.lang.IllegalArgumentException - if any of arguments is null.

tokenize

public static java.util.List<java.lang.String> tokenize(java.lang.String s)

contains

public static <T> boolean contains(T[] array,
                                   T s)
Determines whether a given array contains a given value.

Type Parameters:
T - The type of the array elements and the value to be checked for containment in the array.
Parameters:
array - An array.
s - A value to be checked for containment in the given array.
Returns:
true iff the given array contains the given value.

hasDuplicates

public static <T> boolean hasDuplicates(T[] array)
Determines whether a given array contains duplicate values.

Type Parameters:
T - The type of the array elements.
Parameters:
array - An array.
Returns:
true iff the given array contains duplicate values.
Throws:
java.lang.IllegalArgumentException - if array is null.

openOut

public static java.io.PrintWriter openOut(java.lang.String path)

openOutAppend

public static java.io.PrintWriter openOutAppend(java.lang.String path)

getAbsolutePath

public static java.lang.String getAbsolutePath(java.lang.String parent,
                                               java.lang.String child)

copy

public static void copy(java.lang.String fromFileName,
                        java.lang.String toFileName)

mkdirs

public static boolean mkdirs(java.lang.String dirName)

mkdirs

public static boolean mkdirs(java.lang.String parentName,
                             java.lang.String childName)

mkdirs

public static boolean mkdirs(java.io.File file)

readSerialFile

public static java.lang.Object readSerialFile(java.lang.String serialFileName)

writeSerialFile

public static void writeSerialFile(java.lang.Object o,
                                   java.lang.String serialFileName)

readFileToList

public static void readFileToList(java.lang.String fileName,
                                  java.util.List<java.lang.String> list)

readFileToList

public static void readFileToList(java.io.File file,
                                  java.util.List<java.lang.String> list)

readFileToList

public static java.util.List<java.lang.String> readFileToList(java.lang.String fileName)

readFileToList

public static java.util.List<java.lang.String> readFileToList(java.io.File file)

readFileToMap

public static IndexMap<java.lang.String> readFileToMap(java.lang.String fileName)

readFileToMap

public static IndexMap<java.lang.String> readFileToMap(java.io.File file)

writeListToFile

public static void writeListToFile(java.util.List<java.lang.String> list,
                                   java.lang.String fileName)

writeListToFile

public static void writeListToFile(java.util.List<java.lang.String> list,
                                   java.io.File file)

writeMapToFile

public static void writeMapToFile(IndexMap<java.lang.String> map,
                                  java.lang.String fileName)

writeMapToFile

public static void writeMapToFile(IndexMap<java.lang.String> map,
                                  java.io.File file)

deleteFile

public static void deleteFile(java.lang.String fileName)

deleteFile

public static void deleteFile(java.io.File file)

exists

public static boolean exists(java.lang.String fileName)