diff --git a/example/java/README.md b/example/java/README.md index ca103947f5b2..40b4f0a9f6f3 100644 --- a/example/java/README.md +++ b/example/java/README.md @@ -1,6 +1,6 @@ # TDLib Java example -To run this example, you will need installed JDK >= 1.6. +To run this example, you will need installed JDK >= 1.8. For Javadoc documentation generation PHP is needed. You can find complete build instructions for your operating system at https://tdlib.github.io/td/build.html?language=Java. diff --git a/example/java/org/drinkless/tdlib/Client.java b/example/java/org/drinkless/tdlib/Client.java index 5c61b1c6be2c..f2e8ad04c45b 100644 --- a/example/java/org/drinkless/tdlib/Client.java +++ b/example/java/org/drinkless/tdlib/Client.java @@ -6,8 +6,11 @@ // package org.drinkless.tdlib; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Consumer; /** * Main class for interaction with the TDLib. @@ -62,6 +65,61 @@ public interface LogMessageHandler { void onLogMessage(int verbosityLevel, String message); } + /** + * Result class for an asynchronous function call with a future response. + * + * @param The object type that is returned by the function + */ + public final static class Result { + + private final T object; + + private final TdApi.Error error; + + private Result(T object, TdApi.Error error) { + this.object = object; + this.error = error; + } + + /** + * An object of this type can be returned upon a successful function call, otherwise it will be null. + */ + public Optional object() { + return Optional.ofNullable(object); + } + + /** + * An object of this type can be returned on every function call, in case of an error, otherwise it will be null. + */ + public Optional error() { + return Optional.ofNullable(error); + } + + /** + * Performs an action upon a successful function call and returns current {@link Result}. + * @param action callback called upon a successful function call of query to TDLib + * @return {@link Result} + */ + public Result onSuccess(Consumer action) { + if (object != null) { + action.accept(object); + } + return this; + } + + /** + * Performs an action in case of an error and returns current {@link Result}. + * @param action callback called in case of an error of query to TDLib + * @return {@link Result} + */ + public Result onError(Consumer action) { + if (error != null) { + action.accept(error); + } + return this; + } + } + /** * Exception class thrown when TDLib error occurred while performing {@link #execute(TdApi.Function)}. */ @@ -111,6 +169,27 @@ public void send(TdApi.Function query, ResultHandler resultHandler) { send(query, resultHandler, null); } + /** + * Sends a request to the TDLib. + * + * @param query Object representing a query to the TDLib. + * @param Automatically deduced return type of the query. + * @return {@link CompletableFuture} response with {@link Result} + * If this stage completes exceptionally it throws {@link ExecutionException} + */ + @SuppressWarnings("unchecked") + public CompletableFuture> send(TdApi.Function query) { + CompletableFuture> future = new CompletableFuture<>(); + send(query, object -> { + if (object instanceof TdApi.Error) { + future.complete(new Result<>(null, (TdApi.Error) object)); + } else { + future.complete(new Result<>((T) object, null)); + } + }); + return future; + } + /** * Synchronously executes a TDLib request. Only a few marked accordingly requests can be executed synchronously. * diff --git a/example/java/td_jni.cpp b/example/java/td_jni.cpp index 675829c9b110..182e80b80aee 100644 --- a/example/java/td_jni.cpp +++ b/example/java/td_jni.cpp @@ -114,7 +114,7 @@ static jstring Function_toString(JNIEnv *env, jobject object) { } #endif -static constexpr jint JAVA_VERSION = JNI_VERSION_1_6; +static constexpr jint JAVA_VERSION = JNI_VERSION_1_8; static JavaVM *java_vm; static jobject log_message_handler;