-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathAdvanced.java
28 lines (25 loc) · 1.12 KB
/
Advanced.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.concurrent.atomic.*;
import java.util.concurrent.*;
public final class Advanced {
/** Uses `Server.uploadBatch` to upload the given texts, or cancels if 2 seconds have elapsed */
public static CompletableFuture<Void> upload(String[] texts) {
var cancelFlag = new AtomicBoolean(false);
return Server.uploadBatch(texts, cancelFlag)
.orTimeout(2, TimeUnit.SECONDS)
.exceptionally(e -> {
cancelFlag.set(true);
return null;
});
}
/** Adapts `OldServer.download` to the `CompletableFuture`-based world,
and prints the result if successful. */
public static CompletableFuture<Void> download() {
var future = new CompletableFuture<String>();
OldServer.download(future::complete, future::completeExceptionally);
return future.thenAccept(System.out::println);
}
/** Retries `download` until it succeeds */
public static CompletableFuture<Void> reliableDownload() {
return download().exceptionallyCompose(e -> reliableDownload());
}
}