From 11ce1427166b4c18fe2dc6e2865863df2346044a Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Tue, 13 Oct 2020 23:22:21 +0200 Subject: [PATCH] Change errors behavior --- .../java/it/tdlight/common/ClientState.java | 116 ------------------ .../it/tdlight/common/InternalClient.java | 23 +--- src/main/java/it/tdlight/common/Request.java | 54 -------- .../java/it/tdlight/common/ResponseArray.java | 33 ----- .../it/tdlight/common/ResponseReceiver.java | 1 + .../it/tdlight/common/TDLibException.java | 21 ---- .../it/tdlight/common/TelegramClient.java | 2 +- .../it/tdlight/common/UpdatesHandler.java | 6 +- .../common/{ => utils}/IntSwapper.java | 4 +- 9 files changed, 9 insertions(+), 251 deletions(-) delete mode 100644 src/main/java/it/tdlight/common/ClientState.java delete mode 100644 src/main/java/it/tdlight/common/Request.java delete mode 100644 src/main/java/it/tdlight/common/ResponseArray.java delete mode 100644 src/main/java/it/tdlight/common/TDLibException.java rename src/main/java/it/tdlight/common/{ => utils}/IntSwapper.java (76%) diff --git a/src/main/java/it/tdlight/common/ClientState.java b/src/main/java/it/tdlight/common/ClientState.java deleted file mode 100644 index 29cc1f9..0000000 --- a/src/main/java/it/tdlight/common/ClientState.java +++ /dev/null @@ -1,116 +0,0 @@ -package it.tdlight.common; - -import java.util.StringJoiner; - -public class ClientState { - - private boolean hasClientId; - private long clientId; - private boolean initialized; - private boolean readyToReceive; - private boolean readyToSend; - - private ClientState(boolean hasClientId, long clientId, boolean initialized, boolean readyToReceive, boolean readyToSend) { - this.hasClientId = hasClientId; - this.clientId = clientId; - this.initialized = initialized; - this.readyToReceive = readyToReceive; - this.readyToSend = readyToSend; - } - - public static ClientState of(boolean hasClientId, long clientId, boolean initialized, boolean readyToReceive, boolean readyToSend) { - return new ClientState(hasClientId, clientId, initialized, readyToReceive, readyToSend); - } - - public boolean hasClientId() { - return hasClientId; - } - - public long getClientId() { - return clientId; - } - - public boolean isInitialized() { - return initialized; - } - - public boolean isReadyToReceive() { - return readyToReceive; - } - - public boolean isReadyToSend() { - return readyToSend; - } - - public ClientState setHasClientId(boolean hasClientId) { - this.hasClientId = hasClientId; - return this; - } - - public ClientState setClientId(long clientId) { - this.clientId = clientId; - return this; - } - - public ClientState setInitialized(boolean initialized) { - this.initialized = initialized; - return this; - } - - public ClientState setReadyToReceive(boolean readyToReceive) { - this.readyToReceive = readyToReceive; - return this; - } - - public ClientState setReadyToSend(boolean readyToSend) { - this.readyToSend = readyToSend; - return this; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - ClientState that = (ClientState) o; - - if (hasClientId != that.hasClientId) { - return false; - } - if (clientId != that.clientId) { - return false; - } - if (initialized != that.initialized) { - return false; - } - if (readyToReceive != that.readyToReceive) { - return false; - } - return readyToSend == that.readyToSend; - } - - @Override - public int hashCode() { - int result = (hasClientId ? 1 : 0); - result = 31 * result + (int) (clientId ^ (clientId >>> 32)); - result = 31 * result + (initialized ? 1 : 0); - result = 31 * result + (readyToReceive ? 1 : 0); - result = 31 * result + (readyToSend ? 1 : 0); - return result; - } - - @Override - public String toString() { - return new StringJoiner(", ", ClientState.class.getSimpleName() + "[", "]") - .add("hasClientId=" + hasClientId) - .add("clientId=" + clientId) - .add("initialized=" + initialized) - .add("readyToReceive=" + readyToReceive) - .add("readyToSend=" + readyToSend) - .toString(); - } -} diff --git a/src/main/java/it/tdlight/common/InternalClient.java b/src/main/java/it/tdlight/common/InternalClient.java index d56dbc5..81e4d19 100644 --- a/src/main/java/it/tdlight/common/InternalClient.java +++ b/src/main/java/it/tdlight/common/InternalClient.java @@ -1,9 +1,7 @@ package it.tdlight.common; -import it.tdlight.jni.TdApi.Error; import it.tdlight.jni.TdApi.Function; import it.tdlight.jni.TdApi.Object; -import it.tdlight.jni.TdApi.Update; import it.unimi.dsi.fastutil.longs.LongArrayList; import it.unimi.dsi.fastutil.objects.ObjectArrayList; import java.util.concurrent.ConcurrentHashMap; @@ -82,21 +80,8 @@ public class InternalClient implements ClientEventsHandler, TelegramClient { } } - eventsToFilter.removeIf(event -> { - if (event instanceof Error) { - handleException(updatesHandler.getExceptionHandler(), new TDLibException((Error) event)); - return true; - } - return false; - }); - - ObjectArrayList updates = new ObjectArrayList<>(eventsToFilter.size()); - for (Object object : eventsToFilter) { - updates.add((Update) object); - } - try { - updatesHandler.getUpdatesHandler().onUpdates(updates); + updatesHandler.getUpdatesHandler().onUpdates(eventsToFilter); } catch (Throwable cause) { handleException(updatesHandler.getExceptionHandler(), cause); } @@ -117,11 +102,7 @@ public class InternalClient implements ClientEventsHandler, TelegramClient { private void handleResponse(long eventId, Object event, Handler handler) { if (handler != null) { try { - if (event instanceof Error) { - handleException(handler.getExceptionHandler(), new TDLibException((Error) event)); - } else { - handler.getResultHandler().onResult(event); - } + handler.getResultHandler().onResult(event); } catch (Throwable cause) { handleException(handler.getExceptionHandler(), cause); } diff --git a/src/main/java/it/tdlight/common/Request.java b/src/main/java/it/tdlight/common/Request.java deleted file mode 100644 index 431b129..0000000 --- a/src/main/java/it/tdlight/common/Request.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * This file is part of JTdlib. - * - * JTdlib is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License. - * - * JTdlib is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with JTdlib. If not, see . - */ - -package it.tdlight.common; - -import it.tdlight.jni.TdApi.Function; - -/** - * A request to the TDLib. - */ -public class Request { - private long id; - private Function function; - - /** - * Creates a request with eventId and function. - * @param id Request identifier. Responses to TDLib requests will have the same id as the corresponding request. Updates from TDLib will have id == 0, incoming requests are thus disallowed to have id == 0. - * @param function TDLib API function representing a request to TDLib. - */ - public Request(long id, Function function) { - this.id = id; - this.function = function; - } - - /** - * Get request identifier. - * @return Request identifier. Responses to TDLib requests will have the same id as the corresponding request. Updates from TDLib will have id == 0, incoming requests are thus disallowed to have id == 0. - */ - public long getId() { - return this.id; - } - - /** - * Get TDLib API function. - * @return TDLib API function representing a request to TDLib. - */ - public Function getFunction() { - return this.function; - } -} diff --git a/src/main/java/it/tdlight/common/ResponseArray.java b/src/main/java/it/tdlight/common/ResponseArray.java deleted file mode 100644 index 7deb323..0000000 --- a/src/main/java/it/tdlight/common/ResponseArray.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * This file is part of JTdlib. - * - * JTdlib is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License. - * - * JTdlib is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with JTdlib. If not, see . - */ - -package it.tdlight.common; - -/** - * An array of incoming updates from TDLib. - */ -public class ResponseArray { - private Response[] responses; - - public ResponseArray(Response[] responses) { - this.responses = responses; - } - - public Response[] getResponses() { - return responses; - } -} diff --git a/src/main/java/it/tdlight/common/ResponseReceiver.java b/src/main/java/it/tdlight/common/ResponseReceiver.java index 2e6071b..a76865c 100644 --- a/src/main/java/it/tdlight/common/ResponseReceiver.java +++ b/src/main/java/it/tdlight/common/ResponseReceiver.java @@ -2,6 +2,7 @@ package it.tdlight.common; import static it.tdlight.common.InternalClient.clientInitializationLock; +import it.tdlight.common.utils.IntSwapper; import it.tdlight.jni.TdApi; import it.tdlight.jni.TdApi.Object; import java.util.ArrayList; diff --git a/src/main/java/it/tdlight/common/TDLibException.java b/src/main/java/it/tdlight/common/TDLibException.java deleted file mode 100644 index b807674..0000000 --- a/src/main/java/it/tdlight/common/TDLibException.java +++ /dev/null @@ -1,21 +0,0 @@ -package it.tdlight.common; - -import it.tdlight.jni.TdApi.Error; - -public class TDLibException extends RuntimeException { - - private final Error event; - - public TDLibException(Error event) { - super(event.code + ": " + event.message); - this.event = event; - } - - public int getErrorCode() { - return event.code; - } - - public String getErrorMessage() { - return event.message; - } -} diff --git a/src/main/java/it/tdlight/common/TelegramClient.java b/src/main/java/it/tdlight/common/TelegramClient.java index 81e05b7..284cd4d 100644 --- a/src/main/java/it/tdlight/common/TelegramClient.java +++ b/src/main/java/it/tdlight/common/TelegramClient.java @@ -35,7 +35,7 @@ public interface TelegramClient { * Synchronously executes a TDLib request. Only a few marked accordingly requests can be executed synchronously. * * @param query Object representing a query to the TDLib. - * @return request result. + * @return request result or {@link TdApi.Error}. * @throws NullPointerException if query is null. */ TdApi.Object execute(TdApi.Function query); diff --git a/src/main/java/it/tdlight/common/UpdatesHandler.java b/src/main/java/it/tdlight/common/UpdatesHandler.java index 55aebbb..8ca463d 100644 --- a/src/main/java/it/tdlight/common/UpdatesHandler.java +++ b/src/main/java/it/tdlight/common/UpdatesHandler.java @@ -1,6 +1,6 @@ package it.tdlight.common; -import it.tdlight.jni.TdApi.Update; +import it.tdlight.jni.TdApi.Object; import java.util.List; /** @@ -11,7 +11,7 @@ public interface UpdatesHandler { /** * Callback called on incoming update from TDLib. * - * @param object Updates of type TdApi.Update about new events. + * @param object Updates of type {@link it.tdlight.jni.TdApi.Update} about new events, or {@link it.tdlight.jni.TdApi.Error}. */ - void onUpdates(List object); + void onUpdates(List object); } \ No newline at end of file diff --git a/src/main/java/it/tdlight/common/IntSwapper.java b/src/main/java/it/tdlight/common/utils/IntSwapper.java similarity index 76% rename from src/main/java/it/tdlight/common/IntSwapper.java rename to src/main/java/it/tdlight/common/utils/IntSwapper.java index 9ab1a85..f024d92 100644 --- a/src/main/java/it/tdlight/common/IntSwapper.java +++ b/src/main/java/it/tdlight/common/utils/IntSwapper.java @@ -1,8 +1,8 @@ -package it.tdlight.common; +package it.tdlight.common.utils; import it.unimi.dsi.fastutil.Swapper; -class IntSwapper implements Swapper { +public class IntSwapper implements Swapper { private final int[] array; int tmp;