Skip to content

Commit d3dd159

Browse files
committed
[hibernate#2181] Add CompletionStages#supplyStage
It's useful for when we need to map a try-finally block.
1 parent 4563b8c commit d3dd159

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/util/impl/CompletionStages.java

+20
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@ public static <T> CompletionStage<T> completedFuture(T value) {
9090
return CompletableFuture.completedFuture( value );
9191
}
9292

93+
/**
94+
* Useful for implementing try-finally blocks.
95+
* For example:
96+
* <pre>{@code
97+
* return supplyStage( () -> {
98+
* // Any error here will get caught
99+
* ...
100+
* })
101+
* .whenComplete( (r,t) -> {
102+
* // finally block
103+
* ...
104+
* })
105+
* }</pre>
106+
*/
107+
public static <T> CompletionStage<T> supplyStage(Supplier<CompletionStage<T>> supplier) {
108+
// Using the voidFuture() is the simplest way I found to make sure that everything run in the correct executor
109+
return voidFuture()
110+
.thenCompose( v -> supplier.get() );
111+
}
112+
93113
public static <T> CompletionStage<T> failedFuture(Throwable t) {
94114
CompletableFuture<T> ret = new CompletableFuture<>();
95115
ret.completeExceptionally( t );

0 commit comments

Comments
 (0)