-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
192 additions
and
141 deletions.
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
mirai-core-utils/src/commonMain/kotlin/ResultExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <R, T : R> Result<T>.recoverCatchingSuppressed(transform: (exception: Throwable) -> R): Result<R> { | ||
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 <R> retryCatching( | ||
n: Int, | ||
except: KClass<out Throwable>? = null, | ||
block: (count: Int, lastException: Throwable?) -> R, | ||
): Result<R> { | ||
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 <R> retryCatchingExceptions( | ||
n: Int, | ||
except: KClass<out Exception>? = null, | ||
block: (count: Int, lastException: Throwable?) -> R, | ||
): Result<R> { | ||
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 <R> retryCatching( | ||
n: Int, | ||
except: KClass<out Throwable>? = null, | ||
block: () -> R, | ||
): Result<R> { | ||
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 <R> retryCatchingExceptions( | ||
n: Int, | ||
except: KClass<out Exception>? = null, | ||
block: () -> R, | ||
): Result<R> { | ||
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 <R> runCatchingExceptions(block: () -> R): Result<R> { | ||
return try { | ||
Result.success(block()) | ||
} catch (e: Exception) { | ||
Result.failure(e) | ||
} | ||
} | ||
|
||
public inline fun <R> Result<R>.mapFailure( | ||
block: (Throwable) -> Throwable, | ||
): Result<R> = onFailure { | ||
return Result.failure(block(it)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Boolean>() | ||
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}" | ||
} | ||
} |