From b5809a46a7ae5668a8a4910128980e202000e1c9 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Tue, 4 Jun 2019 14:33:18 +0200 Subject: [PATCH 1/2] POC async lambda implemented --- .../io/vertx/lang/rx/AbstractRxGenerator.java | 77 ++++++++++++++++--- .../io/vertx/lang/rxjava/RxJavaGenerator.java | 5 ++ .../main/java/io/vertx/rx/java/RxHelper.java | 11 ++- .../lang/reactivex/RxJava2Generator.java | 5 ++ .../java/io/vertx/reactivex/SingleHelper.java | 7 ++ .../testmodel/AsyncFunctionParamTCK.java | 68 ++++++++++++++++ .../testmodel/AsyncSupplierParamTCK.java | 67 ++++++++++++++++ 7 files changed, 225 insertions(+), 15 deletions(-) create mode 100644 rx-java2-gen/src/test/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java create mode 100644 rx-java2-gen/src/test/java/io/vertx/codegen/testmodel/AsyncSupplierParamTCK.java diff --git a/rx-gen/src/main/java/io/vertx/lang/rx/AbstractRxGenerator.java b/rx-gen/src/main/java/io/vertx/lang/rx/AbstractRxGenerator.java index a6551b28..fc8baa47 100644 --- a/rx-gen/src/main/java/io/vertx/lang/rx/AbstractRxGenerator.java +++ b/rx-gen/src/main/java/io/vertx/lang/rx/AbstractRxGenerator.java @@ -7,12 +7,15 @@ import io.vertx.codegen.doc.Tag; import io.vertx.codegen.doc.Token; import io.vertx.codegen.type.*; +import io.vertx.core.Handler; import javax.lang.model.element.Element; import java.io.PrintWriter; import java.io.StringWriter; import java.lang.annotation.Annotation; import java.util.*; +import java.util.function.Function; +import java.util.function.Supplier; import static io.vertx.codegen.type.ClassKind.*; import static java.util.stream.Collectors.joining; @@ -510,12 +513,24 @@ protected void startMethodTemplate(ClassTypeInfo type, MethodInfo method, String writer.print(" "); writer.print(method.getName()); writer.print("("); - writer.print(method.getParams().stream().map(it -> genTypeName(it.getType()) + " " + it.getName()).collect(joining(", "))); + writer.print(method.getParams().stream().map(it -> genTypeNameMethodParam(it.getType()) + " " + it.getName()).collect(joining(", "))); writer.print(")"); } - protected final String genTypeName(TypeInfo type) { + protected String genTypeNameMethodParam(TypeInfo type) { + if (isAsyncFunctionType(type)) { + ParameterizedTypeInfo pt = (ParameterizedTypeInfo) type; + return "java.util.function.Function<" + genTypeName(pt.getArg(0)) + ", Single<" + genTypeName(((ParameterizedTypeInfo)pt.getArg(1)).getArg(0)) + ">>"; + } else if (isAsyncSupplierType(type)) { + ParameterizedTypeInfo pt = (ParameterizedTypeInfo) type; + return "java.util.function.Supplier>"; + } else { + return genTypeName(type); + } + } + + protected String genTypeName(TypeInfo type) { if (type.isParameterized()) { ParameterizedTypeInfo pt = (ParameterizedTypeInfo) type; return genTypeName(pt.getRaw()) + pt.getArgs().stream().map(this::genTypeName).collect(joining(", ", "<", ">")); @@ -668,6 +683,20 @@ private String genInvokeDelegate(ClassModel model, MethodInfo method) { return ret.toString(); } + protected abstract String genConvertRxToFuture(TypeInfo type, String paramName); + + private boolean isAsyncFunctionType(TypeInfo type) { + if (!type.isParameterized() || !type.getRaw().getName().equals(Function.class.getCanonicalName())) return false; + TypeInfo returnType = ((ParameterizedTypeInfo)type).getArg(1); + return returnType.getRaw() != null && returnType.getRaw().getName().equals("io.vertx.core.Future"); + } + + private boolean isAsyncSupplierType(TypeInfo type) { + if (!type.isParameterized() || !type.getRaw().getName().equals(Handler.class.getCanonicalName())) return false; + TypeInfo returnType = ((ParameterizedTypeInfo)type).getArg(0); + return returnType.getRaw() != null && returnType.getRaw().getName().equals("io.vertx.core.Future"); + } + private boolean isSameType(TypeInfo type, MethodInfo method) { ClassKind kind = type.getKind(); if (kind.basic || kind.json || kind == DATA_OBJECT || kind == ENUM || kind == OTHER || kind == THROWABLE || kind == VOID) { @@ -693,6 +722,10 @@ private boolean isSameType(TypeInfo type, MethodInfo method) { return false; } + protected String getRxFutureType() { + return "Single"; + } + private String genConvParam(TypeInfo type, MethodInfo method, String expr) { ClassKind kind = type.getKind(); if (isSameType(type, method)) { @@ -714,7 +747,16 @@ private String genConvParam(TypeInfo type, MethodInfo method, String expr) { if (kind == HANDLER) { TypeInfo eventType = parameterizedTypeInfo.getArg(0); ClassKind eventKind = eventType.getKind(); - if (eventKind == ASYNC_RESULT) { + if (isAsyncSupplierType(type)) { + TypeInfo resultType = ((ParameterizedTypeInfo) eventType).getArg(0); + String retTypeConversion = genConvParam(resultType, method, "r"); + String finalMapping = retTypeConversion.equals("r") ? "" : ".map(r -> " + retTypeConversion + ")"; + return "new Handler>() {\n" + + " public void handle(io.vertx.core.Future<" + resultType.getName() + "> fut) {\n" + + " " + expr + ".get()" + finalMapping + ".subscribe(fut::complete, fut::fail);\n" + + " }\n" + + " }"; + } else if (eventKind == ASYNC_RESULT) { TypeInfo resultType = ((ParameterizedTypeInfo) eventType).getArg(0); return "new Handler>() {\n" + " public void handle(AsyncResult<" + resultType.getName() + "> ar) {\n" + @@ -733,14 +775,27 @@ private String genConvParam(TypeInfo type, MethodInfo method, String expr) { " }"; } } else if (kind == FUNCTION) { - TypeInfo argType = parameterizedTypeInfo.getArg(0); - TypeInfo retType = parameterizedTypeInfo.getArg(1); - return "new java.util.function.Function<" + argType.getName() + "," + retType.getName() + ">() {\n" + - " public " + retType.getName() + " apply(" + argType.getName() + " arg) {\n" + - " " + genTypeName(retType) + " ret = " + expr + ".apply(" + genConvReturn(argType, method, "arg") + ");\n" + - " return " + genConvParam(retType, method, "ret") + ";\n" + - " }\n" + - " }"; + if (isAsyncFunctionType(type)) { + TypeInfo argType = parameterizedTypeInfo.getArg(0); + TypeInfo asyncRetType = ((ParameterizedTypeInfo)parameterizedTypeInfo.getArg(1)).getArg(0); + String retTypeConversion = genConvParam(asyncRetType, method, "r"); + String finalMapping = retTypeConversion.equals("r") ? "" : ".map(r -> " + retTypeConversion + ")"; + return "new java.util.function.Function<" + argType.getName() + ",io.vertx.core.Future<" + asyncRetType.getName() + ">>() {\n" + + " public io.vertx.core.Future<" + asyncRetType.getName() + "> apply(" + argType.getName() + " arg) {\n" + + " " + getRxFutureType() + "<" + genTypeName(asyncRetType) + "> ret = " + expr + ".apply(" + genConvReturn(argType, method, "arg") + ");\n" + + " return " + genConvertRxToFuture(asyncRetType, "ret") + finalMapping + ";\n" + + " }\n" + + " }"; + } else { + TypeInfo argType = parameterizedTypeInfo.getArg(0); + TypeInfo retType = parameterizedTypeInfo.getArg(1); + return "new java.util.function.Function<" + argType.getName() + "," + retType.getName() + ">() {\n" + + " public " + retType.getName() + " apply(" + argType.getName() + " arg) {\n" + + " " + genTypeName(retType) + " ret = " + expr + ".apply(" + genConvReturn(argType, method, "arg") + ");\n" + + " return " + genConvParam(retType, method, "ret") + ";\n" + + " }\n" + + " }"; + } } else if (kind == LIST || kind == SET) { return expr + ".stream().map(elt -> " + genConvParam(parameterizedTypeInfo.getArg(0), method, "elt") + ").collect(java.util.stream.Collectors.to" + type.getRaw().getSimpleName() + "())"; } else if (kind == MAP) { diff --git a/rx-java-gen/src/main/java/io/vertx/lang/rxjava/RxJavaGenerator.java b/rx-java-gen/src/main/java/io/vertx/lang/rxjava/RxJavaGenerator.java index 8e0e48f8..3e23dbc8 100644 --- a/rx-java-gen/src/main/java/io/vertx/lang/rxjava/RxJavaGenerator.java +++ b/rx-java-gen/src/main/java/io/vertx/lang/rxjava/RxJavaGenerator.java @@ -28,6 +28,11 @@ protected void genRxImports(ClassModel model, PrintWriter writer) { super.genRxImports(model, writer); } + @Override + protected String genConvertRxToFuture(TypeInfo type, String paramName) { + return "io.vertx.rx.java.RxHelper.toFuture(" + paramName + ")"; + } + @Override protected void genToObservable(TypeInfo streamType, PrintWriter writer) { writer.print(" private rx.Observable<"); diff --git a/rx-java-gen/src/main/java/io/vertx/rx/java/RxHelper.java b/rx-java-gen/src/main/java/io/vertx/rx/java/RxHelper.java index e7c42e9c..309ed8b9 100644 --- a/rx-java-gen/src/main/java/io/vertx/rx/java/RxHelper.java +++ b/rx-java-gen/src/main/java/io/vertx/rx/java/RxHelper.java @@ -7,10 +7,7 @@ import io.vertx.core.streams.ReadStream; import io.vertx.core.streams.WriteStream; import io.vertx.rx.java.impl.WriteStreamSubscriberImpl; -import rx.Observable; -import rx.Observer; -import rx.Scheduler; -import rx.Subscriber; +import rx.*; import rx.functions.Action0; import rx.functions.Action1; import rx.plugins.RxJavaSchedulersHook; @@ -175,6 +172,12 @@ public static Handler> toFuture(Observer observer) { return observable.toHandler(); } + public static Future toFuture(Single single) { + Future fut = Future.future(); + single.subscribe(fut::complete, fut::fail); + return fut; + } + /** * Adapt a {@link Subscriber} as a {@code Handler;}.

* diff --git a/rx-java2-gen/src/main/java/io/vertx/lang/reactivex/RxJava2Generator.java b/rx-java2-gen/src/main/java/io/vertx/lang/reactivex/RxJava2Generator.java index 1963ca06..bfffb8c0 100644 --- a/rx-java2-gen/src/main/java/io/vertx/lang/reactivex/RxJava2Generator.java +++ b/rx-java2-gen/src/main/java/io/vertx/lang/reactivex/RxJava2Generator.java @@ -35,6 +35,11 @@ protected void genRxImports(ClassModel model, PrintWriter writer) { super.genRxImports(model, writer); } + @Override + protected String genConvertRxToFuture(TypeInfo type, String paramName) { + return "io.vertx.reactivex.SingleHelper.fromSingle(" + paramName + ")"; + } + @Override protected void genToObservable(TypeInfo streamType, PrintWriter writer) { writer.print(" private io.reactivex.Observable<"); diff --git a/rx-java2-gen/src/main/java/io/vertx/reactivex/SingleHelper.java b/rx-java2-gen/src/main/java/io/vertx/reactivex/SingleHelper.java index 62f30581..f7ccb2bf 100644 --- a/rx-java2-gen/src/main/java/io/vertx/reactivex/SingleHelper.java +++ b/rx-java2-gen/src/main/java/io/vertx/reactivex/SingleHelper.java @@ -16,6 +16,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; +import java.util.function.Function; /** * @author Julien Viet @@ -41,6 +42,12 @@ public static Single toSingle(Consumer>> handler) return AsyncResultSingle.toSingle(handler); } + public static Future fromSingle(Single single) { + Future fut = Future.future(); + single.subscribe(fut::complete, fut::fail); + return fut; + } + /** * Adapts an Vert.x {@code Handler>} to an RxJava2 {@link SingleObserver}. *

diff --git a/rx-java2-gen/src/test/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java b/rx-java2-gen/src/test/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java new file mode 100644 index 00000000..b910cc60 --- /dev/null +++ b/rx-java2-gen/src/test/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java @@ -0,0 +1,68 @@ +package io.vertx.codegen.testmodel; + +import io.vertx.codegen.annotations.Nullable; +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.Future; +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +/** + * @author Francesco Guardiani + */ +@VertxGen +public interface AsyncFunctionParamTCK { + + List methodWithBasicParam( + Function> byteFunc, + Function> shortFunc, + Function> integerFunc, + Function> longFunc, + Function> floatFunc, + Function> doubleFunc, + Function> booleanFunc, + Function> charFunc, + Function> stringFunc + ); + + List methodWithJsonParam(Function> objectFunc, Function> arrayFunc); + + String methodWithVoidParam(Function> func); + String methodWithUserTypeParam(RefedInterface1 arg, Function> func); + String methodWithObjectParam(Object arg, Function> func); + String methodWithDataObjectParam(Function> func); + String methodWithEnumParam(Function> func); + String methodWithListParam(Function, Future> stringFunc); + String methodWithSetParam(Function, Future> func); + String methodWithMapParam(Function, Future> func); + + String methodWithGenericParam(T t, Function> func); + String methodWithGenericUserTypeParam(T t, Function, Future> func); + + String methodWithBasicReturn( + Function> byteFunc, + Function> shortFunc, + Function> integerFunc, + Function> longFunc, + Function> floatFunc, + Function> doubleFunc, + Function> booleanFunc, + Function> charFunc, + Function> stringFunc + ); + + String methodWithJsonReturn(Function> objectFunc, Function> arrayFunc); + String methodWithObjectReturn(Function> func); + String methodWithDataObjectReturn(Function> func); + String methodWithEnumReturn(Function> func); + String methodWithListReturn(Function>> func); + String methodWithSetReturn(Function>> func); + String methodWithMapReturn(Function>> func); + String methodWithGenericReturn(Function> func); + String methodWithGenericUserTypeReturn(Function, Future>> func); + +} diff --git a/rx-java2-gen/src/test/java/io/vertx/codegen/testmodel/AsyncSupplierParamTCK.java b/rx-java2-gen/src/test/java/io/vertx/codegen/testmodel/AsyncSupplierParamTCK.java new file mode 100644 index 00000000..4ec56db7 --- /dev/null +++ b/rx-java2-gen/src/test/java/io/vertx/codegen/testmodel/AsyncSupplierParamTCK.java @@ -0,0 +1,67 @@ +package io.vertx.codegen.testmodel; + +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.Future; +import io.vertx.core.Handler; +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +/** + * @author Francesco Guardiani + */ +@VertxGen +public interface AsyncSupplierParamTCK { + + List methodWithBasicParam( + Handler> byteFunc, + Handler> shortFunc, + Handler> integerFunc, + Handler> longFunc, + Handler> floatFunc, + Handler> doubleFunc, + Handler> booleanFunc, + Handler> charFunc, + Handler> stringFunc + ); + + List methodWithJsonParam(Handler> objectFunc, Handler> arrayFunc); + + String methodWithVoidParam(Handler> func); + String methodWithUserTypeParam(RefedInterface1 arg, Handler> func); + String methodWithObjectParam(Object arg, Handler> func); + String methodWithDataObjectParam(Handler> func); + String methodWithEnumParam(Handler> func); + String methodWithListParam(Handler> stringFunc); + String methodWithSetParam(Handler> func); + String methodWithMapParam(Handler>> func); + + String methodWithGenericParam(T t, Handler> func); + String methodWithGenericUserTypeParam(T t, Handler> func); + + String methodWithBasicReturn( + Handler> byteFunc, + Handler> shortFunc, + Handler> integerFunc, + Handler> longFunc, + Handler> floatFunc, + Handler> doubleFunc, + Handler> booleanFunc, + Handler> charFunc, + Handler> stringFunc + ); + + String methodWithJsonReturn(Handler> objectFunc, Handler> arrayFunc); + String methodWithDataObjectReturn(Handler> func); + String methodWithEnumReturn(Handler> func); + String methodWithListReturn(Handler>> func); + String methodWithSetReturn(Handler>> func); + String methodWithMapReturn(Handler>> func); + String methodWithGenericReturn(Handler> func); + String methodWithGenericUserTypeReturn(Handler>> func); + +} From d9d6a79fbeba2df37dff42f0f940113c4f6d454e Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Tue, 4 Jun 2019 16:23:26 +0200 Subject: [PATCH 2/2] Added tests for generated interfaces --- .../rxjava/MethodWithAsyncFunction.java | 15 +++++ .../rxjava/MethodWithAsyncSupplier.java | 13 ++++ .../io/vertx/codegen/rxjava/package-info.java | 7 ++ .../testmodel/AsyncFunctionParamTCK.java | 67 +++++++++++++++++++ .../testmodel/AsyncSupplierParamTCK.java | 67 +++++++++++++++++++ .../io/vertx/core/streams/WriteStream.java | 39 +---------- .../rx/java/test/gen/AsyncLambdaTest.java | 31 +++++++++ .../test/gen/MethodWithAsyncFunctionImpl.java | 15 +++++ .../test/gen/MethodWithAsyncSupplierImpl.java | 16 +++++ .../rxjava2/MethodWithAsyncFunction.java | 15 +++++ .../rxjava2/MethodWithAsyncSupplier.java | 15 +++++ .../testmodel/AsyncFunctionParamTCK.java | 1 - .../io/vertx/core/streams/WriteStream.java | 39 +---------- .../vertx/reactivex/test/AsyncLambdaTest.java | 31 +++++++++ .../test/MethodWithAsyncFunctionImpl.java | 15 +++++ .../test/MethodWithAsyncSupplierImpl.java | 19 ++++++ 16 files changed, 332 insertions(+), 73 deletions(-) create mode 100644 rx-java-gen/src/test/java/io/vertx/codegen/rxjava/MethodWithAsyncFunction.java create mode 100644 rx-java-gen/src/test/java/io/vertx/codegen/rxjava/MethodWithAsyncSupplier.java create mode 100644 rx-java-gen/src/test/java/io/vertx/codegen/rxjava/package-info.java create mode 100644 rx-java-gen/src/test/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java create mode 100644 rx-java-gen/src/test/java/io/vertx/codegen/testmodel/AsyncSupplierParamTCK.java create mode 100644 rx-java-gen/src/test/java/io/vertx/rx/java/test/gen/AsyncLambdaTest.java create mode 100644 rx-java-gen/src/test/java/io/vertx/rx/java/test/gen/MethodWithAsyncFunctionImpl.java create mode 100644 rx-java-gen/src/test/java/io/vertx/rx/java/test/gen/MethodWithAsyncSupplierImpl.java create mode 100644 rx-java2-gen/src/test/java/io/vertx/codegen/rxjava2/MethodWithAsyncFunction.java create mode 100644 rx-java2-gen/src/test/java/io/vertx/codegen/rxjava2/MethodWithAsyncSupplier.java create mode 100644 rx-java2-gen/src/test/java/io/vertx/reactivex/test/AsyncLambdaTest.java create mode 100644 rx-java2-gen/src/test/java/io/vertx/reactivex/test/MethodWithAsyncFunctionImpl.java create mode 100644 rx-java2-gen/src/test/java/io/vertx/reactivex/test/MethodWithAsyncSupplierImpl.java diff --git a/rx-java-gen/src/test/java/io/vertx/codegen/rxjava/MethodWithAsyncFunction.java b/rx-java-gen/src/test/java/io/vertx/codegen/rxjava/MethodWithAsyncFunction.java new file mode 100644 index 00000000..1c5d93f5 --- /dev/null +++ b/rx-java-gen/src/test/java/io/vertx/codegen/rxjava/MethodWithAsyncFunction.java @@ -0,0 +1,15 @@ +package io.vertx.codegen.rxjava; + +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.AsyncResult; +import io.vertx.core.Future; +import io.vertx.core.Handler; + +import java.util.function.Function; + +@VertxGen +public interface MethodWithAsyncFunction { + + void method(Function> fn, Handler> resultHandler); + +} diff --git a/rx-java-gen/src/test/java/io/vertx/codegen/rxjava/MethodWithAsyncSupplier.java b/rx-java-gen/src/test/java/io/vertx/codegen/rxjava/MethodWithAsyncSupplier.java new file mode 100644 index 00000000..f3b6044a --- /dev/null +++ b/rx-java-gen/src/test/java/io/vertx/codegen/rxjava/MethodWithAsyncSupplier.java @@ -0,0 +1,13 @@ +package io.vertx.codegen.rxjava; + +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.AsyncResult; +import io.vertx.core.Future; +import io.vertx.core.Handler; + +@VertxGen +public interface MethodWithAsyncSupplier { + + void method(Handler> fn, Handler> resultHandler); + +} diff --git a/rx-java-gen/src/test/java/io/vertx/codegen/rxjava/package-info.java b/rx-java-gen/src/test/java/io/vertx/codegen/rxjava/package-info.java new file mode 100644 index 00000000..b6531aac --- /dev/null +++ b/rx-java-gen/src/test/java/io/vertx/codegen/rxjava/package-info.java @@ -0,0 +1,7 @@ +/** + * @author Julien Viet + */ +@ModuleGen(name = "rxjava", groupPackage = "io.vertx") +package io.vertx.codegen.rxjava; + +import io.vertx.codegen.annotations.ModuleGen; diff --git a/rx-java-gen/src/test/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java b/rx-java-gen/src/test/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java new file mode 100644 index 00000000..1325dc8b --- /dev/null +++ b/rx-java-gen/src/test/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java @@ -0,0 +1,67 @@ +package io.vertx.codegen.testmodel; + +import io.vertx.codegen.annotations.Nullable; +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.Future; +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +/** + * @author Francesco Guardiani + */ +@VertxGen +public interface AsyncFunctionParamTCK { + + List methodWithBasicParam( + Function> byteFunc, + Function> shortFunc, + Function> integerFunc, + Function> longFunc, + Function> floatFunc, + Function> doubleFunc, + Function> booleanFunc, + Function> charFunc, + Function> stringFunc + ); + + List methodWithJsonParam(Function> objectFunc, Function> arrayFunc); + + String methodWithVoidParam(Function> func); + String methodWithUserTypeParam(RefedInterface1 arg, Function> func); + String methodWithObjectParam(Object arg, Function> func); + String methodWithDataObjectParam(Function> func); + String methodWithEnumParam(Function> func); + String methodWithListParam(Function, Future> stringFunc); + String methodWithSetParam(Function, Future> func); + String methodWithMapParam(Function, Future> func); + + String methodWithGenericParam(T t, Function> func); + String methodWithGenericUserTypeParam(T t, Function, Future> func); + + String methodWithBasicReturn( + Function> byteFunc, + Function> shortFunc, + Function> integerFunc, + Function> longFunc, + Function> floatFunc, + Function> doubleFunc, + Function> booleanFunc, + Function> charFunc, + Function> stringFunc + ); + + String methodWithJsonReturn(Function> objectFunc, Function> arrayFunc); + String methodWithDataObjectReturn(Function> func); + String methodWithEnumReturn(Function> func); + String methodWithListReturn(Function>> func); + String methodWithSetReturn(Function>> func); + String methodWithMapReturn(Function>> func); + String methodWithGenericReturn(Function> func); + String methodWithGenericUserTypeReturn(Function, Future>> func); + +} diff --git a/rx-java-gen/src/test/java/io/vertx/codegen/testmodel/AsyncSupplierParamTCK.java b/rx-java-gen/src/test/java/io/vertx/codegen/testmodel/AsyncSupplierParamTCK.java new file mode 100644 index 00000000..4ec56db7 --- /dev/null +++ b/rx-java-gen/src/test/java/io/vertx/codegen/testmodel/AsyncSupplierParamTCK.java @@ -0,0 +1,67 @@ +package io.vertx.codegen.testmodel; + +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.Future; +import io.vertx.core.Handler; +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +/** + * @author Francesco Guardiani + */ +@VertxGen +public interface AsyncSupplierParamTCK { + + List methodWithBasicParam( + Handler> byteFunc, + Handler> shortFunc, + Handler> integerFunc, + Handler> longFunc, + Handler> floatFunc, + Handler> doubleFunc, + Handler> booleanFunc, + Handler> charFunc, + Handler> stringFunc + ); + + List methodWithJsonParam(Handler> objectFunc, Handler> arrayFunc); + + String methodWithVoidParam(Handler> func); + String methodWithUserTypeParam(RefedInterface1 arg, Handler> func); + String methodWithObjectParam(Object arg, Handler> func); + String methodWithDataObjectParam(Handler> func); + String methodWithEnumParam(Handler> func); + String methodWithListParam(Handler> stringFunc); + String methodWithSetParam(Handler> func); + String methodWithMapParam(Handler>> func); + + String methodWithGenericParam(T t, Handler> func); + String methodWithGenericUserTypeParam(T t, Handler> func); + + String methodWithBasicReturn( + Handler> byteFunc, + Handler> shortFunc, + Handler> integerFunc, + Handler> longFunc, + Handler> floatFunc, + Handler> doubleFunc, + Handler> booleanFunc, + Handler> charFunc, + Handler> stringFunc + ); + + String methodWithJsonReturn(Handler> objectFunc, Handler> arrayFunc); + String methodWithDataObjectReturn(Handler> func); + String methodWithEnumReturn(Handler> func); + String methodWithListReturn(Handler>> func); + String methodWithSetReturn(Handler>> func); + String methodWithMapReturn(Handler>> func); + String methodWithGenericReturn(Handler> func); + String methodWithGenericUserTypeReturn(Handler>> func); + +} diff --git a/rx-java-gen/src/test/java/io/vertx/core/streams/WriteStream.java b/rx-java-gen/src/test/java/io/vertx/core/streams/WriteStream.java index 3389dbd3..3a52d9b4 100644 --- a/rx-java-gen/src/test/java/io/vertx/core/streams/WriteStream.java +++ b/rx-java-gen/src/test/java/io/vertx/core/streams/WriteStream.java @@ -14,14 +14,13 @@ import io.vertx.codegen.annotations.Fluent; import io.vertx.codegen.annotations.Nullable; import io.vertx.codegen.annotations.VertxGen; -import io.vertx.core.AsyncResult; import io.vertx.core.Handler; /** * * Represents a stream of data that can be written to. *

- * Any class that implements this interface can be used by a {@link Pipe} to pipe data from a {@code ReadStream} + * Any class that implements this interface can be used by a {@link Pump} to pump data from a {@code ReadStream} * to it. * * @author Tim Fox @@ -49,12 +48,6 @@ public interface WriteStream extends StreamBase { @Fluent WriteStream write(T data); - /** - * Same as {@link #write(T)} but with an {@code handler} called when the operation completes - */ - @Fluent - WriteStream write(T data, Handler> handler); - /** * Ends the stream. *

@@ -62,40 +55,14 @@ public interface WriteStream extends StreamBase { */ void end(); - /** - * Same as {@link #end()} but with an {@code handler} called when the operation completes - */ - void end(Handler> handler); - /** * Same as {@link #end()} but writes some data to the stream before ending. - * - * @implSpec The default default implementation calls sequentially {@link #write(Object)} then {@link #end()} - * @apiNote Implementations might want to perform a single operation - * @param data the data to write */ - default void end(T data) { - write(data); + default void end(T t) { + write(t); end(); } - /** - * Same as {@link #end(T)} but with an {@code handler} called when the operation completes - */ - default void end(T data, Handler> handler) { - if (handler != null) { - write(data, ar -> { - if (ar.succeeded()) { - end(handler); - } else { - handler.handle(ar); - } - }); - } else { - end(data); - } - } - /** * Set the maximum size of the write queue to {@code maxSize}. You will still be able to write to the stream even * if there is more than {@code maxSize} items in the write queue. This is used as an indicator by classes such as diff --git a/rx-java-gen/src/test/java/io/vertx/rx/java/test/gen/AsyncLambdaTest.java b/rx-java-gen/src/test/java/io/vertx/rx/java/test/gen/AsyncLambdaTest.java new file mode 100644 index 00000000..cb634e23 --- /dev/null +++ b/rx-java-gen/src/test/java/io/vertx/rx/java/test/gen/AsyncLambdaTest.java @@ -0,0 +1,31 @@ +package io.vertx.rx.java.test.gen; + +import io.vertx.rxjava.codegen.rxjava.MethodWithAsyncFunction; +import io.vertx.rxjava.codegen.rxjava.MethodWithAsyncSupplier; +import io.vertx.test.core.VertxTestBase; +import org.junit.Test; +import rx.Single; + +public class AsyncLambdaTest extends VertxTestBase { + + @Test + public void methodWithAsyncFunction() { + MethodWithAsyncFunction api = new MethodWithAsyncFunction(new MethodWithAsyncFunctionImpl()); + api.rxMethod(s -> Single.just(s + " francesco")).subscribe(res -> { + assertEquals("hello francesco", res); + testComplete(); + }, this::fail); + await(); + } + + @Test + public void methodWithAsyncSupplier() { + MethodWithAsyncSupplier api = new MethodWithAsyncSupplier(new MethodWithAsyncSupplierImpl()); + api.rxMethod(() -> Single.just("francesco")).subscribe(res -> { + assertEquals("francesco", res); + testComplete(); + }, this::fail); + await(); + } + +} diff --git a/rx-java-gen/src/test/java/io/vertx/rx/java/test/gen/MethodWithAsyncFunctionImpl.java b/rx-java-gen/src/test/java/io/vertx/rx/java/test/gen/MethodWithAsyncFunctionImpl.java new file mode 100644 index 00000000..75349718 --- /dev/null +++ b/rx-java-gen/src/test/java/io/vertx/rx/java/test/gen/MethodWithAsyncFunctionImpl.java @@ -0,0 +1,15 @@ +package io.vertx.rx.java.test.gen; + +import io.vertx.codegen.rxjava.MethodWithAsyncFunction; +import io.vertx.core.AsyncResult; +import io.vertx.core.Future; +import io.vertx.core.Handler; + +import java.util.function.Function; + +public class MethodWithAsyncFunctionImpl implements MethodWithAsyncFunction { + @Override + public void method(Function> fn, Handler> resultHandler) { + fn.apply("hello").setHandler(resultHandler); + } +} diff --git a/rx-java-gen/src/test/java/io/vertx/rx/java/test/gen/MethodWithAsyncSupplierImpl.java b/rx-java-gen/src/test/java/io/vertx/rx/java/test/gen/MethodWithAsyncSupplierImpl.java new file mode 100644 index 00000000..be7fea20 --- /dev/null +++ b/rx-java-gen/src/test/java/io/vertx/rx/java/test/gen/MethodWithAsyncSupplierImpl.java @@ -0,0 +1,16 @@ +package io.vertx.rx.java.test.gen; + +import io.vertx.codegen.rxjava.MethodWithAsyncSupplier; +import io.vertx.core.AsyncResult; +import io.vertx.core.Future; +import io.vertx.core.Handler; + +public class MethodWithAsyncSupplierImpl implements MethodWithAsyncSupplier { + + @Override + public void method(Handler> fn, Handler> resultHandler) { + Future fut = Future.future(); + fut.setHandler(resultHandler); + fn.handle(fut); + } +} diff --git a/rx-java2-gen/src/test/java/io/vertx/codegen/rxjava2/MethodWithAsyncFunction.java b/rx-java2-gen/src/test/java/io/vertx/codegen/rxjava2/MethodWithAsyncFunction.java new file mode 100644 index 00000000..4c4497f4 --- /dev/null +++ b/rx-java2-gen/src/test/java/io/vertx/codegen/rxjava2/MethodWithAsyncFunction.java @@ -0,0 +1,15 @@ +package io.vertx.codegen.rxjava2; + +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.AsyncResult; +import io.vertx.core.Future; +import io.vertx.core.Handler; + +import java.util.function.Function; + +@VertxGen +public interface MethodWithAsyncFunction { + + void method(Function> fn, Handler> resultHandler); + +} diff --git a/rx-java2-gen/src/test/java/io/vertx/codegen/rxjava2/MethodWithAsyncSupplier.java b/rx-java2-gen/src/test/java/io/vertx/codegen/rxjava2/MethodWithAsyncSupplier.java new file mode 100644 index 00000000..9b1dcec8 --- /dev/null +++ b/rx-java2-gen/src/test/java/io/vertx/codegen/rxjava2/MethodWithAsyncSupplier.java @@ -0,0 +1,15 @@ +package io.vertx.codegen.rxjava2; + +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.AsyncResult; +import io.vertx.core.Future; +import io.vertx.core.Handler; + +import java.util.function.Function; + +@VertxGen +public interface MethodWithAsyncSupplier { + + void method(Handler> fn, Handler> resultHandler); + +} diff --git a/rx-java2-gen/src/test/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java b/rx-java2-gen/src/test/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java index b910cc60..1325dc8b 100644 --- a/rx-java2-gen/src/test/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java +++ b/rx-java2-gen/src/test/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java @@ -56,7 +56,6 @@ String methodWithBasicReturn( ); String methodWithJsonReturn(Function> objectFunc, Function> arrayFunc); - String methodWithObjectReturn(Function> func); String methodWithDataObjectReturn(Function> func); String methodWithEnumReturn(Function> func); String methodWithListReturn(Function>> func); diff --git a/rx-java2-gen/src/test/java/io/vertx/core/streams/WriteStream.java b/rx-java2-gen/src/test/java/io/vertx/core/streams/WriteStream.java index 3389dbd3..3a52d9b4 100644 --- a/rx-java2-gen/src/test/java/io/vertx/core/streams/WriteStream.java +++ b/rx-java2-gen/src/test/java/io/vertx/core/streams/WriteStream.java @@ -14,14 +14,13 @@ import io.vertx.codegen.annotations.Fluent; import io.vertx.codegen.annotations.Nullable; import io.vertx.codegen.annotations.VertxGen; -import io.vertx.core.AsyncResult; import io.vertx.core.Handler; /** * * Represents a stream of data that can be written to. *

- * Any class that implements this interface can be used by a {@link Pipe} to pipe data from a {@code ReadStream} + * Any class that implements this interface can be used by a {@link Pump} to pump data from a {@code ReadStream} * to it. * * @author Tim Fox @@ -49,12 +48,6 @@ public interface WriteStream extends StreamBase { @Fluent WriteStream write(T data); - /** - * Same as {@link #write(T)} but with an {@code handler} called when the operation completes - */ - @Fluent - WriteStream write(T data, Handler> handler); - /** * Ends the stream. *

@@ -62,40 +55,14 @@ public interface WriteStream extends StreamBase { */ void end(); - /** - * Same as {@link #end()} but with an {@code handler} called when the operation completes - */ - void end(Handler> handler); - /** * Same as {@link #end()} but writes some data to the stream before ending. - * - * @implSpec The default default implementation calls sequentially {@link #write(Object)} then {@link #end()} - * @apiNote Implementations might want to perform a single operation - * @param data the data to write */ - default void end(T data) { - write(data); + default void end(T t) { + write(t); end(); } - /** - * Same as {@link #end(T)} but with an {@code handler} called when the operation completes - */ - default void end(T data, Handler> handler) { - if (handler != null) { - write(data, ar -> { - if (ar.succeeded()) { - end(handler); - } else { - handler.handle(ar); - } - }); - } else { - end(data); - } - } - /** * Set the maximum size of the write queue to {@code maxSize}. You will still be able to write to the stream even * if there is more than {@code maxSize} items in the write queue. This is used as an indicator by classes such as diff --git a/rx-java2-gen/src/test/java/io/vertx/reactivex/test/AsyncLambdaTest.java b/rx-java2-gen/src/test/java/io/vertx/reactivex/test/AsyncLambdaTest.java new file mode 100644 index 00000000..f5cad793 --- /dev/null +++ b/rx-java2-gen/src/test/java/io/vertx/reactivex/test/AsyncLambdaTest.java @@ -0,0 +1,31 @@ +package io.vertx.reactivex.test; + +import io.reactivex.Single; +import io.vertx.reactivex.codegen.rxjava2.MethodWithAsyncFunction; +import io.vertx.reactivex.codegen.rxjava2.MethodWithAsyncSupplier; +import io.vertx.test.core.VertxTestBase; +import org.junit.Test; + +public class AsyncLambdaTest extends VertxTestBase { + + @Test + public void methodWithAsyncFunction() { + MethodWithAsyncFunction api = new MethodWithAsyncFunction(new MethodWithAsyncFunctionImpl()); + api.rxMethod(s -> Single.just(s + " francesco")).subscribe(res -> { + assertEquals("hello francesco", res); + testComplete(); + }, this::fail); + await(); + } + + @Test + public void methodWithAsyncSupplier() { + MethodWithAsyncSupplier api = new MethodWithAsyncSupplier(new MethodWithAsyncSupplierImpl()); + api.rxMethod(() -> Single.just("francesco")).subscribe(res -> { + assertEquals("francesco", res); + testComplete(); + }, this::fail); + await(); + } + +} diff --git a/rx-java2-gen/src/test/java/io/vertx/reactivex/test/MethodWithAsyncFunctionImpl.java b/rx-java2-gen/src/test/java/io/vertx/reactivex/test/MethodWithAsyncFunctionImpl.java new file mode 100644 index 00000000..1f9e1110 --- /dev/null +++ b/rx-java2-gen/src/test/java/io/vertx/reactivex/test/MethodWithAsyncFunctionImpl.java @@ -0,0 +1,15 @@ +package io.vertx.reactivex.test; + +import io.vertx.codegen.rxjava2.MethodWithAsyncFunction; +import io.vertx.core.AsyncResult; +import io.vertx.core.Future; +import io.vertx.core.Handler; + +import java.util.function.Function; + +public class MethodWithAsyncFunctionImpl implements MethodWithAsyncFunction { + @Override + public void method(Function> fn, Handler> resultHandler) { + fn.apply("hello").setHandler(resultHandler); + } +} diff --git a/rx-java2-gen/src/test/java/io/vertx/reactivex/test/MethodWithAsyncSupplierImpl.java b/rx-java2-gen/src/test/java/io/vertx/reactivex/test/MethodWithAsyncSupplierImpl.java new file mode 100644 index 00000000..438e95d3 --- /dev/null +++ b/rx-java2-gen/src/test/java/io/vertx/reactivex/test/MethodWithAsyncSupplierImpl.java @@ -0,0 +1,19 @@ +package io.vertx.reactivex.test; + +import io.vertx.codegen.rxjava2.MethodWithAsyncFunction; +import io.vertx.codegen.rxjava2.MethodWithAsyncSupplier; +import io.vertx.core.AsyncResult; +import io.vertx.core.Future; +import io.vertx.core.Handler; + +import java.util.function.Function; + +public class MethodWithAsyncSupplierImpl implements MethodWithAsyncSupplier { + + @Override + public void method(Handler> fn, Handler> resultHandler) { + Future fut = Future.future(); + fut.setHandler(resultHandler); + fn.handle(fut); + } +}