Package at.jku.ssw.fp.util
Class Util
- java.lang.Object
-
- at.jku.ssw.fp.util.Util
-
public class Util extends Object
Class with some miscellaneous utility methods.- Author:
- Herbert Praehofer
-
-
Constructor Summary
Constructors Constructor Description Util()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
delay(int ms)
Delays execution for the given milliseconds.static void
log(String msg)
Prints out a log message on standard output.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.static <R> Optional<R>
optional(Callable<R> callable)
Runs the given callable and returns the value wrapped in an optional.static void
runIf(boolean condition, Runnable code)
Runs the given runnable only if the condition is true.static void
withLock(Lock lock, Runnable block)
Executes a runnable with the given lock held.
-
-
-
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 keyB
- the type of the value- Parameters:
map
- the mapkey
- 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 lockblock
- 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 conditioncode
- 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
-
-