2023-06-20 19:45:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2018-2023 KMath contributors.
|
|
|
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package space.kscience.attributes
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A safe builder for [Attributes]
|
2023-09-13 09:00:56 +03:00
|
|
|
*
|
|
|
|
* @param O type marker of an owner object, for which these attributes are made
|
2023-06-20 19:45:21 +03:00
|
|
|
*/
|
2023-11-18 22:29:59 +03:00
|
|
|
public class AttributesBuilder<out O> internal constructor(
|
|
|
|
private val map: MutableMap<Attribute<*>, Any?>,
|
|
|
|
) : Attributes {
|
2023-08-13 19:13:39 +03:00
|
|
|
|
2024-03-08 10:04:37 +03:00
|
|
|
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()
|
|
|
|
|
2023-11-18 22:29:59 +03:00
|
|
|
override val content: Map<out Attribute<*>, Any?> get() = map
|
2023-06-20 19:45:21 +03:00
|
|
|
|
2023-11-18 22:29:59 +03:00
|
|
|
public operator fun <T> set(attribute: Attribute<T>, value: T?) {
|
2023-06-20 19:45:21 +03:00
|
|
|
if (value == null) {
|
2023-11-18 22:29:59 +03:00
|
|
|
map.remove(attribute)
|
2023-06-20 19:45:21 +03:00
|
|
|
} else {
|
2023-11-18 22:29:59 +03:00
|
|
|
map[attribute] = value
|
2023-06-20 19:45:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:29:59 +03:00
|
|
|
public operator fun <V> Attribute<V>.invoke(value: V?) {
|
|
|
|
set(this, value)
|
|
|
|
}
|
|
|
|
|
2024-03-27 08:12:39 +03:00
|
|
|
public infix fun <V> Attribute<V>.put(value: V?) {
|
|
|
|
set(this, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Put all attributes for given [attributes]
|
|
|
|
*/
|
|
|
|
public fun putAll(attributes: Attributes) {
|
2023-06-20 19:45:21 +03:00
|
|
|
map.putAll(attributes.content)
|
|
|
|
}
|
|
|
|
|
2024-03-27 08:12:39 +03:00
|
|
|
public infix fun <V> SetAttribute<V>.add(attrValue: V) {
|
2023-06-20 19:45:21 +03:00
|
|
|
val currentSet: Set<V> = get(this) ?: emptySet()
|
|
|
|
map[this] = currentSet + attrValue
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an element from [SetAttribute]
|
|
|
|
*/
|
2024-03-27 08:12:39 +03:00
|
|
|
public infix fun <V> SetAttribute<V>.remove(attrValue: V) {
|
2023-06-20 19:45:21 +03:00
|
|
|
val currentSet: Set<V> = get(this) ?: emptySet()
|
|
|
|
map[this] = currentSet - attrValue
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:29:59 +03:00
|
|
|
public fun build(): Attributes = AttributesImpl(map)
|
2023-06-20 19:45:21 +03:00
|
|
|
}
|
|
|
|
|
2024-03-27 09:11:35 +03:00
|
|
|
/**
|
|
|
|
* 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()
|