Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CompletableFuture support for java client #2792

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion example/java/README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
46 changes: 46 additions & 0 deletions example/java/org/drinkless/tdlib/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
package org.drinkless.tdlib;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

Expand Down Expand Up @@ -62,6 +63,30 @@ public interface LogMessageHandler {
void onLogMessage(int verbosityLevel, String message);
}

/**
* Result class for an asynchronous function call with a future response.
*
* @param <T> The object type that is returned by the function
*/
public final static class Result<T extends TdApi.Object> {

/**
* An object of this type can be returned upon a successful function call, otherwise it will be null.
*/
public final T object;

/**
* An object of this type can be returned on every function call, in case of an error, otherwise it will be null.
*/
public final TdApi.Error error;

private Result(T object, TdApi.Error error) {
this.object = object;
this.error = error;
}

}

/**
* Exception class thrown when TDLib error occurred while performing {@link #execute(TdApi.Function)}.
*/
Expand Down Expand Up @@ -111,6 +136,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 <T> 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 <T extends TdApi.Object> CompletableFuture<Result<T>> send(TdApi.Function<T> query) {
CompletableFuture<Result<T>> 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.
*
Expand Down
2 changes: 1 addition & 1 deletion example/java/td_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static jstring Function_toString(JNIEnv *env, jobject object) {
return td::jni::to_jstring(env, to_string(td::td_api::Function::fetch(env, object)));
}

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;

Expand Down