mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-06 17:10:48 +08:00
[Review] MessageChainBuilder: do not check built
This commit is contained in:
parent
938cc6dd07
commit
15a9d5c983
@ -55,13 +55,11 @@ public class MessageChainBuilder private constructor(
|
|||||||
public constructor(initialSize: Int) : this(ArrayList<SingleMessage>(initialSize))
|
public constructor(initialSize: Int) : this(ArrayList<SingleMessage>(initialSize))
|
||||||
|
|
||||||
public override fun add(element: SingleMessage): Boolean {
|
public override fun add(element: SingleMessage): Boolean {
|
||||||
checkBuilt()
|
|
||||||
flushCache()
|
flushCache()
|
||||||
return container.add(element)
|
return container.add(element)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun add(element: Message): Boolean {
|
public fun add(element: Message): Boolean {
|
||||||
checkBuilt()
|
|
||||||
flushCache()
|
flushCache()
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
return when (element) {
|
return when (element) {
|
||||||
@ -73,27 +71,23 @@ public class MessageChainBuilder private constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override fun addAll(elements: Collection<SingleMessage>): Boolean {
|
public override fun addAll(elements: Collection<SingleMessage>): Boolean {
|
||||||
checkBuilt()
|
|
||||||
flushCache()
|
flushCache()
|
||||||
return addAll(elements.asSequence())
|
return addAll(elements.asSequence())
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun addAll(elements: Iterable<SingleMessage>): Boolean {
|
public fun addAll(elements: Iterable<SingleMessage>): Boolean {
|
||||||
checkBuilt()
|
|
||||||
flushCache()
|
flushCache()
|
||||||
return addAll(elements.asSequence())
|
return addAll(elements.asSequence())
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmName("addAllFlatten") // erased generic type cause declaration clash
|
@JvmName("addAllFlatten") // erased generic type cause declaration clash
|
||||||
public fun addAll(elements: Iterable<Message>): Boolean {
|
public fun addAll(elements: Iterable<Message>): Boolean {
|
||||||
checkBuilt()
|
|
||||||
flushCache()
|
flushCache()
|
||||||
return addAll(elements.toMessageChain().asSequence())
|
return addAll(elements.toMessageChain().asSequence())
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
public operator fun Message.unaryPlus() {
|
public operator fun Message.unaryPlus() {
|
||||||
checkBuilt()
|
|
||||||
flushCache()
|
flushCache()
|
||||||
add(this)
|
add(this)
|
||||||
}
|
}
|
||||||
@ -101,38 +95,32 @@ public class MessageChainBuilder private constructor(
|
|||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
public operator fun String.unaryPlus() {
|
public operator fun String.unaryPlus() {
|
||||||
checkBuilt()
|
|
||||||
add(this)
|
add(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmSynthetic // they should use add
|
@JvmSynthetic // they should use add
|
||||||
public operator fun plusAssign(plain: String) {
|
public operator fun plusAssign(plain: String) {
|
||||||
checkBuilt()
|
|
||||||
withCache { append(plain) }
|
withCache { append(plain) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmSynthetic // they should use add
|
@JvmSynthetic // they should use add
|
||||||
public operator fun plusAssign(message: Message) {
|
public operator fun plusAssign(message: Message) {
|
||||||
checkBuilt()
|
|
||||||
flushCache()
|
flushCache()
|
||||||
this.add(message)
|
this.add(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmSynthetic // they should use add
|
@JvmSynthetic // they should use add
|
||||||
public operator fun plusAssign(message: SingleMessage) { // avoid resolution ambiguity
|
public operator fun plusAssign(message: SingleMessage) { // avoid resolution ambiguity
|
||||||
checkBuilt()
|
|
||||||
flushCache()
|
flushCache()
|
||||||
this.add(message)
|
this.add(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun add(plain: String) {
|
public fun add(plain: String) {
|
||||||
checkBuilt()
|
|
||||||
withCache { append(plain) }
|
withCache { append(plain) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmSynthetic // they should use add
|
@JvmSynthetic // they should use add
|
||||||
public operator fun plusAssign(charSequence: CharSequence) {
|
public operator fun plusAssign(charSequence: CharSequence) {
|
||||||
checkBuilt()
|
|
||||||
withCache { append(charSequence) }
|
withCache { append(charSequence) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,7 +134,6 @@ public class MessageChainBuilder private constructor(
|
|||||||
|
|
||||||
// avoid resolution to extensions
|
// avoid resolution to extensions
|
||||||
public fun asMessageChain(): MessageChain {
|
public fun asMessageChain(): MessageChain {
|
||||||
built = true
|
|
||||||
this.flushCache()
|
this.flushCache()
|
||||||
return MessageChainImpl(this.constrainSingleMessages())
|
return MessageChainImpl(this.constrainSingleMessages())
|
||||||
}
|
}
|
||||||
@ -161,31 +148,23 @@ public class MessageChainBuilder private constructor(
|
|||||||
return MessageChainBuilder(container.toMutableList())
|
return MessageChainBuilder(container.toMutableList())
|
||||||
}
|
}
|
||||||
|
|
||||||
///////
|
|
||||||
// FOR IMMUTABLE SAFETY
|
|
||||||
|
|
||||||
public override fun remove(element: SingleMessage): Boolean {
|
public override fun remove(element: SingleMessage): Boolean {
|
||||||
checkBuilt()
|
|
||||||
return container.remove(element)
|
return container.remove(element)
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun removeAll(elements: Collection<SingleMessage>): Boolean {
|
public override fun removeAll(elements: Collection<SingleMessage>): Boolean {
|
||||||
checkBuilt()
|
|
||||||
return container.removeAll(elements)
|
return container.removeAll(elements)
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun removeAt(index: Int): SingleMessage {
|
public override fun removeAt(index: Int): SingleMessage {
|
||||||
checkBuilt()
|
|
||||||
return container.removeAt(index)
|
return container.removeAt(index)
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun clear() {
|
public override fun clear() {
|
||||||
checkBuilt()
|
|
||||||
return container.clear()
|
return container.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun set(index: Int, element: SingleMessage): SingleMessage {
|
public override fun set(index: Int, element: SingleMessage): SingleMessage {
|
||||||
checkBuilt()
|
|
||||||
return container.set(index, element)
|
return container.set(index, element)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,7 +179,6 @@ public class MessageChainBuilder private constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private inline fun withCache(block: StringBuilder.() -> Unit): MessageChainBuilder {
|
private inline fun withCache(block: StringBuilder.() -> Unit): MessageChainBuilder {
|
||||||
checkBuilt()
|
|
||||||
if (cache == null) {
|
if (cache == null) {
|
||||||
cache = StringBuilder().apply(block)
|
cache = StringBuilder().apply(block)
|
||||||
} else {
|
} else {
|
||||||
@ -209,9 +187,6 @@ public class MessageChainBuilder private constructor(
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
private var built = false
|
|
||||||
private fun checkBuilt() = check(!built) { "MessageChainBuilder is already built therefore can't modify" }
|
|
||||||
|
|
||||||
private var firstConstrainSingleIndex = -1
|
private var firstConstrainSingleIndex = -1
|
||||||
|
|
||||||
private fun addAndCheckConstrainSingle(element: SingleMessage): Boolean {
|
private fun addAndCheckConstrainSingle(element: SingleMessage): Boolean {
|
||||||
|
Loading…
Reference in New Issue
Block a user