Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions src/main/java/org/apache/sysds/runtime/ooc/cache/OOCFuture.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,36 @@ public T get(long timeout, TimeUnit unit) throws InterruptedException, Execution
}
}

public <R> OOCFuture<R> thenCompose(Function<? super T, ? extends OOCFuture<? extends R>> mapper) {
OOCFuture<R> result = new OOCFuture<>();
whenComplete((value, error) -> {
if(error != null) {
result.completeExceptionally(error);
return;
}

final OOCFuture<? extends R> next;
try {
next = mapper.apply(value);
if(next == null)
throw new NullPointerException("thenCompose mapper returned null");
}
catch(Throwable t) {
result.completeExceptionally(t);
return;
}

next.whenComplete((nextValue, nextError) -> {
if(nextError != null)
result.completeExceptionally(nextError);
else
result.complete(nextValue);
});
});

return result;
}

private <R> void subscribe(Function<? super T, ? extends R> mapper, Consumer<? super R> action,
BiConsumer<? super R, ? super Throwable> completion) {
T value;
Expand Down Expand Up @@ -167,7 +197,6 @@ else if(resultError == null)
action.accept(result);
}
catch(Throwable ignored) {
// Subscribers are independent; one failed callback must not prevent the remaining notifications.
}
}

Expand All @@ -181,8 +210,8 @@ private static final class Subscriber<T> {
private <R> Subscriber(Function<? super T, ? extends R> mapper, Consumer<? super R> action,
BiConsumer<? super R, ? super Throwable> completion, Subscriber<T> next) {
this.mapper = mapper;
this.action = (Consumer<Object>)action;
this.completion = (BiConsumer<Object, Throwable>)(BiConsumer<?, ?>)completion;
this.action = (Consumer<Object>) action;
this.completion = (BiConsumer<Object, Throwable>) completion;
this.next = next;
}

Expand Down
Loading
Loading