Move MessageKey from Message.kt to MessageKey.kt

This commit is contained in:
Him188 2020-12-23 17:51:13 +08:00
parent 77a4aa8977
commit 21cb125984
2 changed files with 51 additions and 48 deletions

View File

@ -320,54 +320,6 @@ public interface ConstrainSingle : SingleMessage {
public val key: MessageKey<*>
}
/**
* 独立的 [MessageKey] 的实现. '独立' `final`, 不支持多态类型. 适用于作为最顶层的 [MessageKey], [MessageSource].
*
* @see AbstractPolymorphicMessageKey
*/
@ExperimentalMessageKey
public abstract class AbstractMessageKey<out M : SingleMessage>(
override val safeCast: (SingleMessage) -> M?,
) : MessageKey<M>
/**
* 多态 [MessageKey].
*
* 示例: [HummerMessage]
* ```
* MessageContent
*
* HummerMessage
*
* +------------+-------------+
* | | |
* PokeMessage VipFace FlashImage
*
* ```
*
* [连接][Message.plus] 一个 [VipFace] 到一个 [MessageChain] ,
* 由于 [VipFace] 最上层为 [MessageContent], 消息链中第一个 [MessageContent] 会被 (保留顺序地) 替换为 [VipFace], 其他所有 [MessageContent] 都会被删除.
*
* :
* ```
* val source: MessageSource = ...
*
* val result = messageChainOf(PlainText("a"), PlainText("b"), source, AtAll) + VipFace.LiuLian
* // result 为 [VipFace.LiuLian, source]
*
* val result = source1 + source2
* // result 为 [source2], 总是右侧替换左侧
* ```
*/
@ExperimentalMessageKey
public abstract class AbstractPolymorphicMessageKey<out B : SingleMessage, out M : B>(
baseKey: MessageKey<B>,
safeCast: (SingleMessage) -> M?,
) : MessageKey<M>, AbstractMessageKey<M>(safeCast) {
internal val topmostKey: MessageKey<*> =
if (baseKey is AbstractPolymorphicMessageKey<*, *>) baseKey.topmostKey else baseKey
}
/**
* 带内容的消息.
*

View File

@ -30,6 +30,57 @@ public interface MessageKey<out M : SingleMessage> {
public val safeCast: (SingleMessage) -> M?
}
/**
* 独立的 [MessageKey] 的实现. '独立' `final`, 不支持多态类型. 适用于作为最顶层的 [MessageKey], [MessageSource].
*
* @see AbstractPolymorphicMessageKey
*/
@ExperimentalMessageKey
public abstract class AbstractMessageKey<out M : SingleMessage>(
override val safeCast: (SingleMessage) -> M?,
) : MessageKey<M>
/**
* 多态 [MessageKey].
*
* 示例: [HummerMessage]
* ```
* MessageContent
*
* HummerMessage
*
* +------------+-------------+
* | | |
* PokeMessage VipFace FlashImage
*
* ```
*
* [连接][Message.plus] 一个 [VipFace] 到一个 [MessageChain] ,
* 由于 [VipFace] 最上层为 [MessageContent], 消息链中第一个 [MessageContent] 会被 (保留顺序地) 替换为 [VipFace], 其他所有 [MessageContent] 都会被删除.
*
* :
* ```
* val source: MessageSource = ...
*
* val result = messageChainOf(PlainText("a"), PlainText("b"), source, AtAll) + VipFace.LiuLian
* // result 为 [VipFace.LiuLian, source]
*
* val result = source1 + source2
* // result 为 [source2], 总是右侧替换左侧
* ```
*/
@ExperimentalMessageKey
public abstract class AbstractPolymorphicMessageKey<out B : SingleMessage, out M : B>(
baseKey: MessageKey<B>,
safeCast: (SingleMessage) -> M?,
) : MessageKey<M>, AbstractMessageKey<M>(safeCast) {
internal val topmostKey: MessageKey<*> =
if (baseKey is AbstractPolymorphicMessageKey<*, *>) baseKey.topmostKey else baseKey
}
@ExperimentalMessageKey
public fun MessageKey<*>.isInstance(message: SingleMessage): Boolean = this.safeCast(message) != null