summaryrefslogtreecommitdiff
path: root/lib/java/src/main/java/org/apache/thrift/async/AsyncMethodFutureAdapter.java
blob: 0bee3a7cf04ab21ee98930c4549ce3d2385e2a26 (plain)
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
29
30
31
32
33
34
35
package org.apache.thrift.async;

import java.util.concurrent.CompletableFuture;

/**
 * A simple adapter that bridges {@link AsyncMethodCallback} with {@link CompletableFuture}-returning style clients.
 * Compiler generated code will invoke this adapter to implement {@code FutureClient}s.
 *
 * @param <T> return type (can be {@link Void}).
 */
public final class AsyncMethodFutureAdapter<T> implements AsyncMethodCallback<T> {

    private AsyncMethodFutureAdapter() {
    }

    public static <T> AsyncMethodFutureAdapter<T> create() {
        return new AsyncMethodFutureAdapter<>();
    }

    private final CompletableFuture<T> future = new CompletableFuture<>();

    public CompletableFuture<T> getFuture() {
        return future;
    }

    @Override
    public void onComplete(T response) {
        future.complete(response);
    }

    @Override
    public void onError(Exception exception) {
        future.completeExceptionally(exception);
    }
}