From dc02278c112e7870860248a1c6aeb8adaa566b8f Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 26 Feb 2020 13:06:18 +0800 Subject: [PATCH] LockFreeLinkedList: fix `peekFirst` --- .../net.mamoe.mirai/utils/LockFreeLinkedList.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt index 95e45b76d..3495a9c31 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt @@ -111,9 +111,13 @@ open class LockFreeLinkedList { } } - open fun peekFirst(): E = head.nextNode.letValueIfValid { return it } ?: throw NoSuchElementException() - - open fun peekLast(): E = head.iterateBeforeFirst { it === tail }.letValueIfValid { return it } ?: throw NoSuchElementException() + open fun peekFirst(): E { + return head + .iterateBeforeFirst { it === tail } + .takeUnless { it.isTail() } + ?.nodeValue + ?: throw NoSuchElementException() + } open fun removeLast(): E { while (true) { @@ -770,11 +774,7 @@ open class LockFreeLinkedListNode( internal val nextNodeRef: AtomicRef> = atomic(nextNode ?: this) inline fun letValueIfValid(block: (E) -> R): R? { - if (!this.isValidElementNode()) { - return null - } - val value = this.nodeValue - return if (value !== null) block(value) else null + return this.takeIf { isValidElementNode() }?.nodeValue?.let(block) } /**