/** * Wraps a Supplier in such a way the it will push the current execution context before any code * gets executed and pop it afterwards * * @param w the functional interface to be wrapped * @return wrapped object if there is a current execution context, or the same object if not. */ public static <T> Supplier<T> wrap(Supplier<T> w) { TaskContext c = current(); if (c != null) { return () -> { c.push(); try { return w.get(); } finally { c.pop(); } }; } return w; }
/** * Wraps a Consumer in such a way the it will push the current execution context before any code * gets executed and pop it afterwards * * @param w the functional interface to be wrapped * @return wrapped object if there is a current execution context, or the same object if not. */ public static <T> Consumer<T> wrap(Consumer<T> w) { TaskContext c = current(); if (c != null) { return (t) -> { c.push(); try { w.accept(t); } finally { c.pop(); } }; } return w; }
/** * Wraps a Function in such a way the it will push the current execution context before any code * gets executed and pop it afterwards * * @param w the functional interface to be wrapped * @return wrapped object if there is a current execution context, or the same object if not. */ public static <T, U, R> BiFunction<T, U, R> wrap(BiFunction<T, U, R> w) { TaskContext c = current(); if (c != null) { return (t, u) -> { c.push(); try { return w.apply(t, u); } finally { c.pop(); } }; } return w; }
/** * Wraps a Runnable in such a way the it will push the current execution context before any code * gets executed and pop it afterwards * * @param w the functional interface to be wrapped * @return wrapped object if there is a current execution context, or the same object if not. */ public static Runnable wrap(Runnable w) { TaskContext c = current(); if (c != null) { return () -> { c.push(); try { w.run(); } finally { c.pop(); } }; } return w; }