/* * 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] * * @param O type marker of an owner object, for which these attributes are made */ public class TypedAttributesBuilder internal constructor(private val map: MutableMap, Any?>) { public constructor() : this(mutableMapOf()) @Suppress("UNCHECKED_CAST") public operator fun get(attribute: Attribute): T? = map[attribute] as? T public operator fun Attribute.invoke(value: V?) { if (value == null) { map.remove(this) } else { map[this] = value } } public fun from(attributes: Attributes) { map.putAll(attributes.content) } public fun SetAttribute.add( attrValue: V, ) { val currentSet: Set = get(this) ?: emptySet() map[this] = currentSet + attrValue } /** * Remove an element from [SetAttribute] */ public fun SetAttribute.remove( attrValue: V, ) { val currentSet: Set = get(this) ?: emptySet() map[this] = currentSet - attrValue } public fun build(): Attributes = Attributes(map) } public typealias AttributesBuilder = TypedAttributesBuilder public fun AttributesBuilder( attributes: Attributes, ): AttributesBuilder = AttributesBuilder(attributes.content.toMutableMap()) public inline fun Attributes(builder: AttributesBuilder.() -> Unit): Attributes = AttributesBuilder().apply(builder).build()