mirror of
https://github.com/mamoe/mirai.git
synced 2025-04-14 23:20:49 +08:00
Improve performance of MessageChain.get
, MessageChain.contains
. Add relevant tests.
This commit is contained in:
parent
850fcd0afe
commit
baf1ca618e
mirai-core-api/src
@ -209,8 +209,10 @@ public sealed interface MessageChain :
|
||||
*
|
||||
* @see MessageChain.getOrFail 在找不到此类型的元素时抛出 [NoSuchElementException]
|
||||
*/
|
||||
public operator fun <M : SingleMessage> get(key: MessageKey<M>): M? =
|
||||
asSequence().mapNotNull { key.safeCast.invoke(it) }.firstOrNull()
|
||||
public operator fun <M : SingleMessage> get(key: MessageKey<M>): M? {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return firstOrNull { key.safeCast.invoke(it) != null } as M?
|
||||
}
|
||||
|
||||
/**
|
||||
* 当存在 [ConstrainSingle.key] 为 [key] 的 [SingleMessage] 实例时返回 `true`.
|
||||
@ -239,7 +241,7 @@ public sealed interface MessageChain :
|
||||
* @see MessageChain.getOrFail 在找不到此类型的元素时抛出 [NoSuchElementException]
|
||||
*/
|
||||
public operator fun <M : SingleMessage> contains(key: MessageKey<M>): Boolean =
|
||||
asSequence().any { key.safeCast.invoke(it) != null }
|
||||
any { key.safeCast.invoke(it) != null }
|
||||
|
||||
@MiraiExperimentalApi
|
||||
override fun appendMiraiCodeTo(builder: StringBuilder) {
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2019-2020 Mamoe Technologies and contributors.
|
||||
* 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.
|
||||
@ -12,6 +12,7 @@ package net.mamoe.mirai.message.data
|
||||
import net.mamoe.mirai.utils.safeCast
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertSame
|
||||
|
||||
|
||||
private open class TestStandaloneConstrainSingleMessage : ConstrainSingle, MessageContent {
|
||||
@ -98,4 +99,26 @@ internal class MessageKeyTest {
|
||||
assertEquals<Any?>(constrainSingle, chain[TestStandaloneConstrainSingleMessage])
|
||||
assertEquals(1, chain.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can get from MessageChain`() {
|
||||
val cons = TestStandaloneConstrainSingleMessage()
|
||||
assertSame(cons, messageChainOf(cons)[TestStandaloneConstrainSingleMessage])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can get from MessageChain2`() {
|
||||
val cons = TestStandaloneConstrainSingleMessage()
|
||||
assertSame(cons, messageChainOf(PlainText("test"), AtAll, cons)[TestStandaloneConstrainSingleMessage])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get can be null`() {
|
||||
assertEquals(null, messageChainOf()[TestStandaloneConstrainSingleMessage])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get can be null2`() {
|
||||
assertEquals(null, messageChainOf(PlainText("test"), AtAll)[TestStandaloneConstrainSingleMessage])
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user