Class Utils

java.lang.Object
io.deephaven.util.Utils

public class Utils extends Object
Landing place for general purpose utility functions.
  • Field Details

  • Constructor Details

    • Utils

      public Utils()
  • Method Details

    • unCheck

      public static void unCheck(ThrowingRunnable<IOException> r)
      Convert IOException to UncheckedIOException.
      Parameters:
      r - the stuff to run
    • unCheck

      public static <T> T unCheck(ThrowingSupplier<T,IOException> r)
      Convert IOException to UncheckedIOException.
      Parameters:
      r - the stuff to run
      Returns:
      the result of the stuff
    • unNull

      public static String unNull(String s)
      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

      public static boolean isEmptyElement(org.jdom2.Element elem, String... ignoredAttrs)
      Checks if an Element 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 check
      ignoredAttrs - 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 create
      wrapee - 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 to wrapElement(String, Element)
      Parameters:
      wrapperName - The name of the wrapper
      parentElem - The element containing the wrapper
      Returns:
      The element that was rapped or null, if the wrapper was not found.
    • getLastModifiedTime

      public static LocalDateTime getLastModifiedTime(File f)
    • getModifiedTimeComparator

      public static Comparator<String> getModifiedTimeComparator(File dir, boolean descending)
      Get a Comparator<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

      public static Comparator<File> getModifiedTimeComparator(boolean descending)
      Get a Comparator<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

      public static RuntimeException sneakyThrow(Throwable t)
    • cleanRoom

      public static URLClassLoader cleanRoom()
    • ensureClosed

      public static void ensureClosed(@NotNull @NotNull AutoCloseable autoCloseable)
      Close an AutoCloseable object and discard any exceptions.
      NB: Per AutoCloseable.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 question
      start - The starting index to reverse (inclusive)
      end - The ending index to reverse (inclusive)
    • areFileLinesEqual

      public 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. Can skip the first N lines.
      Parameters:
      fileA - the first file
      fileB - the second file
      skipLines - how many lines to skip before comparing
      Returns:
      true if contents are equal, false otherwise
    • changeFileExtension

      public static Path changeFileExtension(Path path, String extension)
      Changes if a file extension on a Path. Assumes the current extension starts with a dot.
      Parameters:
      path - the path to the file to change
      extension - the extension
      Returns:
      a Path to a file with the new extension
    • getSimpleNameFor

      public static String getSimpleNameFor(@NotNull @NotNull Object o)
      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

      public static String getSimpleNameFor(@NotNull @NotNull Class<?> objectClass)
      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

      public static <T> T castTo(Object o, String name, Class<T> type)
      require (o instanceof type)
    • makeReferentDescription

      public static String makeReferentDescription(@NotNull @NotNull Object object)
      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