Skip to content

Commit 2ffadc0

Browse files
committed
Moved CompletableFuture subclass references to a separate class, and instantiation to a factory method, in order to avoid premature loading of the class. Fixes failsafe-lib#59.
1 parent d4fe5c3 commit 2ffadc0

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

src/main/java/net/jodah/failsafe/AsyncFailsafe.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public FailsafeFuture<Void> runAsync(AsyncRunnable runnable) {
147147
@SuppressWarnings("unchecked")
148148
private <T> java.util.concurrent.CompletableFuture<T> call(AsyncCallableWrapper<T> callable) {
149149
FailsafeFuture<T> future = new FailsafeFuture<T>((FailsafeConfig<T, ?>) this);
150-
java.util.concurrent.CompletableFuture<T> response = Functions.cancellableFutureOf(future);
150+
java.util.concurrent.CompletableFuture<T> response = net.jodah.failsafe.internal.util.CancellableFuture.of(future);
151151
future.inject(response);
152152
call(callable, future);
153153
return response;

src/main/java/net/jodah/failsafe/Functions.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package net.jodah.failsafe;
22

33
import java.util.concurrent.Callable;
4-
import java.util.concurrent.CompletableFuture;
54
import java.util.concurrent.Semaphore;
6-
import java.util.function.BiConsumer;
75

86
import net.jodah.failsafe.function.AsyncCallable;
97
import net.jodah.failsafe.function.AsyncRunnable;
@@ -155,7 +153,7 @@ public T call() throws Exception {
155153
try {
156154
execution.before();
157155
asyncFutureLock.acquire();
158-
callable.call(execution).whenComplete(new BiConsumer<T, Throwable>() {
156+
callable.call(execution).whenComplete(new java.util.function.BiConsumer<T, Throwable>() {
159157
@Override
160158
public void accept(T innerResult, Throwable failure) {
161159
try {
@@ -187,7 +185,7 @@ static <T> AsyncCallableWrapper<T> asyncOfFuture(final Callable<java.util.concur
187185
public T call() throws Exception {
188186
try {
189187
execution.before();
190-
callable.call().whenComplete(new BiConsumer<T, Throwable>() {
188+
callable.call().whenComplete(new java.util.function.BiConsumer<T, Throwable>() {
191189
@Override
192190
public void accept(T innerResult, Throwable failure) {
193191
// Unwrap CompletionException cause
@@ -213,7 +211,7 @@ static <T> AsyncCallableWrapper<T> asyncOfFuture(
213211
public T call() throws Exception {
214212
try {
215213
execution.before();
216-
callable.call(execution).whenComplete(new BiConsumer<T, Throwable>() {
214+
callable.call(execution).whenComplete(new java.util.function.BiConsumer<T, Throwable>() {
217215
@Override
218216
public void accept(T innerResult, Throwable failure) {
219217
// Unwrap CompletionException cause
@@ -264,16 +262,6 @@ public T call() throws Exception {
264262
};
265263
}
266264

267-
static <T> CompletableFuture<T> cancellableFutureOf(final FailsafeFuture<T> future) {
268-
return new CompletableFuture<T>() {
269-
@Override
270-
public boolean cancel(boolean mayInterruptIfRunning) {
271-
future.cancel(mayInterruptIfRunning);
272-
return super.cancel(mayInterruptIfRunning);
273-
}
274-
};
275-
}
276-
277265
static <T, U, R> CheckedBiFunction<T, U, R> fnOf(final Callable<R> callable) {
278266
return new CheckedBiFunction<T, U, R>() {
279267
@Override
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.jodah.failsafe.internal.util;
2+
3+
import net.jodah.failsafe.FailsafeFuture;
4+
5+
/**
6+
* A CompletableFuture that forwards cancellation requests to an associated FailsafeFuture.
7+
*
8+
* @author Jonathan Halterman
9+
* @param <T>
10+
*/
11+
public class CancellableFuture<T> extends java.util.concurrent.CompletableFuture<T> {
12+
final FailsafeFuture<T> future;
13+
14+
private CancellableFuture(FailsafeFuture<T> future) {
15+
this.future = future;
16+
}
17+
18+
/**
19+
* We use a static factory method instead of the constructor to avoid the class from being accidentally loaded on
20+
* pre-Java 8 VMs when it's not available.
21+
*/
22+
public static <T> java.util.concurrent.CompletableFuture<T> of(FailsafeFuture<T> future) {
23+
return new CancellableFuture<T>(future);
24+
}
25+
26+
@Override
27+
public boolean cancel(boolean mayInterruptIfRunning) {
28+
future.cancel(mayInterruptIfRunning);
29+
return super.cancel(mayInterruptIfRunning);
30+
}
31+
};

0 commit comments

Comments
 (0)