From 600b02f3080e757aff66455e654f72a04d742d9c Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Thu, 8 Sep 2022 03:36:56 +0800 Subject: [PATCH] Added conditions function in UtilsFactory --- .../yukihookapi/hook/utils/UtilsFactory.kt | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/utils/UtilsFactory.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/utils/UtilsFactory.kt index 2aee04ea..7533cad2 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/utils/UtilsFactory.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/utils/UtilsFactory.kt @@ -110,6 +110,67 @@ internal class RunBlockResult(internal val afterMs: Long) { internal inline fun result(result: (Long) -> Unit) = result(afterMs) } +/** + * 创建多项条件判断 - 条件对象 [T] + * @param initiate 方法体 + * @return [Conditions.Result] + */ +internal inline fun T.conditions(initiate: Conditions.() -> Unit) = Conditions(value = this).apply(initiate).build() + +/** + * 构造条件判断类 + * @param value 当前条件对象 + */ +internal class Conditions(internal var value: T) { + + /** 全部判断条件数组 */ + private val conditions = ArrayList() + + /** + * 添加与 (and) 条件 + * @param value 条件值 + */ + internal fun and(value: Boolean) { + conditions.add(value) + } + + /** + * 结束方法体 + * @return [Result] + */ + internal fun build() = Result() + + /** + * 构造条件判断结果类 + */ + inner class Result internal constructor() { + + /** + * 获取条件判断结果 + * @return [Boolean] + */ + private val result by lazy { conditions.takeIf { it.isNotEmpty() }?.any { it.not() }?.not() == true } + + /** + * 当条件成立 + * @param callback 回调 + */ + internal inline fun finally(callback: () -> Unit): Result { + if (result) callback() + return this + } + + /** + * 当条件不成立 + * @param callback 回调 + */ + internal inline fun without(callback: () -> Unit): Result { + if (result.not()) callback() + return this + } + } +} + /** * 获取 [ModifyValue] 对象 * @return [ModifyValue]