Esempio n. 1
0
 /**
  * Wraps a BiConsumer 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> BiConsumer<T, U> wrap(BiConsumer<T, U> w) {
   TaskContext c = current();
   if (c != null) {
     return (t, u) -> {
       c.push();
       try {
         w.accept(t, u);
       } finally {
         c.pop();
       }
     };
   }
   return w;
 }
Esempio n. 2
0
 /**
  * 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;
 }
Esempio n. 3
0
 /**
  * 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;
 }
Esempio n. 4
0
 /**
  * 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;
 }