Skip to content

Commit

Permalink
Merge pull request #887 from simple-robot/optimize-messages-structure
Browse files Browse the repository at this point in the history
优化、调整MessagesBuilder实现结构
  • Loading branch information
ForteScarlet authored Jul 23, 2024
2 parents cbdd4d7 + 628c1fe commit 3f506ef
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 56 deletions.
27 changes: 19 additions & 8 deletions simbot-api/api/simbot-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -1999,18 +1999,26 @@ public final class love/forte/simbot/message/Messages$Companion {
public final fun standardSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
}

public final class love/forte/simbot/message/MessagesBuilder {
public abstract interface class love/forte/simbot/message/MessagesAddable {
public fun add (Ljava/lang/String;)Llove/forte/simbot/message/MessagesAddable;
public abstract fun add (Llove/forte/simbot/message/Message$Element;)Llove/forte/simbot/message/MessagesAddable;
public abstract fun addAll (Ljava/lang/Iterable;)Llove/forte/simbot/message/MessagesAddable;
public abstract fun build ()Llove/forte/simbot/message/Messages;
public fun unaryPlus (Ljava/lang/Iterable;)Llove/forte/simbot/message/MessagesAddable;
public fun unaryPlus (Ljava/lang/String;)Llove/forte/simbot/message/MessagesAddable;
public fun unaryPlus (Llove/forte/simbot/message/Message$Element;)Llove/forte/simbot/message/MessagesAddable;
}

public final class love/forte/simbot/message/MessagesBuilder : love/forte/simbot/message/MessagesAddable {
public static final field Companion Llove/forte/simbot/message/MessagesBuilder$Companion;
public synthetic fun <init> (Ljava/util/List;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun add (Ljava/lang/String;)Llove/forte/simbot/message/MessagesBuilder;
public final fun add (Llove/forte/simbot/message/Message$Element;)Llove/forte/simbot/message/MessagesBuilder;
public final fun addAll (Ljava/lang/Iterable;)Llove/forte/simbot/message/MessagesBuilder;
public final fun build ()Llove/forte/simbot/message/Messages;
public synthetic fun add (Llove/forte/simbot/message/Message$Element;)Llove/forte/simbot/message/MessagesAddable;
public fun add (Llove/forte/simbot/message/Message$Element;)Llove/forte/simbot/message/MessagesBuilder;
public synthetic fun addAll (Ljava/lang/Iterable;)Llove/forte/simbot/message/MessagesAddable;
public fun addAll (Ljava/lang/Iterable;)Llove/forte/simbot/message/MessagesBuilder;
public fun build ()Llove/forte/simbot/message/Messages;
public static final fun create ()Llove/forte/simbot/message/MessagesBuilder;
public static final fun create (Ljava/util/List;)Llove/forte/simbot/message/MessagesBuilder;
public final fun unaryPlus (Ljava/lang/Iterable;)Llove/forte/simbot/message/MessagesBuilder;
public final fun unaryPlus (Ljava/lang/String;)Llove/forte/simbot/message/MessagesBuilder;
public final fun unaryPlus (Llove/forte/simbot/message/Message$Element;)Llove/forte/simbot/message/MessagesBuilder;
}

public final class love/forte/simbot/message/MessagesBuilder$Companion {
Expand All @@ -2025,6 +2033,9 @@ public abstract interface annotation class love/forte/simbot/message/MessagesBui
public final class love/forte/simbot/message/MessagesBuilders {
public static final fun buildMessages (Ljava/util/List;Lkotlin/jvm/functions/Function1;)Llove/forte/simbot/message/Messages;
public static synthetic fun buildMessages$default (Ljava/util/List;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Llove/forte/simbot/message/Messages;
public static final fun plusAssign (Llove/forte/simbot/message/MessagesAddable;Ljava/lang/Iterable;)V
public static final fun plusAssign (Llove/forte/simbot/message/MessagesAddable;Ljava/lang/String;)V
public static final fun plusAssign (Llove/forte/simbot/message/MessagesAddable;Llove/forte/simbot/message/Message$Element;)V
public static final fun plusAssign (Llove/forte/simbot/message/MessagesBuilder;Ljava/lang/Iterable;)V
public static final fun plusAssign (Llove/forte/simbot/message/MessagesBuilder;Ljava/lang/String;)V
public static final fun plusAssign (Llove/forte/simbot/message/MessagesBuilder;Llove/forte/simbot/message/Message$Element;)V
Expand Down
120 changes: 81 additions & 39 deletions simbot-api/src/commonMain/kotlin/love/forte/simbot/message/Messages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,85 @@ public object MessagesKt {
@Retention(AnnotationRetention.BINARY)
public annotation class MessagesBuilderDsl

/**
* 一个可以追加 [Message.Element] 的类型接口。
*
* @since 4.4.0
*
* @see MessagesBuilder
*/
public interface MessagesAddable<T : MessagesAddable<T>> {
/**
* Add an element to the [MessagesBuilder] container.
*
* @param element the element to be added
* @return the updated [MessagesAddable][T] instance
*/
public fun add(element: Message.Element): T

/**
* Adds the given text as [Text]
* to the [MessagesBuilder] container.
*
* @param text the text to add to the container
* @return the updated [MessagesAddable][T] instance
*/
public fun add(text: String): T = add(text.toText())

/**
* Add the given messages to this [MessagesAddable].
*
* @param messages 要添加的消息元素集
* @return the updated [MessagesAddable][T] object
*/
public fun addAll(messages: Iterable<Message.Element>): T

/**
* Add an element to this [MessagesAddable].
*
* @see add
*/
@MessagesBuilderDsl
public operator fun Message.Element.unaryPlus(): T = add(this)

/**
* Add a text as [Text] to this [MessagesAddable].
*
* @see add
*/
@MessagesBuilderDsl
public operator fun String.unaryPlus(): T = add(this)

/**
* Add messages to this [MessagesAddable].
*
* @see add
*/
@MessagesBuilderDsl
public operator fun Iterable<Message.Element>.unaryPlus(): T = addAll(this)

/**
* Build [Messages].
*
* This method constructs and returns a [Messages] object using the container.
*
* @return The constructed [Messages] object.
*/
public fun build(): Messages
}


/**
* 一个用于动态构建 [Messages] 的构建器。
* 使用 [create] 构建。
*
* Kotlin 中也可以使用 [buildMessages] 以 DSL 的方式构建 [Messages]。
*
* @see buildMessages
*/
public class MessagesBuilder private constructor(private val container: MutableList<Message.Element>) {
public class MessagesBuilder
private constructor(private val container: MutableList<Message.Element>) :
MessagesAddable<MessagesBuilder> {
public companion object {
/**
* Creates a new instance of MessagesBuilder.
Expand All @@ -497,23 +571,15 @@ public class MessagesBuilder private constructor(private val container: MutableL
* @param element the element to be added
* @return the updated MessagesBuilder instance
*/
public fun add(element: Message.Element): MessagesBuilder = apply { container.add(element) }

/**
* Adds the given text to the [MessagesBuilder] container.
*
* @param text the text to add to the container
* @return the updated MessagesBuilder object
*/
public fun add(text: String): MessagesBuilder = apply { container.add(text.toText()) }
override fun add(element: Message.Element): MessagesBuilder = apply { container.add(element) }

/**
* Add the given messages to the [MessagesBuilder] container.
*
* @param messages 要添加的消息元素集
* @return the updated MessagesBuilder object
*/
public fun addAll(messages: Iterable<Message.Element>): MessagesBuilder = apply {
override fun addAll(messages: Iterable<Message.Element>): MessagesBuilder = apply {
if (messages is Messages) {
when (messages) {
EmptyMessages -> {
Expand All @@ -534,35 +600,11 @@ public class MessagesBuilder private constructor(private val container: MutableL
}

/**
* Add an element to the [MessagesBuilder] container.
*
* @see add
*/
@MessagesBuilderDsl
public operator fun Message.Element.unaryPlus(): MessagesBuilder = add(this)

/**
* Add a text to the [MessagesBuilder] container.
*
* @see add
*/
@MessagesBuilderDsl
public operator fun String.unaryPlus(): MessagesBuilder = add(this)

/**
* Add messages to the [MessagesBuilder] container.
*
* @see add
*/
@MessagesBuilderDsl
public operator fun Iterable<Message.Element>.unaryPlus(): MessagesBuilder = addAll(this)

/**
* Build method.
* Build [Messages].
*
* This method constructs and returns a Messages object using the container.
* This method constructs and returns a [Messages] object using the container.
*
* @return The constructed Messages object.
* @return The constructed [Messages] object.
*/
public fun build(): Messages = container.toMessages()
override fun build(): Messages = container.toMessages()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Project https://github.com/simple-robot/simpler-robot
* Email ForteScarlet@163.com
*
* This file is part of the Simple Robot Library.
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -30,7 +30,6 @@ import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName



/**
* Builds a list of Messages using the provided container and block.
*
Expand All @@ -48,29 +47,81 @@ public inline fun buildMessages(
/**
* 使用 `+=` operator 添加 [element]。
*
* @see MessagesBuilder.add
* @since 4.4.0
* @see MessagesAddable.add
*/
@MessagesBuilderDsl
public operator fun MessagesBuilder.plusAssign(element: Message.Element) {
public operator fun MessagesAddable<*>.plusAssign(element: Message.Element) {
add(element)
}

/**
* 使用 `+=` operator 添加 [text]。
*
* @see MessagesBuilder.add
* @since 4.4.0
*
* @see MessagesAddable.add
*/
@MessagesBuilderDsl
public operator fun MessagesBuilder.plusAssign(text: String) {
public operator fun MessagesAddable<*>.plusAssign(text: String) {
add(text)
}

/**
* 使用 `+=` operator 添加 [messages]。
*
* @see MessagesBuilder.addAll
* @since 4.4.0
* @see MessagesAddable.addAll
*/
@MessagesBuilderDsl
public operator fun MessagesBuilder.plusAssign(messages: Iterable<Message.Element>) {
public operator fun MessagesAddable<*>.plusAssign(messages: Iterable<Message.Element>) {
addAll(messages)
}

/**
* 使用 `+=` operator 添加 [element]。
*
* @suppress deprecated
* @see MessagesBuilder.add
*/
@Deprecated(
"使用receiver类型为MessagesAddable的重载",
ReplaceWith("plusAssign(messages)")
)
@MessagesBuilderDsl
@JvmName("plusAssign") // binary compatible
public fun MessagesBuilder.plusAssign0(element: Message.Element) {
plusAssign(element)
}

/**
* 使用 `+=` operator 添加 [text]。
*
* @suppress deprecated
* @see MessagesBuilder.add
*/
@Deprecated(
"使用receiver类型为MessagesAddable的重载",
ReplaceWith("plusAssign(messages)")
)
@MessagesBuilderDsl
@JvmName("plusAssign") // binary compatible
public fun MessagesBuilder.plusAssign0(text: String) {
plusAssign(text)
}

/**
* 使用 `+=` operator 添加 [messages]。
*
* @suppress deprecated
* @see MessagesBuilder.addAll
*/
@Deprecated(
"使用receiver类型为MessagesAddable的重载",
ReplaceWith("plusAssign(messages)")
)
@MessagesBuilderDsl
@JvmName("plusAssign") // binary compatible
public fun MessagesBuilder.plusAssign0(messages: Iterable<Message.Element>) {
plusAssign(messages)
}
2 changes: 1 addition & 1 deletion website

0 comments on commit 3f506ef

Please sign in to comment.