Package io.deephaven.util
Class Utils
java.lang.Object
io.deephaven.util.Utils
Landing place for general purpose utility functions.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
static final String
static LogOutput.ObjFormatter<Object>
Append the non-null argument in the same format asmakeReferentDescription(Object)
. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
areFileLinesEqual
(Path fileA, Path fileB, int skipLines) Does a line by line comparison of two files to check if they are the same.static <T> T
require (o instanceof type)static Path
changeFileExtension
(Path path, String extension) Changes if a file extension on a Path.static URLClassLoader
static void
ensureClosed
(@NotNull AutoCloseable autoCloseable) Close anAutoCloseable
object and discard any exceptions.static LocalDateTime
static int
Get the major Java version (e.g.static Comparator<File>
getModifiedTimeComparator
(boolean descending) Get aComparator<File>
that treats its inputs as file names in the same directory basePath, and compares each file by its modified timestatic Comparator<String>
getModifiedTimeComparator
(File dir, boolean descending) Get aComparator<String>
that treats its inputs as file names in the same directory basePath, and compares each file by its modified timestatic String
getSimpleNameFor
(@NotNull Class<?> objectClass) Anonymous inner classes return "" as simple name.static String
getSimpleNameFor
(@NotNull Object o) Anonymous inner classes return "" as simple name.static boolean
isEmptyElement
(org.jdom2.Element elem, String... ignoredAttrs) Checks if anElement
is empty, ignoring the specified set of attributes.static String
makeReferentDescription
(@NotNull Object object) Describe the object in a standardized format without accessing its fields or otherwise risking interacting with partially-initialized state.static <T> void
reverseArraySubset
(T[] array, int start, int end) Reverse a subset of an array.static void
runWithoutInterruption
(Logger log, ThrowingRunnable<InterruptedException> thing, String name) Run something until it is successfully finished without an interrupted exception.static long
sleepIgnoringInterruptions
(long millisToSleep) Sleep, ignoring any interruptionsstatic RuntimeException
static void
Convert IOException to UncheckedIOException.static <T> T
Convert IOException to UncheckedIOException.static String
syntactic sugar to use empty strings instead of null.static org.jdom2.Element
unwrapElement
(@NotNull String wrapperName, @NotNull org.jdom2.Element parentElem) Get the single element that was wrapped by a previous call towrapElement(String, Element)
static org.jdom2.Element
wrapElement
(@NotNull String wrapperName, @NotNull org.jdom2.Element wrapee) Wrap the specified element in a new one with the specified name
-
Field Details
-
MAC_OPTION_STRING
- See Also:
-
MAC_COMMAND_STRING
- See Also:
-
REFERENT_FORMATTER
Append the non-null argument in the same format asmakeReferentDescription(Object)
.
-
-
Constructor Details
-
Utils
public Utils()
-
-
Method Details
-
unCheck
Convert IOException to UncheckedIOException.- Parameters:
r
- the stuff to run
-
unCheck
Convert IOException to UncheckedIOException.- Parameters:
r
- the stuff to run- Returns:
- the result of the stuff
-
unNull
syntactic sugar to use empty strings instead of null.- Parameters:
s
- the string to check- Returns:
- the original string, or "" if it was null
-
runWithoutInterruption
public static void runWithoutInterruption(Logger log, ThrowingRunnable<InterruptedException> thing, String name) Run something until it is successfully finished without an interrupted exception.- Parameters:
thing
- the thing to run despite interruptions.name
- what to call the thing - for logging
-
sleepIgnoringInterruptions
public static long sleepIgnoringInterruptions(long millisToSleep) Sleep, ignoring any interruptions- Parameters:
millisToSleep
- millis to sleep- Returns:
- the number of millis slept
-
isEmptyElement
Checks if anElement
is empty, ignoring the specified set of attributes. An empty element contains no content and no attributes aside from those indicated in ignoredAttrs- Parameters:
elem
- The element to checkignoredAttrs
- A set of attributes that can be present while this element is still considered empty.- Returns:
- true if the element contained no content or attributes excluding those indicated in ignoredAttrs
-
wrapElement
public static org.jdom2.Element wrapElement(@NotNull @NotNull String wrapperName, @NotNull @NotNull org.jdom2.Element wrapee) Wrap the specified element in a new one with the specified name- Parameters:
wrapperName
- The name of the wrapper element to createwrapee
- The element being wrapped.- Returns:
- A new element with the specified name and element as it's content
-
unwrapElement
public static org.jdom2.Element unwrapElement(@NotNull @NotNull String wrapperName, @NotNull @NotNull org.jdom2.Element parentElem) Get the single element that was wrapped by a previous call towrapElement(String, Element)
- Parameters:
wrapperName
- The name of the wrapperparentElem
- The element containing the wrapper- Returns:
- The element that was rapped or null, if the wrapper was not found.
-
getLastModifiedTime
-
getModifiedTimeComparator
Get aComparator<String>
that treats its inputs as file names in the same directory basePath, and compares each file by its modified time- Parameters:
dir
- The root path in which both files reside.- Returns:
- A new
Comparator<String>
.
-
getModifiedTimeComparator
Get aComparator<File>
that treats its inputs as file names in the same directory basePath, and compares each file by its modified time- Returns:
- A new comparator.
-
sneakyThrow
-
cleanRoom
-
ensureClosed
Close anAutoCloseable
object and discard any exceptions.
NB: PerAutoCloseable.close()
, Note that unlike the close method of java.io.Closeable, this close method is not required to be idempotent. In other words, calling this close method more than once may have some visible side effect, unlike Closeable.close which is required to have no effect if called more than once. However, implementers of this interface are strongly encouraged to make their close methods idempotent.- Parameters:
autoCloseable
- The resource to close.
-
reverseArraySubset
public static <T> void reverseArraySubset(T[] array, int start, int end) Reverse a subset of an array.- Parameters:
array
- The array in questionstart
- The starting index to reverse (inclusive)end
- The ending index to reverse (inclusive)
-
areFileLinesEqual
Does a line by line comparison of two files to check if they are the same. Can skip the first N lines.- Parameters:
fileA
- the first filefileB
- the second fileskipLines
- how many lines to skip before comparing- Returns:
- true if contents are equal, false otherwise
-
changeFileExtension
Changes if a file extension on a Path. Assumes the current extension starts with a dot.- Parameters:
path
- the path to the file to changeextension
- the extension- Returns:
- a Path to a file with the new extension
-
getSimpleNameFor
Anonymous inner classes return "" as simple name. In most cases, we want the SimpleName to reflect the class being overridden.For example,
x = new SomeClass() { @Override ... }; String name = getSimpleNameFor(x); // returns "SomeClass"
- Parameters:
o
- the object used to get the class for which to return the SimpleName- Returns:
- The SimpleName of the object's class, or of its superclass
-
getSimpleNameFor
Anonymous inner classes return "" as simple name. In most cases, we want the SimpleName to reflect the class being overridden.- Parameters:
objectClass
- the class for which to return the SimpleName- Returns:
- The SimpleName of the class, or of its superclass
-
castTo
require (o instanceof type) -
makeReferentDescription
Describe the object in a standardized format without accessing its fields or otherwise risking interacting with partially-initialized state.- Parameters:
object
- The object- Returns:
- The description
-
getMajorJavaVersion
public static int getMajorJavaVersion()Get the major Java version (e.g. 8, 11). Throw an exception if it can't be determined, or if it isn't a Deephaven-supported version. Currently supported versions include:- 1.8 (returned as 8)
- 11
- Returns:
- the major Java version
-