mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-17 23:16:01 +00:00
a few more post-complete optimizations
This commit is contained in:
@@ -86,9 +86,14 @@ public abstract class AbstractPromise<T, FS, FA> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<T> fork() {
|
public @NotNull Promise<T> fork() {
|
||||||
CompletablePromise<T> fork = getFactory().unresolved();
|
PromiseCompletion<T> completion = getCompletion();
|
||||||
PromiseUtil.propagateCompletion(this, fork);
|
if (completion == null) {
|
||||||
return fork;
|
CompletablePromise<T> fork = getFactory().unresolved();
|
||||||
|
PromiseUtil.propagateCompletion(this, fork);
|
||||||
|
return fork;
|
||||||
|
} else {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -446,9 +451,20 @@ public abstract class AbstractPromise<T, FS, FA> implements Promise<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Promise<T> orDefault(@NotNull ExceptionalFunction<Throwable, T> function) {
|
public @NotNull Promise<T> orDefault(@NotNull ExceptionalFunction<Throwable, T> function) {
|
||||||
CompletablePromise<T> promise = createLinked();
|
PromiseCompletion<T> completion = getCompletion();
|
||||||
addDirectListener(promise::complete, e -> runCompleter(promise, () -> promise.complete(function.apply(e))));
|
if (completion == null) {
|
||||||
return promise;
|
CompletablePromise<T> promise = createLinked();
|
||||||
|
addDirectListener(promise::complete, e -> runCompleter(promise, () -> promise.complete(function.apply(e))));
|
||||||
|
return promise;
|
||||||
|
} else if (completion.isSuccess()) {
|
||||||
|
return getFactory().resolve(completion.getResult());
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
return getFactory().resolve(function.apply(completion.getException()));
|
||||||
|
} catch (Exception e) {
|
||||||
|
return getFactory().error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -457,7 +473,7 @@ public abstract class AbstractPromise<T, FS, FA> implements Promise<T> {
|
|||||||
addDirectListener(future::complete, future::completeExceptionally);
|
addDirectListener(future::complete, future::completeExceptionally);
|
||||||
future.whenComplete((_, e) -> {
|
future.whenComplete((_, e) -> {
|
||||||
if (e instanceof CancellationException) {
|
if (e instanceof CancellationException) {
|
||||||
this.cancel();
|
cancel();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -55,6 +55,11 @@ public abstract class CompletedPromise<T, FS, FA> extends AbstractPromise<T, FS,
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Promise<T> fork() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull PromiseCompletion<T> getCompletion() {
|
public @NotNull PromiseCompletion<T> getCompletion() {
|
||||||
return completion;
|
return completion;
|
||||||
|
|||||||
Reference in New Issue
Block a user