diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt index ad53049eb..8ddb0cbc7 100644 --- a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt +++ b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt @@ -30,14 +30,14 @@ public interface Attributes { override fun hashCode(): Int public companion object { - public val EMPTY: Attributes = AttributesImpl(emptyMap()) + public val EMPTY: Attributes = MapAttributes(emptyMap()) public fun equals(a1: Attributes, a2: Attributes): Boolean = a1.keys == a2.keys && a1.keys.all { a1[it] == a2[it] } } } -internal class AttributesImpl(override val content: Map, Any?>) : Attributes { +internal class MapAttributes(override val content: Map, Any?>) : Attributes { override fun toString(): String = "Attributes(value=${content.entries})" override fun equals(other: Any?): Boolean = other is Attributes && Attributes.equals(this, other) override fun hashCode(): Int = content.hashCode() @@ -75,7 +75,7 @@ public inline fun Attributes.hasFlag(): Boolean = public fun > Attributes.withAttribute( attribute: A, attrValue: T, -): Attributes = AttributesImpl(content + (attribute to attrValue)) +): Attributes = MapAttributes(content + (attribute to attrValue)) public fun > Attributes.withAttribute(attribute: A): Attributes = withAttribute(attribute, Unit) @@ -83,7 +83,7 @@ public fun > Attributes.withAttribute(attribute: A): Attribu /** * Create a new [Attributes] by modifying the current one */ -public fun Attributes.modified(block: AttributesBuilder.() -> Unit): Attributes = Attributes { +public fun Attributes.modified(block: AttributesBuilder.() -> Unit): Attributes = Attributes { putAll(this@modified) block() } @@ -91,7 +91,7 @@ public fun Attributes.modified(block: AttributesBuilder.() -> Unit): Attr /** * Create new [Attributes] by removing [attribute] key */ -public fun Attributes.withoutAttribute(attribute: Attribute<*>): Attributes = AttributesImpl(content.minus(attribute)) +public fun Attributes.withoutAttribute(attribute: Attribute<*>): Attributes = MapAttributes(content.minus(attribute)) /** * Add an element to a [SetAttribute] @@ -101,7 +101,7 @@ public fun > Attributes.withAttributeElement( attrValue: T, ): Attributes { val currentSet: Set = get(attribute) ?: emptySet() - return AttributesImpl( + return MapAttributes( content + (attribute to (currentSet + attrValue)) ) } @@ -114,7 +114,7 @@ public fun > Attributes.withoutAttributeElement( attrValue: T, ): Attributes { val currentSet: Set = get(attribute) ?: emptySet() - return AttributesImpl(content + (attribute to (currentSet - attrValue))) + return MapAttributes(content + (attribute to (currentSet - attrValue))) } /** @@ -123,13 +123,13 @@ public fun > Attributes.withoutAttributeElement( public fun > Attributes( attribute: A, attrValue: T, -): Attributes = AttributesImpl(mapOf(attribute to attrValue)) +): Attributes = MapAttributes(mapOf(attribute to attrValue)) /** * Create Attributes with a single [Unit] valued attribute */ public fun > Attributes( attribute: A, -): Attributes = AttributesImpl(mapOf(attribute to Unit)) +): Attributes = MapAttributes(mapOf(attribute to Unit)) -public operator fun Attributes.plus(other: Attributes): Attributes = AttributesImpl(content + other.content) \ No newline at end of file +public operator fun Attributes.plus(other: Attributes): Attributes = MapAttributes(content + other.content) \ No newline at end of file diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt index 0a185f74c..6f42d1dcf 100644 --- a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt +++ b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt @@ -6,13 +6,14 @@ package space.kscience.attributes /** - * A safe builder for [Attributes] + * A builder for [Attributes]. + * The builder is not thread safe * * @param O type marker of an owner object, for which these attributes are made */ -public class AttributesBuilder internal constructor( - private val map: MutableMap, Any?>, -) : Attributes { +public class AttributesBuilder internal constructor() : Attributes { + + private val map = mutableMapOf, Any?>() override fun toString(): String = "Attributes(value=${content.entries})" override fun equals(other: Any?): Boolean = other is Attributes && Attributes.equals(this, other) @@ -56,7 +57,7 @@ public class AttributesBuilder internal constructor( map[this] = currentSet - attrValue } - public fun build(): Attributes = AttributesImpl(map) + public fun build(): Attributes = MapAttributes(map) } /** @@ -64,4 +65,4 @@ public class AttributesBuilder internal constructor( * @param O the type for which attributes are built. The type is used only during compilation phase for static extension dispatch */ public fun Attributes(builder: AttributesBuilder.() -> Unit): Attributes = - AttributesBuilder(mutableMapOf()).apply(builder).build() \ No newline at end of file + AttributesBuilder().apply(builder).build() \ No newline at end of file