Skip to content

Commit 48b38b4

Browse files
committed
=promise Optimize toCompletableFuture implantation if it's already completed.
1 parent 424b198 commit 48b38b4

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

  • subprojects/parseq/src/main/java/com/linkedin/parseq/promise

subprojects/parseq/src/main/java/com/linkedin/parseq/promise/Promise.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,23 @@ public interface Promise<P> {
136136
* @return CompletionStage
137137
*/
138138
default CompletionStage<P> toCompletionStage() {
139-
final CompletableFuture<P> future = new CompletableFuture<>();
140-
addListener(p -> {
141-
if (!p.isFailed()) {
142-
future.complete(p.get());
143-
}
144-
else {
145-
future.completeExceptionally(p.getError());
146-
}
147-
});
148-
return future;
139+
if (isDone()) {
140+
return CompletableFuture.completedFuture(get());
141+
} else if (isFailed()) {
142+
final CompletableFuture<P> future = new CompletableFuture<>();
143+
future.completeExceptionally(getError());
144+
return future;
145+
} else {
146+
final CompletableFuture<P> future = new CompletableFuture<>();
147+
addListener(p -> {
148+
if (!p.isFailed()) {
149+
future.complete(p.get());
150+
}
151+
else {
152+
future.completeExceptionally(p.getError());
153+
}
154+
});
155+
return future;
156+
}
149157
}
150158
}

0 commit comments

Comments
 (0)