Package com.illumon.util
Class Utils
java.lang.Object
com.illumon.util.Utils
public class Utils extends Object
Landing place for general purpose utility functions.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Utils.Timer
Mark a start time, and allow logging of elapsed time and time between log messages (steps of a process). -
Field Summary
Fields Modifier and Type Field Description static String
MAC_COMMAND_STRING
static String
MAC_OPTION_STRING
static com.fishlib.base.log.LogOutput.ObjFormatter<Object>
REFERENT_FORMATTER
Append the non-null argument in the same format asmakeReferentDescription(Object)
. -
Constructor Summary
Constructors Constructor Description Utils()
-
Method Summary
Modifier and Type Method Description static 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
castTo(Object o, String name, Class<T> type)
require (o instanceof type)static <T> T
castTo(Object o, String name, Class<T> type, int numCallsBelowRequirer)
require (o instanceof type)static Path
changeFileExtension(Path path, String extension)
Changes if a file extension on a Path.static URLClassLoader
cleanRoom()
static void
ensureClosed(AutoCloseable autoCloseable)
Close anAutoCloseable
object and discard any exceptions.static boolean
fileDeletePrivileged(File file)
Delete a file in a privileged contextstatic boolean
fileExistsPrivileged(File file)
Perform a file existence check in a privileged context.static File
fileGetAbsoluteFilePrivileged(File file)
Get an absolute File in a privileged context.static String
fileGetAbsolutePathPrivileged(File file)
Get an absolute path in a privileged context.static DirectoryStream<Path>
fileGetDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter)
static boolean
fileIsDirectoryPrivileged(File file)
Perform an IsDirectory check in a privileged context.static boolean
fileIsDirectoryPrivileged(Path path, LinkOption... options)
Perform an IsDirectory check in a privileged context.static boolean
fileMkdirPrivileged(File file)
Create the directory specified by this File, excluding parent directories, in a privileged context.static boolean
fileRenameToPrivileged(File file, File dest)
Rename a file in a privileged context.static LocalDateTime
getLastModifiedTime(File f)
static int
getMajorJavaVersion()
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(Class<?> objectClass)
Anonymous inner classes return "" as simple name.static String
getSimpleNameFor(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(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(com.fishlib.io.logger.Logger log, com.fishlib.base.Procedure.ThrowingNullary<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
sneakyThrow(Throwable t)
static <T> T
unCheck(com.fishlib.base.Function.ThrowingNullary<T,IOException> r)
Convert IOException to UncheckedIOException.static void
unCheck(com.fishlib.base.Procedure.ThrowingNullary<IOException> r)
Convert IOException to UncheckedIOException.static String
unNull(String s)
syntactic sugar to use empty strings instead of null.static org.jdom2.Element
unwrapElement(String wrapperName, org.jdom2.Element parentElem)
Get the single element that was wrapped by a previous call towrapElement(String, Element)
static org.jdom2.Element
wrapElement(String wrapperName, org.jdom2.Element wrapee)
Wrap the specified element in a new one with the specified name
-
Field Details
-
MAC_OPTION_STRING
- See Also:
- Constant Field Values
-
MAC_COMMAND_STRING
- See Also:
- Constant Field Values
-
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(com.fishlib.io.logger.Logger log, com.fishlib.base.Procedure.ThrowingNullary<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 String wrapperName, @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 String wrapperName, @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) -
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
-
fileExistsPrivileged
Perform a file existence check in a privileged context.- Parameters:
file
- The File to check- Returns:
- same as File.exists
-
fileIsDirectoryPrivileged
Perform an IsDirectory check in a privileged context.- Parameters:
file
- the File to check- Returns:
- same as File.isDirectory
-
fileIsDirectoryPrivileged
Perform an IsDirectory check in a privileged context.- Parameters:
path
- the File to check- Returns:
- same as File.isDirectory
-
fileGetDirectoryStream
public static DirectoryStream<Path> fileGetDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException- Throws:
IOException
-
fileGetAbsoluteFilePrivileged
Get an absolute File in a privileged context.- Parameters:
file
- the File to check- Returns:
- same as File.getAbsoluteFile
-
fileGetAbsolutePathPrivileged
Get an absolute path in a privileged context.- Parameters:
file
- the File to check- Returns:
- same as File.getAbsolutePath
-
fileMkdirPrivileged
Create the directory specified by this File, excluding parent directories, in a privileged context.- Parameters:
file
- The directory to create- Returns:
- same as File.mkdir
-
fileRenameToPrivileged
Rename a file in a privileged context.- Parameters:
file
- the File to renamedest
- the new name- Returns:
- same as File.renameTo
-
fileDeletePrivileged
Delete a file in a privileged context- Parameters:
file
- The File to delete- Returns:
- same as File.delete
-