From 2b4d9a8e79198ad20a15cd5cb034eacc3b30c2d4 Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 18 Feb 2020 13:35:14 +0800 Subject: [PATCH] Add tryNTimes --- .../net/mamoe/mirai/utils/addSuppressed.kt | 16 +++++ .../kotlin/net.mamoe.mirai/utils/tryNTimes.kt | 71 +++++++++++++++++++ .../net/mamoe/mirai/utils/addSuppressed.kt | 16 +++++ 3 files changed, 103 insertions(+) create mode 100644 mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/addSuppressed.kt create mode 100644 mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/tryNTimes.kt create mode 100644 mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/addSuppressed.kt diff --git a/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/addSuppressed.kt b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/addSuppressed.kt new file mode 100644 index 000000000..b153e249b --- /dev/null +++ b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/addSuppressed.kt @@ -0,0 +1,16 @@ +package net.mamoe.mirai.utils + +private var isAddSuppressedSupported: Boolean = true + +@MiraiInternalAPI +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +actual fun Throwable.addSuppressed(e: Throwable) { + if (!isAddSuppressedSupported) { + return + } + try { + this.addSuppressed(e) + } catch (e: Exception) { + isAddSuppressedSupported = false + } +} \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/tryNTimes.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/tryNTimes.kt new file mode 100644 index 000000000..b401974b7 --- /dev/null +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/tryNTimes.kt @@ -0,0 +1,71 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.utils + +@MiraiInternalAPI +expect fun Throwable.addSuppressed(e: Throwable) + +@MiraiInternalAPI +@Suppress("DuplicatedCode") +inline fun tryNTimes(repeat: Int, block: () -> R): R { + var lastException: Throwable? = null + + repeat(repeat) { + try { + return block() + } catch (e: Throwable) { + if (lastException == null) { + lastException = e + } + lastException!!.addSuppressed(e) + } + } + + throw lastException!! +} + +@MiraiInternalAPI +@Suppress("DuplicatedCode") +inline fun tryNTimesOrNull(repeat: Int, block: () -> R): R? { + var lastException: Throwable? = null + + repeat(repeat) { + try { + return block() + } catch (e: Throwable) { + if (lastException == null) { + lastException = e + } + lastException!!.addSuppressed(e) + } + } + + return null +} + +@MiraiInternalAPI +@Suppress("DuplicatedCode") +inline fun tryNTimesOrException(repeat: Int, block: () -> R): Throwable? { + var lastException: Throwable? = null + + repeat(repeat) { + try { + block() + return null + } catch (e: Throwable) { + if (lastException == null) { + lastException = e + } + lastException!!.addSuppressed(e) + } + } + + return lastException!! +} \ No newline at end of file diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/addSuppressed.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/addSuppressed.kt new file mode 100644 index 000000000..b153e249b --- /dev/null +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/addSuppressed.kt @@ -0,0 +1,16 @@ +package net.mamoe.mirai.utils + +private var isAddSuppressedSupported: Boolean = true + +@MiraiInternalAPI +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +actual fun Throwable.addSuppressed(e: Throwable) { + if (!isAddSuppressedSupported) { + return + } + try { + this.addSuppressed(e) + } catch (e: Exception) { + isAddSuppressedSupported = false + } +} \ No newline at end of file