Class Util
- Since:
- 2.1
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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
Adds a shutdown hook which runs the givenRunnable
.static void
Adds a shutdown hook which runs the givenRunnable
.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
anda - f
).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
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.
-
Method Details
-
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
Adds a shutdown hook which runs the givenRunnable
.- Parameters:
handler
- The handler to run when the runtime is about to exit- See Also:
-
onClose
Adds a shutdown hook which runs the givenRunnable
.- Parameters:
handler
- The handler to run when the runtime is about to exitwaitForNonDaemon
- 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
Creates a string of the given length containing characters chosen pseudo-randomly from the set of characters used for hexadecimal encoding (0 - 9
anda - f
).- Parameters:
length
- The length of the resulting string- Returns:
- The string containing random hex characters
-
splitQuotedString
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 theString.split(String, int)
method. The returned list is at mostmax
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 splitdelim
- The delimiter charactermax
- If positive, the maximum number of individual tokens to return- Returns:
- The list of separated tokens
- Since:
- 2.12.1
-