Package blbutil

Class Utilities

java.lang.Object
blbutil.Utilities

public class Utilities extends Object
Class Utilities contains miscellaneous static utility methods.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <E> HashMap<E,Integer>
    arrayToMap(E[] array)
    Returns a map from array element to array index
    static String
    commandLine(String program, String[] args)
    Returns a string representation of the command line arguments.
    static <E> int[][]
    commonIndices(E[] a1, E[] a2)
    Returns a two-dimensional array with indices of common array elements.
    static void
    Prints the specified string to the specified PrintWriter and to standard out.
    static void
    Prints the specified string to the specified PrintWriter and to standard out.
    static String
    elapsedNanos(long nanoseconds)
    Returns a string representation of the specified elapsed time in the format "H hours M minutes S seconds".
    static void
    Prints "Terminating program." to standard error and terminates the Java virtual machine.
    static void
    Prints the specified string to standard error and then terminates the Java virtual machine.
    static void
    Prints the specified exception, its stack trace and then terminates the Java virtual machine.
    static void
    exit(Throwable thr, String str)
    Prints the specified exception, its stack trace, and the specified string to standard error and then terminates the Java virtual machine.
    static Set<String>
    idSet(File file)
    Returns a set of identifiers found in a text file that has one identifier per line.
    static String
    Returns the current minutes and seconds as a string.
    static void
    Prints a summary of memory use at the time of method invocation to standard output.
    static void
    shuffle(int[] ia, int nElements, Random random)
    Shuffles the specified array so that a random set of ${code nElements} elements from the array are the first ${code nElements} elements.
    static void
    shuffle(int[] ia, Random random)
    Randomly shuffles the elements of the specified array.
    static String
    Returns the current local time as a string.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • commandLine

      public static String commandLine(String program, String[] args)
      Returns a string representation of the command line arguments. The exact details of the representation are unspecified and subject to change.
      Parameters:
      program - the name of the program's jar file.
      args - command line arguments.
      Returns:
      a string representation of the command line arguments.
    • printMemoryUse

      public static void printMemoryUse(String msg)
      Prints a summary of memory use at the time of method invocation to standard output.
      Parameters:
      msg - a string a message to be printed with the summary of memory use
    • timeStamp

      public static String timeStamp()
      Returns the current local time as a string. The exact details of the string representation are unspecified and subject to change.
      Returns:
      the current local time as a string.
    • minutesAndSeconds

      public static String minutesAndSeconds()
      Returns the current minutes and seconds as a string. The exact details of the string representation are unspecified and subject to change.
      Returns:
      the current local time as a string.
    • idSet

      public static Set<String> idSet(File file)

      Returns a set of identifiers found in a text file that has one identifier per line. The empty set is returned if file == null. Blank lines are ignored, and white-space that begins or ends a line is ignored.

      If an IOException is thrown, an error message is printed to standard error and the Java virtual machine is forced to terminate.
      Parameters:
      file - a text file with one identifier per line
      Returns:
      a set of identifiers
      Throws:
      IllegalArgumentException - if the specified file does not exist
      IllegalArgumentException - if the specified file is a directory
      IllegalArgumentException - if any line of the specified file contains two non-white-space characters separated by one or more white-space characters
    • duoPrint

      public static void duoPrint(PrintWriter out, CharSequence s)
      Prints the specified string to the specified PrintWriter and to standard out. The line separator string is not appended to the specified string before printing.
      Parameters:
      out - a print writer
      s - a string to be printed
      Throws:
      NullPointerException - if out == null
    • duoPrintln

      public static void duoPrintln(PrintWriter out, CharSequence s)
      Prints the specified string to the specified PrintWriter and to standard out. The line separator string is appended to the specified string before printing.
      Parameters:
      out - a print writer
      s - a string to be printed
      Throws:
      NullPointerException - if out == null
    • elapsedNanos

      public static String elapsedNanos(long nanoseconds)
      Returns a string representation of the specified elapsed time in the format "H hours M minutes S seconds".
      Parameters:
      nanoseconds - the elapsed time in nanoseconds
      Returns:
      a string representation of the specified elapsed time
    • exit

      public static void exit(Throwable thr, String str)
      Prints the specified exception, its stack trace, and the specified string to standard error and then terminates the Java virtual machine.
      Parameters:
      thr - an exception or error to be printed to standard error
      str - a string to be printed to standard error
      Throws:
      NullPointerException - if t == null
    • exit

      public static void exit(Throwable t)
      Prints the specified exception, its stack trace and then terminates the Java virtual machine.
      Parameters:
      t - an exception or error to be printed to standard error
      Throws:
      NullPointerException - if e == null
    • exit

      public static void exit(String s)
      Prints the specified string to standard error and then terminates the Java virtual machine.
      Parameters:
      s - a string to be written to standard error
    • exit

      public static void exit()
      Prints "Terminating program." to standard error and terminates the Java virtual machine.
    • shuffle

      public static void shuffle(int[] ia, Random random)
      Randomly shuffles the elements of the specified array.
      Parameters:
      ia - an array to be shuffled
      random - a random number generator
      Throws:
      NullPointerException - if ia == null || random == null
    • shuffle

      public static void shuffle(int[] ia, int nElements, Random random)
      Shuffles the specified array so that a random set of ${code nElements} elements from the array are the first ${code nElements} elements. The specified array is unchanged if nElements <= 0.
      Parameters:
      ia - an array to be shuffled
      nElements - the size of the random set of elements which are shuffled to the beginning of the array
      random - a random number generator
      Throws:
      IndexOutOfBoundsException - if nElements > ia.length
      NullPointerException - if ia == null || random == null
    • commonIndices

      public static <E> int[][] commonIndices(E[] a1, E[] a2)
      Returns a two-dimensional array with indices of common array elements. If there are n elements in common between the a1 and a2 arrays, then for j satisfying (0 <= j && j < n), the [0][j] and P[1][j] elements of the returned array are the indices of the j-th common element in the a1 and a2 arrays respectively. Common elements are indexed according to their order in the a1 array.
      Type Parameters:
      E - the type of array element
      Parameters:
      a1 - an array of elements
      a2 - an array of elements
      Returns:
      a two-dimensional array with indicies of common array elements
      Throws:
      IllegalArgumentException - if the a1 or a2 arrays have a duplicate element
      NullPointerException - if a1 == null || a2 == null
    • arrayToMap

      public static <E> HashMap<E,Integer> arrayToMap(E[] array)
      Returns a map from array element to array index
      Type Parameters:
      E - the type of array element
      Parameters:
      array - an array
      Returns:
      a map from array element to array index
      Throws:
      IllegalArgumentException - if the array has a duplicate element
      NullPointerException - if array == null