From 41733e23ef327ed6661e2eb1a4d830a92cc3257c Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 29 Jun 2021 15:14:56 +0800 Subject: [PATCH] Extract utils into multiple files --- .../src/commonMain/kotlin/ResultExtensions.kt | 153 ++++++++++++++++++ .../src/commonMain/kotlin/StandardUtils.kt | 141 ---------------- .../src/commonMain/kotlin/systemProp.kt | 39 +++++ 3 files changed, 192 insertions(+), 141 deletions(-) create mode 100644 mirai-core-utils/src/commonMain/kotlin/ResultExtensions.kt create mode 100644 mirai-core-utils/src/commonMain/kotlin/systemProp.kt diff --git a/mirai-core-utils/src/commonMain/kotlin/ResultExtensions.kt b/mirai-core-utils/src/commonMain/kotlin/ResultExtensions.kt new file mode 100644 index 000000000..645ca7046 --- /dev/null +++ b/mirai-core-utils/src/commonMain/kotlin/ResultExtensions.kt @@ -0,0 +1,153 @@ +/* + * Copyright 2019-2021 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 + */ + + +@file:JvmMultifileClass +@file:JvmName("MiraiUtils") + +package net.mamoe.mirai.utils + +import kotlin.reflect.KClass + + +@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") +@kotlin.internal.InlineOnly +@kotlin.internal.LowPriorityInOverloadResolution +public inline fun Result.recoverCatchingSuppressed(transform: (exception: Throwable) -> R): Result { + return when (val exception = exceptionOrNull()) { + null -> this + else -> { + try { + Result.success(transform(exception)) + } catch (e: Throwable) { + e.addSuppressed(exception) + Result.failure(e) + } + } + } +} + +@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") +@kotlin.internal.InlineOnly +@kotlin.internal.LowPriorityInOverloadResolution +public inline fun retryCatching( + n: Int, + except: KClass? = null, + block: (count: Int, lastException: Throwable?) -> R, +): Result { + require(n >= 0) { + "param n for retryCatching must not be negative" + } + var exception: Throwable? = null + repeat(n) { + try { + return Result.success(block(it, exception)) + } catch (e: Throwable) { + if (except?.isInstance(e) == true) { + return Result.failure(e) + } + exception?.addSuppressed(e) + exception = e + } + } + return Result.failure(exception!!) +} + +@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") +@kotlin.internal.InlineOnly +@kotlin.internal.LowPriorityInOverloadResolution +public inline fun retryCatchingExceptions( + n: Int, + except: KClass? = null, + block: (count: Int, lastException: Throwable?) -> R, +): Result { + require(n >= 0) { + "param n for retryCatching must not be negative" + } + var exception: Throwable? = null + repeat(n) { + try { + return Result.success(block(it, exception)) + } catch (e: Exception) { + if (except?.isInstance(e) == true) { + return Result.failure(e) + } + exception?.addSuppressed(e) + exception = e + } + } + return Result.failure(exception!!) +} + + +@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") +@kotlin.internal.InlineOnly +public inline fun retryCatching( + n: Int, + except: KClass? = null, + block: () -> R, +): Result { + require(n >= 0) { + "param n for retryCatching must not be negative" + } + var exception: Throwable? = null + repeat(n) { + try { + return Result.success(block()) + } catch (e: Throwable) { + if (except?.isInstance(e) == true) { + return Result.failure(e) + } + exception?.addSuppressed(e) + exception = e + } + } + return Result.failure(exception!!) +} + +@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") +@kotlin.internal.InlineOnly +public inline fun retryCatchingExceptions( + n: Int, + except: KClass? = null, + block: () -> R, +): Result { + require(n >= 0) { + "param n for retryCatching must not be negative" + } + var exception: Throwable? = null + repeat(n) { + try { + return Result.success(block()) + } catch (e: Exception) { + if (except?.isInstance(e) == true) { + return Result.failure(e) + } + exception?.addSuppressed(e) + exception = e + } + } + return Result.failure(exception!!) +} + +@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") +@kotlin.internal.InlineOnly +public inline fun runCatchingExceptions(block: () -> R): Result { + return try { + Result.success(block()) + } catch (e: Exception) { + Result.failure(e) + } +} + +public inline fun Result.mapFailure( + block: (Throwable) -> Throwable, +): Result = onFailure { + return Result.failure(block(it)) +} \ No newline at end of file diff --git a/mirai-core-utils/src/commonMain/kotlin/StandardUtils.kt b/mirai-core-utils/src/commonMain/kotlin/StandardUtils.kt index ad47244f1..6bb00e75a 100644 --- a/mirai-core-utils/src/commonMain/kotlin/StandardUtils.kt +++ b/mirai-core-utils/src/commonMain/kotlin/StandardUtils.kt @@ -15,7 +15,6 @@ package net.mamoe.mirai.utils import java.util.* import kotlin.contracts.InvocationKind import kotlin.contracts.contract -import kotlin.reflect.KClass public inline fun Any?.cast(): T { contract { returns() implies (this@cast is T) } @@ -44,136 +43,6 @@ public inline fun Iterable<*>.firstIsInstanceOrNull(): R? { } -@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") -@kotlin.internal.InlineOnly -@kotlin.internal.LowPriorityInOverloadResolution -public inline fun Result.recoverCatchingSuppressed(transform: (exception: Throwable) -> R): Result { - return when (val exception = exceptionOrNull()) { - null -> this - else -> { - try { - Result.success(transform(exception)) - } catch (e: Throwable) { - e.addSuppressed(exception) - Result.failure(e) - } - } - } -} - -@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") -@kotlin.internal.InlineOnly -@kotlin.internal.LowPriorityInOverloadResolution -public inline fun retryCatching( - n: Int, - except: KClass? = null, - block: (count: Int, lastException: Throwable?) -> R -): Result { - require(n >= 0) { - "param n for retryCatching must not be negative" - } - var exception: Throwable? = null - repeat(n) { - try { - return Result.success(block(it, exception)) - } catch (e: Throwable) { - if (except?.isInstance(e) == true) { - return Result.failure(e) - } - exception?.addSuppressed(e) - exception = e - } - } - return Result.failure(exception!!) -} - -@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") -@kotlin.internal.InlineOnly -@kotlin.internal.LowPriorityInOverloadResolution -public inline fun retryCatchingExceptions( - n: Int, - except: KClass? = null, - block: (count: Int, lastException: Throwable?) -> R -): Result { - require(n >= 0) { - "param n for retryCatching must not be negative" - } - var exception: Throwable? = null - repeat(n) { - try { - return Result.success(block(it, exception)) - } catch (e: Exception) { - if (except?.isInstance(e) == true) { - return Result.failure(e) - } - exception?.addSuppressed(e) - exception = e - } - } - return Result.failure(exception!!) -} - - -@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") -@kotlin.internal.InlineOnly -public inline fun retryCatching( - n: Int, - except: KClass? = null, - block: () -> R -): Result { - require(n >= 0) { - "param n for retryCatching must not be negative" - } - var exception: Throwable? = null - repeat(n) { - try { - return Result.success(block()) - } catch (e: Throwable) { - if (except?.isInstance(e) == true) { - return Result.failure(e) - } - exception?.addSuppressed(e) - exception = e - } - } - return Result.failure(exception!!) -} - -@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") -@kotlin.internal.InlineOnly -public inline fun retryCatchingExceptions( - n: Int, - except: KClass? = null, - block: () -> R -): Result { - require(n >= 0) { - "param n for retryCatching must not be negative" - } - var exception: Throwable? = null - repeat(n) { - try { - return Result.success(block()) - } catch (e: Exception) { - if (except?.isInstance(e) == true) { - return Result.failure(e) - } - exception?.addSuppressed(e) - exception = e - } - } - return Result.failure(exception!!) -} - -@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") -@kotlin.internal.InlineOnly -public inline fun runCatchingExceptions(block: () -> R): Result { - return try { - Result.success(block()) - } catch (e: Exception) { - Result.failure(e) - } -} - public inline fun MutableList.replaceAllKotlin(operator: (E) -> E) { val li: MutableListIterator = this.listIterator() while (li.hasNext()) { @@ -181,16 +50,6 @@ public inline fun MutableList.replaceAllKotlin(operator: (E) -> E) { } } -public fun systemProp(name: String, default: String): String = - System.getProperty(name, default) ?: default - -public fun systemProp(name: String, default: Boolean): Boolean = - System.getProperty(name, default.toString())?.toBoolean() ?: default - - -public fun systemProp(name: String, default: Long): Long = - System.getProperty(name, default.toString())?.toLongOrNull() ?: default - public fun Throwable.getRootCause(maxDepth: Int = 20): Throwable { var depth = 0 diff --git a/mirai-core-utils/src/commonMain/kotlin/systemProp.kt b/mirai-core-utils/src/commonMain/kotlin/systemProp.kt new file mode 100644 index 000000000..3c9faf301 --- /dev/null +++ b/mirai-core-utils/src/commonMain/kotlin/systemProp.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2019-2021 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 + */ + + +@file:JvmMultifileClass +@file:JvmName("MiraiUtils") + +package net.mamoe.mirai.utils + +import java.util.concurrent.ConcurrentHashMap + + +public fun systemProp(name: String, default: String): String = + System.getProperty(name, default) ?: default + +public fun systemProp(name: String, default: Boolean): Boolean = + System.getProperty(name, default.toString())?.toBoolean() ?: default + + +public fun systemProp(name: String, default: Long): Long = + System.getProperty(name, default.toString())?.toLongOrNull() ?: default + + +private val debugProps = ConcurrentHashMap() +public fun Any?.toDebugString(prop: String, default: Boolean = false): String { + if (this == null) return "null" + val debug = debugProps.getOrPut(prop) { systemProp(prop, default) } + return if (debug) { + "${this::class.simpleName}($this)" + } else { + "${this::class.simpleName}" + } +} \ No newline at end of file