Skip to content

Commit cee6d9f

Browse files
committed
Adding Promise.getNow(...) methiod similar to the one available with CompletableFuture.
1 parent fd1b326 commit cee6d9f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/main/java/net/tascalate/concurrent/Promise.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
package net.tascalate.concurrent;
1717

1818
import java.util.concurrent.CompletionStage;
19+
import java.util.concurrent.ExecutionException;
1920
import java.util.concurrent.Executor;
2021
import java.util.concurrent.Future;
2122
import java.util.function.BiConsumer;
2223
import java.util.function.BiFunction;
2324
import java.util.function.Consumer;
2425
import java.util.function.Function;
26+
import java.util.function.Supplier;
2527

2628
/**
2729
* <p>{@link Promise} is a combination of the {@link CompletionStage} and {@link Future} contracts.
@@ -34,6 +36,18 @@
3436
* a type of the successfully resolved promise value
3537
*/
3638
public interface Promise<T> extends Future<T>, CompletionStage<T> {
39+
40+
default public T getNow(T valueIfAbsent) throws InterruptedException, ExecutionException {
41+
return getNow(() -> valueIfAbsent);
42+
}
43+
44+
default public T getNow(Supplier<T> valueIfAbsent) throws InterruptedException, ExecutionException {
45+
if (isDone()) {
46+
return get();
47+
} else {
48+
return valueIfAbsent.get();
49+
}
50+
}
3751

3852
public <U> Promise<U> thenApply(Function<? super T, ? extends U> fn);
3953

0 commit comments

Comments
 (0)