summaryrefslogtreecommitdiff
path: root/lib/java/src/main/java/org/apache/thrift/async/AsyncMethodFutureAdapter.java
blob: 202af7bffb01d4aa05c80fc8f49bed02c9ba0aa0 (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);
  }
}