From df979c9e676f9f7a4c01c47e8d1062cf774b6f21 Mon Sep 17 00:00:00 2001 From: SayantanRC Date: Tue, 7 Jun 2022 04:23:45 +0530 Subject: [PATCH 1/4] issue_313 [WIP]: commit ResultSupreme.kt with documents. --- .../foundation/e/apps/api/ResultSupreme.kt | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 app/src/main/java/foundation/e/apps/api/ResultSupreme.kt diff --git a/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt b/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt new file mode 100644 index 000000000..02ece3ebd --- /dev/null +++ b/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2022 ECORP + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License + * along with this program. If not, see . + */ + +package foundation.e.apps.api + +import foundation.e.apps.utils.enums.ResultStatus +import java.util.concurrent.TimeoutException + +/** + * Another implementation of Result class. + * This removes the use of [ResultStatus] class for different status. + * This class also follows the standard code patterns. However, we still have the same + * flaw that [data] is nullable. As such we may have to add extra null checks or just + * brute force with !! + * + * Also since for each case we now use an inner class with slightly different name, + * we need some refactoring. + * + * Issue: https://gitlab.e.foundation/e/os/backlog/-/issues/313 + */ +sealed class ResultSupreme { + + /** + * Success case. + * Use [isSuccess] to check. + * + * @param data End result of processing. + */ + class Success(data: T): ResultSupreme() { + init { this.data = data } + } + + /** + * Timed out during network related job. + * Use [isTimeout] to check. + * + * @param data The process is expected to output some blank data, but it cannot be null. + * Example can be an empty list. + * @param exception Optional exception from try-catch block. + */ + class Timeout(data: T, exception: Exception = TimeoutException()) : + ResultSupreme() { + init { + this.data = data + this.exception = exception + } + } + + /** + * Miscellaneous error case. + * No valid data from processing. + * Use [isUnknownError] to check. + */ + class Error() : ResultSupreme() { + /** + * @param message A String message to log or display to the user. + * @param exception Optional exception from try-catch block. + */ + constructor(message: String, exception: Exception = Exception()) : this() { + this.message = message + this.exception = exception + } + + /** + * @param data Non-null data. Example a String which could not be parsed into a JSON. + * @param message A optional String message to log or display to the user. + */ + constructor(data: T, message: String = "") : this() { + this.data = data + this.message = message + } + } + + /** + * Data from processing. May be null. + */ + var data: T? = null + + /** + * A custom string message for logging or displaying to the user. + */ + var message: String = "" + + /** + * Exception from try-catch block for error cases. + */ + var exception: Exception = Exception() + + fun isValidData() = data != null + + fun isSuccess() = this is Success && isValidData() + fun isTimeout() = this is Timeout + fun isUnknownError() = this is Error +} \ No newline at end of file -- GitLab From e10bca6d38de08fabeb474b4af6963785e0ec531 Mon Sep 17 00:00:00 2001 From: SayantanRC Date: Tue, 7 Jun 2022 15:28:00 +0530 Subject: [PATCH 2/4] issue_313 [WIP]: ResultSupreme create() function that uses ResultStatus to create instance. --- .../foundation/e/apps/api/ResultSupreme.kt | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt b/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt index 02ece3ebd..15f2c7c89 100644 --- a/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt +++ b/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt @@ -105,4 +105,33 @@ sealed class ResultSupreme { fun isSuccess() = this is Success && isValidData() fun isTimeout() = this is Timeout fun isUnknownError() = this is Error + + companion object { + + /** + * Function to create an instance of ResultSupreme from a [ResultStatus] status, + * and other available info - [data], [message], [exception]. + */ + fun create( + status: ResultStatus, + data: T? = null, + message: String = "", + exception: Exception = Exception(), + ): ResultSupreme { + val resultObject = when { + status == ResultStatus.OK && data!= null -> Success(data) + status == ResultStatus.TIMEOUT && data!= null -> Timeout(data) + else -> Error(message, exception) + } + resultObject.apply { + if (isUnknownError()) { + this.data = data + } else { + this.message = message + this.exception = exception + } + } + return resultObject + } + } } \ No newline at end of file -- GitLab From 11cdd416568cbfc9fa563f7873a7ee2527163870 Mon Sep 17 00:00:00 2001 From: SayantanRC Date: Tue, 7 Jun 2022 16:37:49 +0530 Subject: [PATCH 3/4] issue_313 [WIP]: ResultSupreme setData() to set non null data. --- .../main/java/foundation/e/apps/api/ResultSupreme.kt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt b/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt index 15f2c7c89..c46dfe8a9 100644 --- a/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt +++ b/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt @@ -41,7 +41,7 @@ sealed class ResultSupreme { * @param data End result of processing. */ class Success(data: T): ResultSupreme() { - init { this.data = data } + init { setData(data) } } /** @@ -55,7 +55,7 @@ sealed class ResultSupreme { class Timeout(data: T, exception: Exception = TimeoutException()) : ResultSupreme() { init { - this.data = data + setData(data) this.exception = exception } } @@ -80,7 +80,7 @@ sealed class ResultSupreme { * @param message A optional String message to log or display to the user. */ constructor(data: T, message: String = "") : this() { - this.data = data + setData(data) this.message = message } } @@ -89,6 +89,7 @@ sealed class ResultSupreme { * Data from processing. May be null. */ var data: T? = null + private set /** * A custom string message for logging or displaying to the user. @@ -106,6 +107,10 @@ sealed class ResultSupreme { fun isTimeout() = this is Timeout fun isUnknownError() = this is Error + fun setData(data: T) { + this.data = data + } + companion object { /** -- GitLab From 804ff87a5d2115d7ea9b6256fee392e364fd23ea Mon Sep 17 00:00:00 2001 From: SayantanRC Date: Wed, 8 Jun 2022 01:51:22 +0530 Subject: [PATCH 4/4] issue_313 [WIP]: create ResultSupreme.replicate method --- .../foundation/e/apps/api/ResultSupreme.kt | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt b/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt index c46dfe8a9..444c61228 100644 --- a/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt +++ b/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt @@ -138,5 +138,31 @@ sealed class ResultSupreme { } return resultObject } + + /** + * Create a similar [ResultSupreme] instance i.e. of type [Success], [Timeout]... + * using a supplied [result] object but with a different generic type and new data. + * + * @param result Class of [ResultSupreme] whose replica is to be made. + * @param newData Nullable new data for this replica. + * @param message Optional new message for this replica. If not provided, + * the new object will get the message from [result]. + * @param exception Optional new exception for this replica. If not provided, + * the new object will get the exception from [result]. + */ + fun replicate( + result: ResultSupreme<*>, + newData: T?, + message: String? = null, + exception: Exception? = null, + ): ResultSupreme { + val status = when (result) { + is Success -> ResultStatus.OK + is Timeout -> ResultStatus.TIMEOUT + is Error -> ResultStatus.UNKNOWN + } + return create(status, newData, message ?: result.message, + exception ?: result.exception) + } } } \ No newline at end of file -- GitLab