From 2ffdecbe41ebaa221b6152b96760d25eba0156b8 Mon Sep 17 00:00:00 2001 From: Vaci Koblizek Date: Thu, 8 Oct 2020 15:35:15 +0100 Subject: [PATCH] Make Response typed --- runtime/src/main/java/org/capnproto/Capability.java | 7 +++++-- .../src/main/java/org/capnproto/RemotePromise.java | 7 ++++--- runtime/src/main/java/org/capnproto/Request.java | 12 +++++++++--- runtime/src/main/java/org/capnproto/RequestHook.java | 2 +- runtime/src/main/java/org/capnproto/Response.java | 10 +++++++--- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/runtime/src/main/java/org/capnproto/Capability.java b/runtime/src/main/java/org/capnproto/Capability.java index 277e5fa..f711e83 100644 --- a/runtime/src/main/java/org/capnproto/Capability.java +++ b/runtime/src/main/java/org/capnproto/Capability.java @@ -306,7 +306,7 @@ public final class Capability { final CompletableFuture cancelAllowed; MessageBuilder request; - Response response; + Response response; AnyPointer.Builder responseBuilder; ClientHook clientRef; @@ -333,7 +333,10 @@ public final class Capability { if (this.response == null) { var localResponse = new LocalResponse(); this.responseBuilder = localResponse.message.getRoot(AnyPointer.factory); - this.response = new Response(this.responseBuilder.asReader(), localResponse); + this.response = new Response<>( + AnyPointer.factory, + this.responseBuilder.asReader(), + localResponse); } return this.responseBuilder; } diff --git a/runtime/src/main/java/org/capnproto/RemotePromise.java b/runtime/src/main/java/org/capnproto/RemotePromise.java index a9acd6e..ab278bc 100644 --- a/runtime/src/main/java/org/capnproto/RemotePromise.java +++ b/runtime/src/main/java/org/capnproto/RemotePromise.java @@ -4,15 +4,16 @@ import java.util.concurrent.CompletableFuture; class RemotePromise { - final CompletableFuture response; + final CompletableFuture> response; final PipelineHook pipeline; - RemotePromise(CompletableFuture response, PipelineHook pipeline) { + RemotePromise(CompletableFuture> response, + PipelineHook pipeline) { this.response = response; this.pipeline = pipeline; } - public CompletableFuture getResponse() { + public CompletableFuture> getResponse() { return response; } diff --git a/runtime/src/main/java/org/capnproto/Request.java b/runtime/src/main/java/org/capnproto/Request.java index dd6d580..6573f7b 100644 --- a/runtime/src/main/java/org/capnproto/Request.java +++ b/runtime/src/main/java/org/capnproto/Request.java @@ -22,11 +22,17 @@ public class Request { return params.getAs(paramsBuilder); } - CompletableFuture send() { + RemotePromise send() { var typelessPromise = hook.send(); hook = null; // prevent reuse - return typelessPromise.getResponse().thenApply( - response -> response.getAs(resultsReader)); + var typedPromise = typelessPromise.getResponse().thenApply(response -> { + return new Response( + resultsReader, + response.get(), + response.hook); + }); + + return new RemotePromise(typedPromise, typelessPromise.pipeline); } static Request newBrokenRequest(Throwable exc) { diff --git a/runtime/src/main/java/org/capnproto/RequestHook.java b/runtime/src/main/java/org/capnproto/RequestHook.java index 1ba5df7..5813326 100644 --- a/runtime/src/main/java/org/capnproto/RequestHook.java +++ b/runtime/src/main/java/org/capnproto/RequestHook.java @@ -2,7 +2,7 @@ package org.capnproto; import java.util.concurrent.CompletableFuture; -interface RequestHook { +public interface RequestHook { RemotePromise send(); CompletableFuture sendStreaming(); Object getBrand(); diff --git a/runtime/src/main/java/org/capnproto/Response.java b/runtime/src/main/java/org/capnproto/Response.java index 8c188da..ee6c038 100644 --- a/runtime/src/main/java/org/capnproto/Response.java +++ b/runtime/src/main/java/org/capnproto/Response.java @@ -1,16 +1,20 @@ package org.capnproto; -class Response { +class Response { + final FromPointerReader factory; final ResponseHook hook; final AnyPointer.Reader results; - public Response(AnyPointer.Reader reader, ResponseHook hook) { + public Response(FromPointerReader factory, + AnyPointer.Reader reader, + ResponseHook hook) { + this.factory = factory; this.hook = hook; this.results = reader; } - public final T getAs(FromPointerReader factory) { + public final Results get() { return this.results.getAs(factory); } } \ No newline at end of file