Skip to content

Commit

Permalink
Change logic of AttributesBuilder. It no longer exposes the constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
altavir committed Mar 27, 2024
1 parent 3b74968 commit ac851be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<out Attribute<*>, Any?>) : Attributes {
internal class MapAttributes(override val content: Map<out Attribute<*>, 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()
Expand Down Expand Up @@ -75,23 +75,23 @@ public inline fun <reified A : FlagAttribute> Attributes.hasFlag(): Boolean =
public fun <T, A : Attribute<T>> Attributes.withAttribute(
attribute: A,
attrValue: T,
): Attributes = AttributesImpl(content + (attribute to attrValue))
): Attributes = MapAttributes(content + (attribute to attrValue))

public fun <A : Attribute<Unit>> Attributes.withAttribute(attribute: A): Attributes =
withAttribute(attribute, Unit)

/**
* Create a new [Attributes] by modifying the current one
*/
public fun <T> Attributes.modified(block: AttributesBuilder<T>.() -> Unit): Attributes = Attributes<T> {
public fun <O> Attributes.modified(block: AttributesBuilder<O>.() -> Unit): Attributes = Attributes<O> {
putAll(this@modified)
block()
}

/**
* 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]
Expand All @@ -101,7 +101,7 @@ public fun <T, A : SetAttribute<T>> Attributes.withAttributeElement(
attrValue: T,
): Attributes {
val currentSet: Set<T> = get(attribute) ?: emptySet()
return AttributesImpl(
return MapAttributes(
content + (attribute to (currentSet + attrValue))
)
}
Expand All @@ -114,7 +114,7 @@ public fun <T, A : SetAttribute<T>> Attributes.withoutAttributeElement(
attrValue: T,
): Attributes {
val currentSet: Set<T> = get(attribute) ?: emptySet()
return AttributesImpl(content + (attribute to (currentSet - attrValue)))
return MapAttributes(content + (attribute to (currentSet - attrValue)))
}

/**
Expand All @@ -123,13 +123,13 @@ public fun <T, A : SetAttribute<T>> Attributes.withoutAttributeElement(
public fun <T, A : Attribute<T>> 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 <A : Attribute<Unit>> 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)
public operator fun Attributes.plus(other: Attributes): Attributes = MapAttributes(content + other.content)
Original file line number Diff line number Diff line change
Expand Up @@ -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<out O> internal constructor(
private val map: MutableMap<Attribute<*>, Any?>,
) : Attributes {
public class AttributesBuilder<out O> internal constructor() : Attributes {

private val map = mutableMapOf<Attribute<*>, Any?>()

override fun toString(): String = "Attributes(value=${content.entries})"
override fun equals(other: Any?): Boolean = other is Attributes && Attributes.equals(this, other)
Expand Down Expand Up @@ -56,12 +57,12 @@ public class AttributesBuilder<out O> internal constructor(
map[this] = currentSet - attrValue
}

public fun build(): Attributes = AttributesImpl(map)
public fun build(): Attributes = MapAttributes(map)
}

/**
* Create [Attributes] with a given [builder]
* @param O the type for which attributes are built. The type is used only during compilation phase for static extension dispatch
*/
public fun <O> Attributes(builder: AttributesBuilder<O>.() -> Unit): Attributes =
AttributesBuilder<O>(mutableMapOf()).apply(builder).build()
AttributesBuilder<O>().apply(builder).build()

0 comments on commit ac851be

Please sign in to comment.