Skip to content

Commit ad3a948

Browse files
committed
Fixing exceptions propagation with Promise.getNow(...)
1 parent cee6d9f commit ad3a948

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>net.tascalate.concurrent</groupId>
66
<artifactId>net.tascalate.concurrent.lib</artifactId>
7-
<version>0.6-SNAPSHOT</version>
7+
<version>0.5.1-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>Tascalate Concurrenct</name>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public T get() throws InterruptedException, ExecutionException {
7474
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
7575
return completionStage.get(timeout, unit);
7676
}
77+
78+
@Override
79+
public T getNow(T valueIfAbsent) {
80+
return completionStage.getNow(valueIfAbsent);
81+
}
7782

7883
static boolean cancelPromise(final CompletionStage<?> promise, final boolean mayInterruptIfRunning) {
7984
if (promise instanceof Future) {

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package net.tascalate.concurrent;
1717

18+
import java.util.concurrent.CompletionException;
1819
import java.util.concurrent.CompletionStage;
1920
import java.util.concurrent.ExecutionException;
2021
import java.util.concurrent.Executor;
@@ -25,6 +26,7 @@
2526
import java.util.function.Function;
2627
import java.util.function.Supplier;
2728

29+
2830
/**
2931
* <p>{@link Promise} is a combination of the {@link CompletionStage} and {@link Future} contracts.
3032
* It provides both composition methods of the former and blocking access methods of the later.
@@ -37,13 +39,20 @@
3739
*/
3840
public interface Promise<T> extends Future<T>, CompletionStage<T> {
3941

40-
default public T getNow(T valueIfAbsent) throws InterruptedException, ExecutionException {
42+
default public T getNow(T valueIfAbsent) {
4143
return getNow(() -> valueIfAbsent);
4244
}
4345

44-
default public T getNow(Supplier<T> valueIfAbsent) throws InterruptedException, ExecutionException {
46+
default public T getNow(Supplier<T> valueIfAbsent) {
4547
if (isDone()) {
46-
return get();
48+
try {
49+
return get();
50+
} catch (InterruptedException ex) {
51+
// Should not happen when isDone() returns true
52+
throw new RuntimeException(ex);
53+
} catch (ExecutionException ex) {
54+
throw new CompletionException(ex);
55+
}
4756
} else {
4857
return valueIfAbsent.get();
4958
}

0 commit comments

Comments
 (0)