|
|
@@ -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 <reified T> Any?.cast(): T {
|
|
|
contract { returns() implies (this@cast is T) }
|
|
|
@@ -44,136 +43,6 @@ public inline fun <reified R> Iterable<*>.firstIsInstanceOrNull(): R? {
|
|
|
}
|
|
|
|
|
|
|
|
|
-@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
|
|
|
[email protected]
|
|
|
[email protected]
|
|
|
-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")
|
|
|
[email protected]
|
|
|
[email protected]
|
|
|
-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")
|
|
|
[email protected]
|
|
|
[email protected]
|
|
|
-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")
|
|
|
[email protected]
|
|
|
-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")
|
|
|
[email protected]
|
|
|
-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")
|
|
|
[email protected]
|
|
|
-public inline fun <R> runCatchingExceptions(block: () -> R): Result<R> {
|
|
|
- return try {
|
|
|
- Result.success(block())
|
|
|
- } catch (e: Exception) {
|
|
|
- Result.failure(e)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
public inline fun <E> MutableList<E>.replaceAllKotlin(operator: (E) -> E) {
|
|
|
val li: MutableListIterator<E> = this.listIterator()
|
|
|
while (li.hasNext()) {
|
|
|
@@ -181,16 +50,6 @@ public inline fun <E> MutableList<E>.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
|