forked from kscience/kmath
Change logic of AttributesBuilder. It no longer exposes the constructor
This commit is contained in:
parent
3b74968f9a
commit
ac851bea62
@ -30,14 +30,14 @@ public interface Attributes {
|
|||||||
override fun hashCode(): Int
|
override fun hashCode(): Int
|
||||||
|
|
||||||
public companion object {
|
public companion object {
|
||||||
public val EMPTY: Attributes = AttributesImpl(emptyMap())
|
public val EMPTY: Attributes = MapAttributes(emptyMap())
|
||||||
|
|
||||||
public fun equals(a1: Attributes, a2: Attributes): Boolean =
|
public fun equals(a1: Attributes, a2: Attributes): Boolean =
|
||||||
a1.keys == a2.keys && a1.keys.all { a1[it] == a2[it] }
|
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 toString(): String = "Attributes(value=${content.entries})"
|
||||||
override fun equals(other: Any?): Boolean = other is Attributes && Attributes.equals(this, other)
|
override fun equals(other: Any?): Boolean = other is Attributes && Attributes.equals(this, other)
|
||||||
override fun hashCode(): Int = content.hashCode()
|
override fun hashCode(): Int = content.hashCode()
|
||||||
@ -75,7 +75,7 @@ public inline fun <reified A : FlagAttribute> Attributes.hasFlag(): Boolean =
|
|||||||
public fun <T, A : Attribute<T>> Attributes.withAttribute(
|
public fun <T, A : Attribute<T>> Attributes.withAttribute(
|
||||||
attribute: A,
|
attribute: A,
|
||||||
attrValue: T,
|
attrValue: T,
|
||||||
): Attributes = AttributesImpl(content + (attribute to attrValue))
|
): Attributes = MapAttributes(content + (attribute to attrValue))
|
||||||
|
|
||||||
public fun <A : Attribute<Unit>> Attributes.withAttribute(attribute: A): Attributes =
|
public fun <A : Attribute<Unit>> Attributes.withAttribute(attribute: A): Attributes =
|
||||||
withAttribute(attribute, Unit)
|
withAttribute(attribute, Unit)
|
||||||
@ -83,7 +83,7 @@ public fun <A : Attribute<Unit>> Attributes.withAttribute(attribute: A): Attribu
|
|||||||
/**
|
/**
|
||||||
* Create a new [Attributes] by modifying the current one
|
* 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)
|
putAll(this@modified)
|
||||||
block()
|
block()
|
||||||
}
|
}
|
||||||
@ -91,7 +91,7 @@ public fun <T> Attributes.modified(block: AttributesBuilder<T>.() -> Unit): Attr
|
|||||||
/**
|
/**
|
||||||
* Create new [Attributes] by removing [attribute] key
|
* 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]
|
* Add an element to a [SetAttribute]
|
||||||
@ -101,7 +101,7 @@ public fun <T, A : SetAttribute<T>> Attributes.withAttributeElement(
|
|||||||
attrValue: T,
|
attrValue: T,
|
||||||
): Attributes {
|
): Attributes {
|
||||||
val currentSet: Set<T> = get(attribute) ?: emptySet()
|
val currentSet: Set<T> = get(attribute) ?: emptySet()
|
||||||
return AttributesImpl(
|
return MapAttributes(
|
||||||
content + (attribute to (currentSet + attrValue))
|
content + (attribute to (currentSet + attrValue))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -114,7 +114,7 @@ public fun <T, A : SetAttribute<T>> Attributes.withoutAttributeElement(
|
|||||||
attrValue: T,
|
attrValue: T,
|
||||||
): Attributes {
|
): Attributes {
|
||||||
val currentSet: Set<T> = get(attribute) ?: emptySet()
|
val currentSet: Set<T> = get(attribute) ?: emptySet()
|
||||||
return AttributesImpl(content + (attribute to (currentSet - attrValue)))
|
return MapAttributes(content + (attribute to (currentSet - attrValue)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -123,13 +123,13 @@ public fun <T, A : SetAttribute<T>> Attributes.withoutAttributeElement(
|
|||||||
public fun <T, A : Attribute<T>> Attributes(
|
public fun <T, A : Attribute<T>> Attributes(
|
||||||
attribute: A,
|
attribute: A,
|
||||||
attrValue: T,
|
attrValue: T,
|
||||||
): Attributes = AttributesImpl(mapOf(attribute to attrValue))
|
): Attributes = MapAttributes(mapOf(attribute to attrValue))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create Attributes with a single [Unit] valued attribute
|
* Create Attributes with a single [Unit] valued attribute
|
||||||
*/
|
*/
|
||||||
public fun <A : Attribute<Unit>> Attributes(
|
public fun <A : Attribute<Unit>> Attributes(
|
||||||
attribute: A,
|
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)
|
@ -6,13 +6,14 @@
|
|||||||
package space.kscience.attributes
|
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
|
* @param O type marker of an owner object, for which these attributes are made
|
||||||
*/
|
*/
|
||||||
public class AttributesBuilder<out O> internal constructor(
|
public class AttributesBuilder<out O> internal constructor() : Attributes {
|
||||||
private val map: MutableMap<Attribute<*>, Any?>,
|
|
||||||
) : Attributes {
|
private val map = mutableMapOf<Attribute<*>, Any?>()
|
||||||
|
|
||||||
override fun toString(): String = "Attributes(value=${content.entries})"
|
override fun toString(): String = "Attributes(value=${content.entries})"
|
||||||
override fun equals(other: Any?): Boolean = other is Attributes && Attributes.equals(this, other)
|
override fun equals(other: Any?): Boolean = other is Attributes && Attributes.equals(this, other)
|
||||||
@ -56,7 +57,7 @@ public class AttributesBuilder<out O> internal constructor(
|
|||||||
map[this] = currentSet - attrValue
|
map[this] = currentSet - attrValue
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun build(): Attributes = AttributesImpl(map)
|
public fun build(): Attributes = MapAttributes(map)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -64,4 +65,4 @@ public class AttributesBuilder<out O> internal constructor(
|
|||||||
* @param O the type for which attributes are built. The type is used only during compilation phase for static extension dispatch
|
* @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 =
|
public fun <O> Attributes(builder: AttributesBuilder<O>.() -> Unit): Attributes =
|
||||||
AttributesBuilder<O>(mutableMapOf()).apply(builder).build()
|
AttributesBuilder<O>().apply(builder).build()
|
Loading…
Reference in New Issue
Block a user