Class Util


  • public class Util
    extends Object
    Class with some miscellaneous utility methods.
    Author:
    Herbert Praehofer
    • Constructor Detail

      • Util

        public Util()
    • Method Detail

      • lookup

        public static <A,​B> Optional<B> lookup​(Map<A,​B> map,
                                                     A key)
        Returns an optional value from getting a value from a map based on a key. If no value exists in the map for the key, an empty optional is returned.
        Type Parameters:
        A - the type of the key
        B - the type of the value
        Parameters:
        map - the map
        key - the key
        Returns:
        an optional value with an empty optional if no value exists for the key
      • optional

        public static <R> Optional<R> optional​(Callable<R> callable)
        Runs the given callable and returns the value wrapped in an optional. If the callable throws and exception, an empty optional is returned.
        Type Parameters:
        R - the result value
        Parameters:
        callable - the callable to execute
        Returns:
        the return value wrapped in an optional, with an empty optional in case of an exception
      • withLock

        public static void withLock​(Lock lock,
                                    Runnable block)
        Executes a runnable with the given lock held.
        Parameters:
        lock - the lock
        block - the runnable to execute with the lock held
      • runIf

        public static void runIf​(boolean condition,
                                 Runnable code)
        Runs the given runnable only if the condition is true.
        Parameters:
        condition - the condition
        code - the runnable to execute based on the condition
      • delay

        public static void delay​(int ms)
        Delays execution for the given milliseconds.
        Parameters:
        ms - the delay in milliseconds
      • log

        public static void log​(String msg)
        Prints out a log message on standard output.
        Parameters:
        msg - the log message