a few more post-complete optimizations

This commit is contained in:
tommyskeff
2025-01-09 20:21:41 +00:00
parent ae15089b3d
commit e4e61c4b6c
2 changed files with 28 additions and 7 deletions

View File

@@ -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() {
PromiseCompletion<T> completion = getCompletion();
if (completion == null) {
CompletablePromise<T> fork = getFactory().unresolved(); CompletablePromise<T> fork = getFactory().unresolved();
PromiseUtil.propagateCompletion(this, fork); PromiseUtil.propagateCompletion(this, fork);
return 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) {
PromiseCompletion<T> completion = getCompletion();
if (completion == null) {
CompletablePromise<T> promise = createLinked(); CompletablePromise<T> promise = createLinked();
addDirectListener(promise::complete, e -> runCompleter(promise, () -> promise.complete(function.apply(e)))); addDirectListener(promise::complete, e -> runCompleter(promise, () -> promise.complete(function.apply(e))));
return promise; 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();
} }
}); });

View File

@@ -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;