com.myjavatools.lib
Class Tools

java.lang.Object
  extended by com.myjavatools.lib.Tools

public abstract class Tools
extends java.lang.Object


Constructor Summary
Tools()
           
 
Method Summary
static boolean bark(java.lang.String msg)
          Displays an error message
static java.lang.String commandLineArg(java.lang.String arg)
          Wraps command line argument into quotes if it contains spaces
static void fatalError(boolean condition, java.lang.String message)
          Reports a fatal error to sderr and exits, upon a condition
static void fatalError(java.lang.Exception exception)
          Reports a fatal exception to stderr and exits
static void fatalError(java.lang.String message)
          Reports a fatal error to sderr and exits
static void fatalError(java.lang.String message, java.lang.Exception exception)
          Reports an error message and an exception to sderr and exits
static java.lang.String freeMemory()
          Collects garbage, reports back
static java.lang.Long[] gc()
          Collects garbage, reports back
static java.lang.String getCommandOutput(java.lang.String command)
          returns as string the output of a command
static java.util.Map<java.lang.String,java.lang.String> getEnv()
          gets values of all environment variables
static boolean inform(java.lang.String msg)
          Displays an informative message
static boolean runCommand(java.lang.String cmd)
          Runs a command in current directory
static boolean runCommand(java.lang.String cmd, java.io.InputStream in, java.io.PrintStream out, java.io.PrintStream err)
          Runs a command in current directory
static boolean runCommand(java.lang.String cmd, java.io.PrintStream out, java.io.PrintStream err)
          Runs a command in current directory
static boolean runCommand(java.lang.String cmd, java.lang.String dir)
          Runs a command in specified directory
static boolean runCommand(java.lang.String cmd, java.lang.String dir, java.io.InputStream in, java.io.PrintStream out, java.io.PrintStream err)
          Runs a command in specified directory
static boolean runCommand(java.lang.String cmd, java.lang.String dir, java.io.PrintStream out, java.io.PrintStream err)
          Runs a command in specified directory
static boolean runCommand(java.lang.String cmd, java.lang.String dir, java.io.Reader in, java.io.PrintWriter out, java.io.PrintWriter err)
          Runs a command in specified directory
static boolean runCommand(java.lang.String cmd, java.lang.String dir, java.io.Reader in, java.io.PrintWriter out, java.io.PrintWriter err, java.util.Map<java.lang.String,java.lang.String> environment)
           
static int setLowestPriority()
          sets the lowest priority, Thread.MIN_PRIORITY
static int setPriority(int priority)
          sets priority and returns previous priority
static boolean whether(java.lang.String msg)
          Displays a message and receives user's choice (yes/no)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Tools

public Tools()
Method Detail

fatalError

public static final void fatalError(boolean condition,
                                    java.lang.String message)
Reports a fatal error to sderr and exits, upon a condition

Parameters:
condition - it is fatal error when condition is true
message - the error message

Example:

fatalError(!file.exists(), "File " + file + " does not exist!")


fatalError

public static final void fatalError(java.lang.String message)
Reports a fatal error to sderr and exits

Parameters:
message - the error message

Example:

fatalError("Your System is Windows!")


fatalError

public static final void fatalError(java.lang.Exception exception)
Reports a fatal exception to stderr and exits

Parameters:
exception - the fatal exception

Example:

try {
  String s = null;
  s.toString();
} catch (Exception e) {
  fatalError(e);
}
prints

java.lang.NullPointerException
        at com.myjavatools.util.Tools.main


fatalError

public static final void fatalError(java.lang.String message,
                                    java.lang.Exception exception)
Reports an error message and an exception to sderr and exits

Parameters:
message - the error message
exception - the fatal exception

Example:

try {
  String s = null;
  s.toString();
} catch (Exception e) {
  fatalError("Null pointers are bad!", e);
}
prints

Null pointers are bad!
java.lang.NullPointerException
        at com.myjavatools.util.Tools.main


bark

public static boolean bark(java.lang.String msg)
Displays an error message

Parameters:
msg - the message to display
Returns:
always false

Example:
  • bark("Code Red Alert!").

  • inform

    public static boolean inform(java.lang.String msg)
    Displays an informative message

    Parameters:
    msg - the message to display
    Returns:
    always false

    Example:
  • inform("To beguile the time, be like the time.").

  • whether

    public static boolean whether(java.lang.String msg)
    Displays a message and receives user's choice (yes/no)

    Parameters:
    msg - the messge to display
    Returns:
    true if user clicked "Yes", otherwise returns "No"

    Example:
  • if (!whether("Want this program to proceed?")) System.exit(1);

  • gc

    public static java.lang.Long[] gc()
    Collects garbage, reports back

    Returns:
    an array of three Longs, containing total memory, free memory before and after gc

    freeMemory

    public static java.lang.String freeMemory()
    Collects garbage, reports back

    Returns:
    a string with memory usage report

    getEnv

    public static java.util.Map<java.lang.String,java.lang.String> getEnv()
    gets values of all environment variables

    Returns:
    Map The returned map contains environment variable names as keys and their values as values.

    runCommand

    public static boolean runCommand(java.lang.String cmd)
    Runs a command in current directory

    Parameters:
    cmd - the command to run; append '&' if no need to wait
    Returns:
    false if return code is not 0

    Examples:
  • runCommand("notepad&")> launches Notepad
  • runCommand("rmdir xxx")> returns false (unless you had a directory 'xxx' and it was just removed).

  • runCommand

    public static boolean runCommand(java.lang.String cmd,
                                     java.lang.String dir)
    Runs a command in specified directory

    Parameters:
    cmd - the command to run; append '&' if no need to wait
    dir - starting directory name
    Returns:
    false if return code is not 0

    Example:
  • runCommand("cmd /c dir .", "C:\\Program Files")>.

  • runCommand

    public static boolean runCommand(java.lang.String cmd,
                                     java.io.PrintStream out,
                                     java.io.PrintStream err)
    Runs a command in current directory

    Parameters:
    cmd - the command to run; append '&' if no need to wait
    out - command output stream
    err - command error stream
    Returns:
    false if return code is not 0

    Example:
  • runCommand("cmd /c dir .", "C:\\Program Files", System.out, System.err)>.

  • runCommand

    public static boolean runCommand(java.lang.String cmd,
                                     java.lang.String dir,
                                     java.io.PrintStream out,
                                     java.io.PrintStream err)
    Runs a command in specified directory

    Parameters:
    cmd - the command to run; append '&' if no need to wait
    dir - starting directory name
    out - command output stream
    err - command error stream
    Returns:
    false if return code is not 0

    Example:
  • runCommand("cmd /c dir .", "C:\\Program Files", System.out, System.err)>.

  • runCommand

    public static boolean runCommand(java.lang.String cmd,
                                     java.io.InputStream in,
                                     java.io.PrintStream out,
                                     java.io.PrintStream err)
    Runs a command in current directory

    Parameters:
    cmd - the command to run; append '&' if no need to wait
    in - command input stream
    out - command output stream
    err - command error stream
    Returns:
    false if return code is not 0

    Example:
  • runCommand("cmd /c dir .", "C:\\Program Files", System.in, System.out, System.err)>.

  • runCommand

    public static boolean runCommand(java.lang.String cmd,
                                     java.lang.String dir,
                                     java.io.InputStream in,
                                     java.io.PrintStream out,
                                     java.io.PrintStream err)
    Runs a command in specified directory

    Parameters:
    cmd - the command to run; append '&' if no need to wait
    dir - starting directory name
    in - command input stream
    out - command output stream
    err - command error stream
    Returns:
    false if return code is not 0

    Example:
  • runCommand("cmd /c dir .", "C:\\Program Files", System.in, System.out, System.err)>.

  • runCommand

    public static boolean runCommand(java.lang.String cmd,
                                     java.lang.String dir,
                                     java.io.Reader in,
                                     java.io.PrintWriter out,
                                     java.io.PrintWriter err)
    Runs a command in specified directory

    Parameters:
    cmd - the command to run; append '&' if no need to wait
    dir - starting directory name
    in - command input reader
    out - command output writer
    err - command error writer
    Returns:
    false if return code is not 0

    Example:
  • runCommand("cmd /c dir .", "C:\\Program Files", System.in, System.out, System.err)>.

  • runCommand

    public static boolean runCommand(java.lang.String cmd,
                                     java.lang.String dir,
                                     java.io.Reader in,
                                     java.io.PrintWriter out,
                                     java.io.PrintWriter err,
                                     java.util.Map<java.lang.String,java.lang.String> environment)

    getCommandOutput

    public static java.lang.String getCommandOutput(java.lang.String command)
    returns as string the output of a command

    Parameters:
    command - String the command to run
    Returns:
    String command output (from STDOUT)

    commandLineArg

    public static java.lang.String commandLineArg(java.lang.String arg)
    Wraps command line argument into quotes if it contains spaces

    Parameters:
    arg -
    Returns:
    wrapped value

    setPriority

    public static int setPriority(int priority)
    sets priority and returns previous priority

    Parameters:
    priority - int
    Returns:
    int the priority that was before

    setLowestPriority

    public static int setLowestPriority()
    sets the lowest priority, Thread.MIN_PRIORITY

    Returns:
    int the priority that was before