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 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
79 changes: 79 additions & 0 deletions example/java/org/drinkless/tdlib/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 <T> The object type that is returned by the function
*/
public final static class Result<T extends TdApi.Object> {

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<T> 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<TdApi.Error> error() {
return Optional.ofNullable(error);
}

/**
* Performs an action upon a successful function call and returns current {@link Result<T>}.
* @param action callback called upon a successful function call of query to TDLib
* @return {@link Result<T>}
*/
public Result<T> onSuccess(Consumer<T> action) {
if (object != null) {
action.accept(object);
}
return this;
}

/**
* Performs an action in case of an error and returns current {@link Result<T>}.
* @param action callback called in case of an error of query to TDLib
* @return {@link Result<T>}
*/
public Result<T> onError(Consumer<TdApi.Error> action) {
if (error != null) {
action.accept(error);
}
return this;
}
}

/**
* Exception class thrown when TDLib error occurred while performing {@link #execute(TdApi.Function)}.
*/
Expand Down Expand Up @@ -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 <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 @@ -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;

Expand Down