Class Util

java.lang.Object
org.omegazero.common.util.Util

public final class Util extends Object
Class containing several mostly unrelated utility methods.
Since:
2.1
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Formats the current time in this format:
    (weekday) (month) DD YYYY HH:MM:SS.mmm
    where (weekday) is the three-letter shortened name of the current weekday (Mon, Tue, etc) and (month) the three-letter shortened name of the current month (Jan, Feb, etc).
    static boolean
    Checks if there is at least one non-daemon thread running.
    The caller thread and the "DestroyJavaVM" thread are excluded from this check.
    static void
    onClose(Runnable handler)
    Adds a shutdown hook which runs the given Runnable.
    static void
    onClose(Runnable handler, boolean waitForNonDaemon)
    Adds a shutdown hook which runs the given Runnable.
    static String
    randomHex(int length)
    Creates a string of the given length containing characters chosen pseudo-randomly from the set of characters used for hexadecimal encoding (0 - 9 and a - f).
    static List<String>
    splitQuotedString(String str, char delim, int max)
    Splits a string, possibly containing quoted substrings, on the given delimiter character.
    static void
    Blocks this thread until all non-daemon threads apart from the caller and "DestroyJavaVM" thread have exited (nonDaemonThreadRunning() returns false).
    static boolean
    Blocks this thread until all non-daemon threads apart from the caller and "DestroyJavaVM" thread have exited (nonDaemonThreadRunning() returns false), or at least timeout milliseconds have passed.

    Methods inherited from class java.lang.Object

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

    • getFormattedTime

      public static String getFormattedTime()
      Formats the current time in this format:
      (weekday) (month) DD YYYY HH:MM:SS.mmm
      where (weekday) is the three-letter shortened name of the current weekday (Mon, Tue, etc) and (month) the three-letter shortened name of the current month (Jan, Feb, etc).
      Returns:
      Formatted time
    • onClose

      public static void onClose(Runnable handler)
      Adds a shutdown hook which runs the given Runnable.
      Parameters:
      handler - The handler to run when the runtime is about to exit
      See Also:
    • onClose

      public static void onClose(Runnable handler, boolean waitForNonDaemon)
      Adds a shutdown hook which runs the given Runnable.
      Parameters:
      handler - The handler to run when the runtime is about to exit
      waitForNonDaemon - If true and the shutdown was triggered, handler will only be run if all other non-daemon threads have exited
    • nonDaemonThreadRunning

      public static boolean nonDaemonThreadRunning()
      Checks if there is at least one non-daemon thread running.
      The caller thread and the "DestroyJavaVM" thread are excluded from this check.
      Returns:
      true if there is at least one non-daemon thread running apart from the caller and "DestroyJavaVM" thread
    • waitForNonDaemonThreads

      public static void waitForNonDaemonThreads()
      Blocks this thread until all non-daemon threads apart from the caller and "DestroyJavaVM" thread have exited (nonDaemonThreadRunning() returns false).
      See Also:
    • waitForNonDaemonThreads

      public static boolean waitForNonDaemonThreads(int timeout)
      Blocks this thread until all non-daemon threads apart from the caller and "DestroyJavaVM" thread have exited (nonDaemonThreadRunning() returns false), or at least timeout milliseconds have passed.

      If the caller thread is interrupted while waiting, this method returns.

      Parameters:
      timeout - Maximum amount of time to wait for non-daemon threads to exit, in milliseconds. May be 0 to wait for an unlimited amount of time
      Returns:
      true if this function returns because all non-daemon threads have exited, or false if the timeout was exceeded or this thread was interrupted
      Implementation Note:
      The poll interval is 50 milliseconds, meaning this method may not return immediately after all said threads have exited.
    • randomHex

      public static String randomHex(int length)
      Creates a string of the given length containing characters chosen pseudo-randomly from the set of characters used for hexadecimal encoding (0 - 9 and a - f).
      Parameters:
      length - The length of the resulting string
      Returns:
      The string containing random hex characters
    • splitQuotedString

      public static List<String> splitQuotedString(String str, char delim, int max)
      Splits a string, possibly containing quoted substrings, on the given delimiter character.

      Substrings enclosed in double quotes ("") are treated as a single token in the output, and the quotes are removed. Otherwise, all string parts separated by the given delimiter are treated as a single token, and are put into the returned list as one element each. If a double quote character is preceded by a backslash, it is treated as a regular character.

      If the double quote character is given as the delimiter, the behavior is undefined.

      The max parameter works similar as in the String.split(String, int) method. The returned list is at most max elements long and any excess tokens are stored in the last element unchanged. max may be non-positive to return any amount of elements.

      Examples:

      
              splitQuotedString("\"aa \\\" bb\" cc dd", ' ', -1) returns [aa " bb, cc, dd]
              splitQuotedString("\"\" cc dd", ' ', -1) or splitQuotedString(" cc dd", ' ', -1) returns [, cc, dd]
              splitQuotedString("a a \"aa \\\" bb\" cc dd", ' ', 3) returns [a, a, aa " bb cc dd]
       
      Parameters:
      str - The string to split
      delim - The delimiter character
      max - If positive, the maximum number of individual tokens to return
      Returns:
      The list of separated tokens
      Since:
      2.12.1