diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt index 2fcd52f8c..2efa4d328 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt @@ -106,11 +106,9 @@ internal class BotImpl @PublishedApi internal constructor( // endregion // region contacts - @UseExperimental(MiraiInternalAPI::class) - override val groups: ContactList = ContactList(MutableContactList()) + override val groups: ContactList = ContactList(LockFreeLinkedList()) - @UseExperimental(MiraiInternalAPI::class) - override val qqs: ContactList = ContactList(MutableContactList()) + override val qqs: ContactList = ContactList(LockFreeLinkedList()) /** * 线程安全地获取缓存的 QQ 对象. 若没有对应的缓存, 则会创建一个. diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/ContactList.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/ContactList.kt index b3aab356c..46ac648d5 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/ContactList.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/ContactList.kt @@ -12,7 +12,7 @@ import net.mamoe.mirai.utils.joinToString */ @UseExperimental(MiraiInternalAPI::class) @Suppress("unused") -class ContactList(@PublishedApi internal val delegate: MutableContactList) { +class ContactList(@PublishedApi internal val delegate: LockFreeLinkedList) { /** * ID 列表的字符串表示. * 如: @@ -35,22 +35,14 @@ class ContactList(@PublishedApi internal val delegate: MutableConta override fun toString(): String = delegate.joinToString(separator = ", ", prefix = "ContactList(", postfix = ")") } -/** - * 可修改联系人列表. 只会在内部使用. - */ -@MiraiInternalAPI -class MutableContactList : LockFreeLinkedList() { - override fun toString(): String = joinToString(separator = ", ", prefix = "MutableContactList(", postfix = ")") +operator fun LockFreeLinkedList.get(id: UInt): C { + forEach { if (it.id == id) return it } + throw NoSuchElementException() +} - operator fun get(id: UInt): C { - forEach { if (it.id == id) return it } - throw NoSuchElementException() - } +fun LockFreeLinkedList.getOrNull(id: UInt): C? { + forEach { if (it.id == id) return it } + return null +} - fun getOrNull(id: UInt): C? { - forEach { if (it.id == id) return it } - return null - } - - fun getOrAdd(id: UInt, supplier: () -> C): C = super.filteringGetOrAdd({it.id == id}, supplier) -} \ No newline at end of file +fun LockFreeLinkedList.getOrAdd(id: UInt, supplier: () -> C): C = filteringGetOrAdd({ it.id == id }, supplier) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/GroupPacket.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/GroupPacket.kt index 3595c197d..2f7b1bcf3 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/GroupPacket.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/GroupPacket.kt @@ -10,6 +10,7 @@ import net.mamoe.mirai.message.internal.toPacket import net.mamoe.mirai.network.BotNetworkHandler import net.mamoe.mirai.network.protocol.tim.TIMProtocol import net.mamoe.mirai.network.protocol.tim.packet.* +import net.mamoe.mirai.utils.LockFreeLinkedList import net.mamoe.mirai.utils.MiraiInternalAPI import net.mamoe.mirai.utils.io.* import net.mamoe.mirai.withSession @@ -54,7 +55,7 @@ internal data class RawGroupInfo( @Suppress("NOTHING_TO_INLINE") // this function it only executed in one place. @UseExperimental(MiraiInternalAPI::class) inline fun parseBy(group: Group): GroupInfo = group.bot.withSession { - val memberList = MutableContactList() + val memberList = LockFreeLinkedList() members.forEach { entry: Map.Entry -> entry.key.qq().let { group.Member(it, entry.value, it.coroutineContext) } }