1
votes

In scala, there is a trait like

trait Client {
  def get(requests: Seq[Request]): Future[Seq[Response]]
}

How to implement the class in Java with some fake implementation like return Future.successful(List.empty())?

I tried

class KVClient implements Client {
    @Override
    public Future<Seq<Response>> get(Seq<Request> requests) {
      return Future.successful(List.empty());
    }

But it didn't compile. Error is "KVClient is not abstract and does not override abstract method get(Seq) in Client"

1
Can you post exactly what is the compiler error you get?. In addition to this can you post the imports of the classes you are using? - JArgente
You can use ` Future future = CompletableFuture.completedFuture(value); ` to return a successful future. and please post the error as well - Geetika
The trait is trait Client def get(requests: Seq[Request]): Future[Seq[Response]] } I have KVClient which implements Client. ``` @Override public Future<Seq<Response>> get(Seq<Request> requests) { return Future.successful(List.empty()); } ``` The error is "KVClient is not abstract and does not override abstract method get(Seq<Request>) in Client" - Jing Huang

1 Answers

0
votes

if i understood you correctly you are trying to return result or error of response future.

Simple solution would be:

public CompletableFuture<Optional<?>> send() throws ExecutionException, InterruptedException {
      final CompletableFuture<Optional<?>> optionalCompletableFuture = CompletableFuture.supplyAsync(() -> "")
        .thenApplyAsync(f -> {
          if (isError(f)) {
            return Optional.empty();
          }
          return Optional.of(f);
        });
      return optionalCompletableFuture;

    }