diff --git a/aksw-commons-rx/src/main/java/org/aksw/commons/rx/lookup/LookupService.java b/aksw-commons-rx/src/main/java/org/aksw/commons/rx/lookup/LookupService.java index c57cb729..fc0a8df3 100644 --- a/aksw-commons-rx/src/main/java/org/aksw/commons/rx/lookup/LookupService.java +++ b/aksw-commons-rx/src/main/java/org/aksw/commons/rx/lookup/LookupService.java @@ -14,6 +14,12 @@ import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Single; +/** + * A lookup service builds a cold flowable that upon execution retrieves data for the given set of keys. + * + * @param + * @param + */ public interface LookupService extends Function, Flowable>> //CompletableFuture>> { diff --git a/aksw-commons-utils-parent/aksw-commons-utils/src/main/java/org/aksw/commons/util/exception/FinallyRunAll.java b/aksw-commons-utils-parent/aksw-commons-utils/src/main/java/org/aksw/commons/util/exception/FinallyRunAll.java index 51201be3..6b9aaa31 100644 --- a/aksw-commons-utils-parent/aksw-commons-utils/src/main/java/org/aksw/commons/util/exception/FinallyRunAll.java +++ b/aksw-commons-utils-parent/aksw-commons-utils/src/main/java/org/aksw/commons/util/exception/FinallyRunAll.java @@ -2,8 +2,11 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.List; import java.util.concurrent.Callable; +import java.util.function.Consumer; +import java.util.stream.Collectors; import org.aksw.commons.util.function.ThrowingRunnable; @@ -84,4 +87,47 @@ protected void runAction(int index) { public static void run(ThrowingRunnable ... actions) { new FinallyRunAll(Arrays.asList(actions)).run(); } + + public static void runAll(Collection actions, Consumer runner, ThrowingRunnable finallyAction) { + ThrowingRunnable[] runnables = actions.stream().map(action -> { + ThrowingRunnable r = () -> { + runner.accept(action); + }; + return r; + }).collect(Collectors.toList()).toArray(new ThrowingRunnable[0]); + + try { + run(runnables); + } finally { + if (finallyAction != null) { + try { + finallyAction.run(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } + + // Alternative approach using a loop + if (false) { + List exceptions = null; + for (T action : actions) { + try { + runner.accept(action); + } catch (Exception e) { + if (exceptions == null) { + exceptions = new ArrayList<>(); + } + exceptions.add(e); + } + } + + if (exceptions != null) { + RuntimeException e = new RuntimeException(); + exceptions.forEach(e::addSuppressed); + throw e; + } + } + } + }