daikon.util
Class UtilMDE

Object
  extended by UtilMDE

public final class UtilMDE
extends Object

Utility functions that do not belong elsewhere in the plume package.


Nested Class Summary
static class UtilMDE.EnumerationIterator<T>
          Converts an Enumeration into an Iterator.
static class UtilMDE.FilteredIterator<T>
           
static class UtilMDE.IteratorEnumeration<T>
          Converts an Iterator into an Enumeration.
static class UtilMDE.MergedIterator<T>
          An Iterator that returns the elements in each of its argument Iterators, in turn.
static class UtilMDE.MergedIterator2<T>
          An Iterator that returns first the elements returned by its first argument, then the elements returned by its second argument.
static class UtilMDE.NullableStringComparator
           
static class UtilMDE.RemoveFirstAndLastIterator<T>
          Returns an iterator just like its argument, except that the first and last elements are removed.
static class UtilMDE.WildcardFilter
           
 
Method Summary
static void addToClasspath(String dir)
          Add the directory to the system classpath.
static String appendProperty(Properties p, String key, String value)
          Set the property to its previous value concatenated to the given value.
static String arglistFromJvm(String arglist)
          Convert an argument list from JVML format to Java format.
static String arglistToJvm(String arglist)
          Convert a fully-qualified argument list from Java format to JVML format.
static String backticks(List<String> command)
          Execute the given command, and return all its output as a string.
static String backticks(String... command)
          Execute the given command, and return all its output as a string.
static String backTrace()
          Deprecated. use "backtrace(new Throwable())" instead, to avoid spurious "at plume.UtilMDE.backTrace(UtilMDE.java:1491)" in output.
static String backTrace(Throwable t)
          For the current backtrace, do "backtrace(new Throwable())".
static BufferedReader bufferedFileReader(File file)
          Returns a BufferedReader for the file, accounting for the possibility that the file is compressed.
static BufferedReader bufferedFileReader(File file, String charsetName)
          Returns a BufferedReader for the file, accounting for the possibility that the file is compressed.
static BufferedReader bufferedFileReader(String filename)
          Returns a BufferedReader for the file, accounting for the possibility that the file is compressed.
static BufferedReader BufferedFileReader(String filename)
          Deprecated. use bufferedFileReader (note lowercase first letter)
static BufferedReader bufferedFileReader(String filename, String charsetName)
          Returns a BufferedReader for the file, accounting for the possibility that the file is compressed.
static BufferedWriter bufferedFileWriter(String filename)
          Returns a BufferedWriter for the file, accounting for the possibility that the file is compressed.
static BufferedWriter BufferedFileWriter(String filename)
          Deprecated. use bufferedFileWriter (note lowercase first letter)
static BufferedWriter bufferedFileWriter(String filename, boolean append)
          Returns a BufferedWriter for the file, accounting for the possibility that the file is compressed.
static BufferedWriter BufferedFileWriter(String filename, boolean append)
          Deprecated. use bufferedFileWriter (note lowercase first letter)
static boolean canCreateAndWrite(File file)
          Returns true if the file exists and is writable, or if the file can be created.
static Class<?> classForName(String className)
          Like @link{Class.forName(String)}, but works when the string represents a primitive type, too.
static String classnameFromJvm(String classname)
          Convert a classname from JVML format to Java format.
static String classnameToJvm(String classname)
          Convert a fully-qualified classname from Java format to JVML format.
static long count_lines(String filename)
          Count the number of lines in the specified file
static int count(String s, int ch)
          Return the number of times the character appears in the string.
static int count(String s, String sub)
          Return the number of times the second string appears in the first.
static ArrayList<ArrayList<Integer>> create_combinations(int arity, int start, int cnt)
          Returns a list of lists of each combination (with repetition, but not permutations) of integers from start to cnt (inclusive) over arity dimensions.
static
<T> List<List<T>>
create_combinations(int dims, int start, List<T> objs)
          Returns a list of lists of each combination (with repetition, but not permutations) of the specified objects starting at index start over dims dimensions, for dims > 0.
static File createTempDir(String prefix, String suffix)
          Creates an empty directory in the default temporary-file directory, using the given prefix and suffix to generate its name.
static void deleteDir(File dir)
          Deletes the directory at dirName and all its files.
static void deleteDir(String dirName)
          Deletes the directory at dirName and all its files.
static boolean equalFiles(String file1, String file2)
          Returns true iff files have the same contents.
static boolean equalFiles(String file1, String file2, boolean trimLines)
          Returns true iff files have the same contents.
static String escapeNonASCII(String orig)
          Escape unprintable characters in the target, following the usual Java backslash conventions, so that the result is sure to be printable ASCII.
static String escapeNonJava(Character ch)
           
static String escapeNonJava(String orig)
          Escape \, ", newline, and carriage-return characters in the target as \\, \\", \n, and \r; return a new string if any modifications were necessary.
static File expandFilename(File name)
          Does tilde expansion on a file name (to the user's home directory).
static String expandFilename(String name)
          Does tilde expansion on a file name (to the user's home directory).
static InputStream fileInputStream(File file)
          Returns an InputStream for the file, accounting for the possibility that the file is compressed.
static InputStreamReader fileReader(File file)
          Returns a Reader for the file, accounting for the possibility that the file is compressed.
static InputStreamReader fileReader(File file, String charsetName)
          Returns a Reader for the file, accounting for the possibility that the file is compressed.
static InputStreamReader fileReader(String filename)
          Returns a Reader for the file, accounting for the possibility that the file is compressed.
static Object getFromSet(Set<?> set, Object key)
          Returns the object in this set that is equal to key.
static int hash(double x)
           
static int hash(double[] a)
           
static int hash(double[] a, double[] b)
           
static int hash(double a, double b)
           
static int hash(double a, double b, double c)
           
static int hash(long l)
           
static int hash(long[] a)
           
static int hash(long[] a, long[] b)
           
static int hash(long a, long b)
           
static int hash(long a, long b, long c)
           
static int hash(String a)
           
static int hash(String[] a)
           
static int hash(String a, String b)
           
static int hash(String a, String b, String c)
           
static String human_readable(long val)
          Convert a number into an abbreviation such as "5.00K" for 5000 or "65.0M" for 65000000.
static
<T> Integer
incrementMap(Map<T,Integer> m, T key, int count)
          Increment the Integer which is indexed by key in the Map.
static String inferLineSeparator(File filename)
          Tries to infer the line separator used in a file.
static String inferLineSeparator(String filename)
          Tries to infer the line separator used in a file.
static int intersectionCardinality(BitSet a, BitSet b)
          Returns the cardinality of the intersection of the two BitSets.
static int intersectionCardinality(BitSet a, BitSet b, BitSet c)
          Returns the cardinality of the intersection of the three BitSets.
static boolean intersectionCardinalityAtLeast(BitSet a, BitSet b, BitSet c, int i)
          Returns true if the cardinality of the intersection of the two BitSets is at least the given value.
static boolean intersectionCardinalityAtLeast(BitSet a, BitSet b, int i)
          Returns true if the cardinality of the intersection of the two BitSets is at least the given value.
static String java_source(File name)
          Returns a string version of the name that can be used in Java source.
static String join(List<?> v, String delim)
          Concatenate the string representations of the objects, placing the delimiter between them.
static String join(Object[] a, String delim)
          Concatenate the string representations of the objects, placing the delimiter between them.
static String joinLines(List<String> v, String delim)
          Concatenate the string representations of the objects, placing the system-specific line separator between them.
static String joinLines(Object... a)
          Concatenate the string representations of the objects, placing the system-specific line separator between them.
static LineNumberReader lineNumberFileReader(File file)
          Returns a LineNumberReader for the file, accounting for the possibility that the file is compressed.
static LineNumberReader LineNumberFileReader(File file)
          Deprecated. use lineNumberFileReader (note lowercase first letter)
static LineNumberReader lineNumberFileReader(String filename)
          Returns a LineNumberReader for the file, accounting for the possibility that the file is compressed.
static LineNumberReader LineNumberFileReader(String filename)
          Deprecated. use lineNumberFileReader (note lowercase first letter)
static Class<?> loadClassFromFile(String className, String pathname)
           
static String lpad(String s, int length)
           
static
<T> Vector<T>
makeVector(Enumeration<T> e)
          Returns a vector containing the elements of the enumeration.
static
<K,V> void
mapToString(Appendable sb, Map<K,V> m, String linePrefix)
          Write a multi-line representation of the map into the given Appendable (e.g., a StringBuilder).
static
<K,V> String
mapToString(Map<K,V> m)
           
static Method methodForName(String method)
           
static Method methodForName(String classname, String methodname, Class<?>[] params)
           
static String nplural(int n, String noun)
           
static String patternQuote(String s)
          Returns a literal pattern String for the specified String.
static String primitive_name_to_jvm(String primitive_name)
          Convert a primitive java type name (e.g., "int", "double", etc.) to the single character JVM name (e.g., "I", "D", etc.).
static boolean propertyIsTrue(Properties p, String key)
          Determines whether a property has value "true", "yes", or "1".
static
<T> List<T>
randomElements(Iterator<T> itor, int num_elts)
          Return an List containing num_elts randomly chosen elements from the iterator, or all the elements of the iterator if there are fewer.
static
<T> List<T>
randomElements(Iterator<T> itor, int num_elts, Random random)
          Return an List containing num_elts randomly chosen elements from the iterator, or all the elements of the iterator if there are fewer.
static String readerContents(Reader r)
          Reads the entire contents of the reader and returns it as a string.
static String readFile(File file)
          Reads the entire contents of the file and returns it as a string.
static Object readObject(File file)
          Reads an Object from a File.
static
<T> List<T>
removeDuplicates(List<T> l)
          Returns a copy of the list with duplicates removed.
static String removeWhitespaceAfter(String arg, String delimiter)
          Remove all whitespace after instances of delimiter.
static String removeWhitespaceAround(String arg, String delimiter)
          Remove all whitespace before or after instances of delimiter.
static String removeWhitespaceBefore(String arg, String delimiter)
          Remove all whitespace before instances of delimiter.
static String replaceString(String target, String oldStr, String newStr)
          Return a new string which is the text of target with all instances of oldStr replaced by newStr.
static String rpad(double num, int length)
           
static String rpad(int num, int length)
           
static String rpad(String s, int length)
           
static String setDefault(Properties p, String key, String value)
          Deprecated. use setDefaultMaybe
static String setDefaultMaybe(Properties p, String key, String value)
          Set the property only if it was not previously set.
static void setFinalField(Object o, String fieldName, Object value)
          Sets the given field, which may be final.
static
<K extends Comparable<? super K>,V>
Collection<K>
sortedKeySet(Map<K,V> m)
          Returns a sorted version of m.keySet().
static
<K,V> Collection<K>
sortedKeySet(Map<K,V> m, Comparator<K> comparator)
          Returns a sorted version of m.keySet().
static
<T> List<T>
sortList(List<T> l, Comparator<? super T> c)
          Returns the sorted version of the list.
static String[] split(String s, char delim)
          Return an array of Strings representing the characters between successive instances of the delimiter character.
static String[] split(String s, String delim)
          Return an array of Strings representing the characters between successive instances of the delimiter String.
static String[] splitLines(String s)
          Return an array of Strings, one for each line in the argument.
static void streamCopy(InputStream from, OutputStream to)
          Copy the contents of the input stream to the output stream.
static String streamString(InputStream is)
          Return a String containing all the characters from the input stream.
static Vector<Object> tokens(String str)
          Return a Vector of the Strings returned by StringTokenizer.StringTokenizer(String) with the given arguments.
static Vector<Object> tokens(String str, String delim)
          Return a Vector of the Strings returned by StringTokenizer.StringTokenizer(String,String) with the given arguments.
static Vector<Object> tokens(String str, String delim, boolean returnTokens)
          Return a Vector of the Strings returned by StringTokenizer.StringTokenizer(String,String,boolean) with the given arguments.
static String unescapeNonJava(String orig)
          Replace "\\", "\"", "\n", and "\r" sequences by their one-character equivalents.
static String unqualified_name(Class<?> cls)
          Returns the simple unqualified class name that corresponds to the specified class.
static String unqualified_name(String qualified_name)
          Returns the simple unqualified class name that corresponds to the specified fully qualified name.
static void writeFile(File file, String contents)
          Creates a file with the given name and writes the specified string to it.
static void writeObject(Object o, File file)
          Writes an Object to a File.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

intersectionCardinalityAtLeast

public static boolean intersectionCardinalityAtLeast(BitSet a,
                                                     BitSet b,
                                                     int i)
Returns true if the cardinality of the intersection of the two BitSets is at least the given value.


intersectionCardinalityAtLeast

public static boolean intersectionCardinalityAtLeast(BitSet a,
                                                     BitSet b,
                                                     BitSet c,
                                                     int i)
Returns true if the cardinality of the intersection of the two BitSets is at least the given value.


intersectionCardinality

public static int intersectionCardinality(BitSet a,
                                          BitSet b)
Returns the cardinality of the intersection of the two BitSets.


intersectionCardinality

public static int intersectionCardinality(BitSet a,
                                          BitSet b,
                                          BitSet c)
Returns the cardinality of the intersection of the three BitSets.


fileInputStream

public static InputStream fileInputStream(File file)
                                   throws IOException
Returns an InputStream for the file, accounting for the possibility that the file is compressed. (A file whose name ends with ".gz" is treated as compressed.)

Warning: The "gzip" program writes and reads files containing concatenated gzip files. As of Java 1.4, Java reads just the first one: it silently discards all characters (including gzipped files) after the first gzipped file.

Throws:
IOException

fileReader

public static InputStreamReader fileReader(String filename)
                                    throws FileNotFoundException,
                                           IOException
Returns a Reader for the file, accounting for the possibility that the file is compressed. (A file whose name ends with ".gz" is treated as compressed.)

Warning: The "gzip" program writes and reads files containing concatenated gzip files. As of Java 1.4, Java reads just the first one: it silently discards all characters (including gzipped files) after the first gzipped file.

Throws:
FileNotFoundException
IOException

fileReader

public static InputStreamReader fileReader(File file)
                                    throws FileNotFoundException,
                                           IOException
Returns a Reader for the file, accounting for the possibility that the file is compressed. (A file whose name ends with ".gz" is treated as compressed.)

Warning: The "gzip" program writes and reads files containing concatenated gzip files. As of Java 1.4, Java reads just the first one: it silently discards all characters (including gzipped files) after the first gzipped file.

Throws:
FileNotFoundException
IOException

fileReader

public static InputStreamReader fileReader(File file,
                                           String charsetName)
                                    throws FileNotFoundException,
                                           IOException
Returns a Reader for the file, accounting for the possibility that the file is compressed. (A file whose name ends with ".gz" is treated as compressed.)

Parameters:
charsetName - may be null, or the name of a Charset

Warning: The "gzip" program writes and reads files containing concatenated gzip files. As of Java 1.4, Java reads just the first one: it silently discards all characters (including gzipped files) after the first gzipped file.

Throws:
FileNotFoundException
IOException

bufferedFileReader

public static BufferedReader bufferedFileReader(String filename)
                                         throws FileNotFoundException,
                                                IOException
Returns a BufferedReader for the file, accounting for the possibility that the file is compressed. (A file whose name ends with ".gz" is treated as compressed.)

Warning: The "gzip" program writes and reads files containing concatenated gzip files. As of Java 1.4, Java reads just the first one: it silently discards all characters (including gzipped files) after the first gzipped file.

Throws:
FileNotFoundException
IOException

bufferedFileReader

public static BufferedReader bufferedFileReader(File file)
                                         throws FileNotFoundException,
                                                IOException
Returns a BufferedReader for the file, accounting for the possibility that the file is compressed. (A file whose name ends with ".gz" is treated as compressed.)

Warning: The "gzip" program writes and reads files containing concatenated gzip files. As of Java 1.4, Java reads just the first one: it silently discards all characters (including gzipped files) after the first gzipped file.

Throws:
FileNotFoundException
IOException

bufferedFileReader

public static BufferedReader bufferedFileReader(String filename,
                                                String charsetName)
                                         throws FileNotFoundException,
                                                IOException
Returns a BufferedReader for the file, accounting for the possibility that the file is compressed. (A file whose name ends with ".gz" is treated as compressed.)

Warning: The "gzip" program writes and reads files containing concatenated gzip files. As of Java 1.4, Java reads just the first one: it silently discards all characters (including gzipped files) after the first gzipped file.

Throws:
FileNotFoundException
IOException

bufferedFileReader

public static BufferedReader bufferedFileReader(File file,
                                                String charsetName)
                                         throws FileNotFoundException,
                                                IOException
Returns a BufferedReader for the file, accounting for the possibility that the file is compressed. (A file whose name ends with ".gz" is treated as compressed.)

Warning: The "gzip" program writes and reads files containing concatenated gzip files. As of Java 1.4, Java reads just the first one: it silently discards all characters (including gzipped files) after the first gzipped file.

Throws:
FileNotFoundException
IOException

lineNumberFileReader

public static LineNumberReader lineNumberFileReader(String filename)
                                             throws FileNotFoundException,
                                                    IOException
Returns a LineNumberReader for the file, accounting for the possibility that the file is compressed. (A file whose name ends with ".gz" is treated as compressed.)

Warning: The "gzip" program writes and reads files containing concatenated gzip files. As of Java 1.4, Java reads just the first one: it silently discards all characters (including gzipped files) after the first gzipped file.

Throws:
FileNotFoundException
IOException

lineNumberFileReader

public static LineNumberReader lineNumberFileReader(File file)
                                             throws FileNotFoundException,
                                                    IOException
Returns a LineNumberReader for the file, accounting for the possibility that the file is compressed. (A file whose name ends with ".gz" is treated as compressed.)

Warning: The "gzip" program writes and reads files containing concatenated gzip files. As of Java 1.4, Java reads just the first one: it silently discards all characters (including gzipped files) after the first gzipped file.

Throws:
FileNotFoundException
IOException

bufferedFileWriter

public static BufferedWriter bufferedFileWriter(String filename)
                                         throws IOException
Returns a BufferedWriter for the file, accounting for the possibility that the file is compressed. (A file whose name ends with ".gz" is treated as compressed.)

Warning: The "gzip" program writes and reads files containing concatenated gzip files. As of Java 1.4, Java reads just the first one: it silently discards all characters (including gzipped files) after the first gzipped file.

Throws:
IOException

bufferedFileWriter

public static BufferedWriter bufferedFileWriter(String filename,
                                                boolean append)
                                         throws IOException
Returns a BufferedWriter for the file, accounting for the possibility that the file is compressed. (A file whose name ends with ".gz" is treated as compressed.)

Warning: The "gzip" program writes and reads files containing concatenated gzip files. As of Java 1.4, Java reads just the first one: it silently discards all characters (including gzipped files) after the first gzipped file.

Parameters:
append - if true, the resulting BufferedWriter appends to the end of the file instead of the beginning.
Throws:
IOException

BufferedFileReader

@Deprecated
public static BufferedReader BufferedFileReader(String filename)
                                         throws FileNotFoundException,
                                                IOException
Deprecated. use bufferedFileReader (note lowercase first letter)

Throws:
FileNotFoundException
IOException

LineNumberFileReader

@Deprecated
public static LineNumberReader LineNumberFileReader(String filename)
                                             throws FileNotFoundException,
                                                    IOException
Deprecated. use lineNumberFileReader (note lowercase first letter)

Throws:
FileNotFoundException
IOException

LineNumberFileReader

@Deprecated
public static LineNumberReader LineNumberFileReader(File file)
                                             throws FileNotFoundException,
                                                    IOException
Deprecated. use lineNumberFileReader (note lowercase first letter)

Throws:
FileNotFoundException
IOException

BufferedFileWriter

@Deprecated
public static BufferedWriter BufferedFileWriter(String filename)
                                         throws IOException
Deprecated. use bufferedFileWriter (note lowercase first letter)

Throws:
IOException

BufferedFileWriter

@Deprecated
public static BufferedWriter BufferedFileWriter(String filename,
                                                           boolean append)
                                         throws IOException
Deprecated. use bufferedFileWriter (note lowercase first letter)

Throws:
IOException

classForName

public static Class<?> classForName(String className)
                             throws ClassNotFoundException
Like @link{Class.forName(String)}, but works when the string represents a primitive type, too. If the original name can't be found, it also tries looking for the name with the last '.' changed to a dollar sign ($). This accounts for inner classes which are sometimes separated with '.'.

Throws:
ClassNotFoundException

classnameToJvm

public static String classnameToJvm(String classname)
Convert a fully-qualified classname from Java format to JVML format. For example, convert "java.lang.Object[]" to "[Ljava/lang/Object;".


primitive_name_to_jvm

public static String primitive_name_to_jvm(String primitive_name)
Convert a primitive java type name (e.g., "int", "double", etc.) to the single character JVM name (e.g., "I", "D", etc.). Throws IllegalArgumentException if primitive_name is not a valid name.


arglistToJvm

public static String arglistToJvm(String arglist)
Convert a fully-qualified argument list from Java format to JVML format. For example, convert "(java.lang.Integer[], int, java.lang.Integer[][])" to "([Ljava/lang/Integer;I[[Ljava/lang/Integer;)".


classnameFromJvm

public static String classnameFromJvm(String classname)
Convert a classname from JVML format to Java format. For example, convert "[Ljava/lang/Object;" to "java.lang.Object[]".


arglistFromJvm

public static String arglistFromJvm(String arglist)
Convert an argument list from JVML format to Java format. For example, convert "([Ljava/lang/Integer;I[[Ljava/lang/Integer;)" to "(java.lang.Integer[], int, java.lang.Integer[][])".


loadClassFromFile

public static Class<?> loadClassFromFile(String className,
                                         String pathname)
                                  throws FileNotFoundException,
                                         IOException
Parameters:
pathname - the pathname of a .class file
Returns:
a Java Object corresponding to the Class defined in the .class file
Throws:
FileNotFoundException
IOException

addToClasspath

public static void addToClasspath(String dir)
Add the directory to the system classpath.


count_lines

public static long count_lines(String filename)
                        throws IOException
Count the number of lines in the specified file

Throws:
IOException

inferLineSeparator

public static String inferLineSeparator(String filename)
                                 throws IOException
Tries to infer the line separator used in a file.

Throws:
IOException

inferLineSeparator

public static String inferLineSeparator(File filename)
                                 throws IOException
Tries to infer the line separator used in a file.

Throws:
IOException

equalFiles

public static boolean equalFiles(String file1,
                                 String file2)
Returns true iff files have the same contents.


equalFiles

public static boolean equalFiles(String file1,
                                 String file2,
                                 boolean trimLines)
Returns true iff files have the same contents.

Parameters:
trimLines - if true, call String.trim on each line before comparing

canCreateAndWrite

public static boolean canCreateAndWrite(File file)
Returns true if the file exists and is writable, or if the file can be created.


createTempDir

public static File createTempDir(String prefix,
                                 String suffix)
                          throws IOException
Creates an empty directory in the default temporary-file directory, using the given prefix and suffix to generate its name. For example calling createTempDir("myPrefix", "mySuffix") will create the following directory: temporaryFileDirectory/myUserName/myPrefix_someString_suffix. someString is internally generated to ensure no temporary files of the same name are generated.

Parameters:
prefix - The prefix string to be used in generating the file's name; must be at least three characters long
suffix - The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used Returns: An abstract pathname denoting a newly-created empty file
Throws:
IllegalArgumentException - If the prefix argument contains fewer than three characters
IOException - If a file could not be created
SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method does not allow a file to be created
See Also:
File.createTempFile(String, String, File)

deleteDir

public static void deleteDir(String dirName)
Deletes the directory at dirName and all its files. Fails if dirName has any subdirectories.


deleteDir

public static void deleteDir(File dir)
Deletes the directory at dirName and all its files. Fails if dirName has any subdirectories.


expandFilename

public static File expandFilename(File name)
Does tilde expansion on a file name (to the user's home directory).


expandFilename

public static String expandFilename(String name)
Does tilde expansion on a file name (to the user's home directory).


java_source

public static String java_source(File name)
Returns a string version of the name that can be used in Java source. On Windows, the file will return a backslash separated string. Since backslash is an escape character, it must be quoted itself inside the string. The current implementation presumes that backslashes don't appear in filenames except as windows path separators. That seems like a reasonable assumption.


writeObject

public static void writeObject(Object o,
                               File file)
                        throws IOException
Writes an Object to a File.

Throws:
IOException

readObject

public static Object readObject(File file)
                         throws IOException,
                                ClassNotFoundException
Reads an Object from a File.

Throws:
IOException
ClassNotFoundException

readerContents

public static String readerContents(Reader r)
Reads the entire contents of the reader and returns it as a string. Any IOException encountered will be turned into an Error.


readFile

public static String readFile(File file)
Reads the entire contents of the file and returns it as a string. Any IOException encountered will be turned into an Error.


writeFile

public static void writeFile(File file,
                             String contents)
Creates a file with the given name and writes the specified string to it. If the file currently exists (and is writable) it is overwritten Any IOException encountered will be turned into an Error.


hash

public static final int hash(double x)

hash

public static final int hash(double a,
                             double b)

hash

public static final int hash(double a,
                             double b,
                             double c)

hash

public static final int hash(double[] a)

hash

public static final int hash(double[] a,
                             double[] b)

hash

public static final int hash(long l)

hash

public static final int hash(long a,
                             long b)

hash

public static final int hash(long a,
                             long b,
                             long c)

hash

public static final int hash(long[] a)

hash

public static final int hash(long[] a,
                             long[] b)

hash

public static final int hash(String a)

hash

public static final int hash(String a,
                             String b)

hash

public static final int hash(String a,
                             String b,
                             String c)

hash

public static final int hash(String[] a)

randomElements

public static <T> List<T> randomElements(Iterator<T> itor,
                                         int num_elts)
Return an List containing num_elts randomly chosen elements from the iterator, or all the elements of the iterator if there are fewer. It examines every element of the iterator, but does not keep them all in memory.


randomElements

public static <T> List<T> randomElements(Iterator<T> itor,
                                         int num_elts,
                                         Random random)
Return an List containing num_elts randomly chosen elements from the iterator, or all the elements of the iterator if there are fewer. It examines every element of the iterator, but does not keep them all in memory.


incrementMap

public static <T> Integer incrementMap(Map<T,Integer> m,
                                       T key,
                                       int count)
Increment the Integer which is indexed by key in the Map. If the key isn't in the Map, it is added. Throws an error if the key is in the Map but maps to a non-Integer.


mapToString

public static <K,V> String mapToString(Map<K,V> m)

mapToString

public static <K,V> void mapToString(Appendable sb,
                                     Map<K,V> m,
                                     String linePrefix)
Write a multi-line representation of the map into the given Appendable (e.g., a StringBuilder).


sortedKeySet

public static <K extends Comparable<? super K>,V> Collection<K> sortedKeySet(Map<K,V> m)
Returns a sorted version of m.keySet().


sortedKeySet

public static <K,V> Collection<K> sortedKeySet(Map<K,V> m,
                                               Comparator<K> comparator)
Returns a sorted version of m.keySet().


methodForName

public static Method methodForName(String method)
                            throws ClassNotFoundException,
                                   NoSuchMethodException,
                                   SecurityException
Throws:
ClassNotFoundException
NoSuchMethodException
SecurityException

methodForName

public static Method methodForName(String classname,
                                   String methodname,
                                   Class<?>[] params)
                            throws ClassNotFoundException,
                                   NoSuchMethodException,
                                   SecurityException
Throws:
ClassNotFoundException
NoSuchMethodException
SecurityException

backticks

public static String backticks(String... command)
Execute the given command, and return all its output as a string.


backticks

public static String backticks(List<String> command)
Execute the given command, and return all its output as a string.


propertyIsTrue

public static boolean propertyIsTrue(Properties p,
                                     String key)
Determines whether a property has value "true", "yes", or "1".

See Also:
Properties.getProperty(java.lang.String)

appendProperty

public static String appendProperty(Properties p,
                                    String key,
                                    String value)
Set the property to its previous value concatenated to the given value. Returns the previous value.

See Also:
Properties.getProperty(java.lang.String), Properties.setProperty(java.lang.String, java.lang.String)

setDefault

@Deprecated
public static String setDefault(Properties p,
                                           String key,
                                           String value)
Deprecated. use setDefaultMaybe

Set the property only if it was not previously set.

See Also:
Properties.getProperty(java.lang.String), Properties.setProperty(java.lang.String, java.lang.String)

setDefaultMaybe

public static String setDefaultMaybe(Properties p,
                                     String key,
                                     String value)
Set the property only if it was not previously set.

See Also:
Properties.getProperty(java.lang.String), Properties.setProperty(java.lang.String, java.lang.String)

patternQuote

public static String patternQuote(String s)
Returns a literal pattern String for the specified String.

This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern.

Metacharacters or escape sequences in the input sequence will be given no special meaning.

Parameters:
s - The string to be literalized
Returns:
A literal string replacement
Since:
1.5

setFinalField

public static void setFinalField(Object o,
                                 String fieldName,
                                 Object value)
                          throws NoSuchFieldException,
                                 IllegalAccessException
Sets the given field, which may be final. Intended for use in readObject and nowhere else!

Throws:
NoSuchFieldException
IllegalAccessException

getFromSet

public static Object getFromSet(Set<?> set,
                                Object key)
Returns the object in this set that is equal to key. The Set abstraction doesn't provide this; it only provides "contains". Returns null if the argument is null, or if it isn't in the set.


streamCopy

public static void streamCopy(InputStream from,
                              OutputStream to)
Copy the contents of the input stream to the output stream.


streamString

public static String streamString(InputStream is)
Return a String containing all the characters from the input stream.


replaceString

public static String replaceString(String target,
                                   String oldStr,
                                   String newStr)
Return a new string which is the text of target with all instances of oldStr replaced by newStr.


split

public static String[] split(String s,
                             char delim)
Return an array of Strings representing the characters between successive instances of the delimiter character. Always returns an array of length at least 1 (it might contain only the empty string).

See Also:
split(String s, String delim)

split

public static String[] split(String s,
                             String delim)
Return an array of Strings representing the characters between successive instances of the delimiter String. Always returns an array of length at least 1 (it might contain only the empty string).

See Also:
split(String s, char delim)

splitLines

public static String[] splitLines(String s)
Return an array of Strings, one for each line in the argument. Always returns an array of length at least 1 (it might contain only the empty string). All common line separators (cr, lf, cr-lf, or lf-cr) are supported. Note that a string that ends with a line separator will return an empty string as the last element of the array.

See Also:
split(String s, char delim)

join

public static String join(Object[] a,
                          String delim)
Concatenate the string representations of the objects, placing the delimiter between them.

See Also:
ArraysMDE.toString(int[])

joinLines

public static String joinLines(Object... a)
Concatenate the string representations of the objects, placing the system-specific line separator between them.

See Also:
ArraysMDE.toString(int[])

join

public static String join(List<?> v,
                          String delim)
Concatenate the string representations of the objects, placing the delimiter between them.

See Also:
AbstractCollection.toString()

joinLines

public static String joinLines(List<String> v,
                               String delim)
Concatenate the string representations of the objects, placing the system-specific line separator between them.

See Also:
AbstractCollection.toString()

escapeNonJava

public static String escapeNonJava(String orig)
Escape \, ", newline, and carriage-return characters in the target as \\, \\", \n, and \r; return a new string if any modifications were necessary. The intent is that by surrounding the return value with double quote marks, the result will be a Java string literal denoting the original string. Previously known as quote().


escapeNonJava

public static String escapeNonJava(Character ch)

escapeNonASCII

public static String escapeNonASCII(String orig)
Escape unprintable characters in the target, following the usual Java backslash conventions, so that the result is sure to be printable ASCII. Returns a new string.


unescapeNonJava

public static String unescapeNonJava(String orig)
Replace "\\", "\"", "\n", and "\r" sequences by their one-character equivalents. All other backslashes are removed (for instance, octal/hex escape sequences are not turned into their respective characters). This is the inverse operation of escapeNonJava(). Previously known as unquote().


removeWhitespaceAround

public static String removeWhitespaceAround(String arg,
                                            String delimiter)
Remove all whitespace before or after instances of delimiter.


removeWhitespaceAfter

public static String removeWhitespaceAfter(String arg,
                                           String delimiter)
Remove all whitespace after instances of delimiter.


removeWhitespaceBefore

public static String removeWhitespaceBefore(String arg,
                                            String delimiter)
Remove all whitespace before instances of delimiter.


nplural

public static String nplural(int n,
                             String noun)

lpad

public static String lpad(String s,
                          int length)

rpad

public static String rpad(String s,
                          int length)

rpad

public static String rpad(int num,
                          int length)

rpad

public static String rpad(double num,
                          int length)

count

public static int count(String s,
                        int ch)
Return the number of times the character appears in the string.


count

public static int count(String s,
                        String sub)
Return the number of times the second string appears in the first.


tokens

public static Vector<Object> tokens(String str,
                                    String delim,
                                    boolean returnTokens)
Return a Vector of the Strings returned by StringTokenizer.StringTokenizer(String,String,boolean) with the given arguments.

The static type is Vector because StringTokenizer extends Enumeration instead of Enumeration as it should (probably due to backward-compatibility).


tokens

public static Vector<Object> tokens(String str,
                                    String delim)
Return a Vector of the Strings returned by StringTokenizer.StringTokenizer(String,String) with the given arguments.


tokens

public static Vector<Object> tokens(String str)
Return a Vector of the Strings returned by StringTokenizer.StringTokenizer(String) with the given arguments.


backTrace

public static String backTrace(Throwable t)
For the current backtrace, do "backtrace(new Throwable())".


backTrace

@Deprecated
public static String backTrace()
Deprecated. use "backtrace(new Throwable())" instead, to avoid spurious "at plume.UtilMDE.backTrace(UtilMDE.java:1491)" in output.

See Also:
backTrace(Throwable)

sortList

public static <T> List<T> sortList(List<T> l,
                                   Comparator<? super T> c)
Returns the sorted version of the list. Does not alter the list. Simply calls Collections.sort(List, Comparator).


removeDuplicates

public static <T> List<T> removeDuplicates(List<T> l)
Returns a copy of the list with duplicates removed. Retains the original order.


makeVector

public static <T> Vector<T> makeVector(Enumeration<T> e)
Returns a vector containing the elements of the enumeration.


create_combinations

public static <T> List<List<T>> create_combinations(int dims,
                                                    int start,
                                                    List<T> objs)
Returns a list of lists of each combination (with repetition, but not permutations) of the specified objects starting at index start over dims dimensions, for dims > 0. For example, create_combinations (1, 0, {a, b, c}) returns: {a}, {b}, {c} And create_combinations (2, 0, {a, b, c}) returns: {a, a}, {a, b}, {a, c} {b, b}, {b, c}, {c, c}


create_combinations

public static ArrayList<ArrayList<Integer>> create_combinations(int arity,
                                                                int start,
                                                                int cnt)
Returns a list of lists of each combination (with repetition, but not permutations) of integers from start to cnt (inclusive) over arity dimensions. For example, create_combinations (1, 0, 2) returns: {0}, {1}, {2} And create_combinations (2, 0, 2) returns: {0, 0}, {0, 1}, {0, 2} {1, 1} {1, 2}, {2, 2}


unqualified_name

public static String unqualified_name(String qualified_name)
Returns the simple unqualified class name that corresponds to the specified fully qualified name. For example if qualified name is java.lang.String, String will be returned.


unqualified_name

public static String unqualified_name(Class<?> cls)
Returns the simple unqualified class name that corresponds to the specified class. For example if qualified name of the class is java.lang.String, String will be returned.


human_readable

public static String human_readable(long val)
Convert a number into an abbreviation such as "5.00K" for 5000 or "65.0M" for 65000000. K stands for 1000, not 1024; M stands for 1000000, not 1048576, etc. There are always exactly 3 decimal digits of precision in the result (counting both sides of the decimal point).