td
td copied to clipboard
CompletableFuture support for java client
Added option to java client to send query with CompletableFuture response. It allows work in a functional style with asynchronous requests, as well as chaining and combining.
// with Optional check
client.send(new TdApi.GetMe()).thenAccept(response ->
response.object().ifPresentOrElse(
me -> System.out.println("I'm " + me.firstName + " " + me.lastName),
() -> System.out.println(response.error().orElseGet(TdApi.Error::new))
)
);
// with callbacks
client.send(new TdApi.GetMe())
.thenAccept(response -> response
.onSuccess(me -> System.out.println("I'm " + me.firstName + " " + me.lastName))
.onError(System.out::println)
);
Because of CompletableFuture available since java 1.8, minimum supported version has been updated too.
Thank you, @p-vorobyev!
Unfortunately, this can't be merged now, because CompletableFuture is available on Android only since Android 7.0 and Telegram apps still support Android 4.*, but it can be merged after support for the old Android versions is dropped, which should happen someday.