kmath/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt

135 lines
4.2 KiB
Kotlin
Raw Normal View History

/*
* 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
2023-11-18 22:29:59 +03:00
/**
* A set of attributes. The implementation must guarantee that [content] keys correspond to their value types.
2023-11-18 22:29:59 +03:00
*/
public interface Attributes {
2024-02-18 15:05:56 +03:00
/**
* Raw content for this [Attributes]
*/
2023-11-18 22:29:59 +03:00
public val content: Map<out Attribute<*>, Any?>
2024-02-18 15:05:56 +03:00
/**
* Attribute keys contained in this [Attributes]
*/
public val keys: Set<Attribute<*>> get() = content.keys
2024-02-18 15:05:56 +03:00
/**
* Provide an attribute value. Return null if attribute is not present or if its value is null.
*/
@Suppress("UNCHECKED_CAST")
public operator fun <T> get(attribute: Attribute<T>): T? = content[attribute] as? T
2024-03-08 10:04:37 +03:00
override fun toString(): String
override fun equals(other: Any?): Boolean
override fun hashCode(): Int
public companion object {
2023-11-18 22:29:59 +03:00
public val EMPTY: Attributes = AttributesImpl(emptyMap())
2024-03-08 10:04:37 +03:00
public fun equals(a1: Attributes, a2: Attributes): Boolean =
a1.keys == a2.keys && a1.keys.all { a1[it] == a2[it] }
}
}
2024-03-08 10:04:37 +03:00
internal class AttributesImpl(override val content: Map<out Attribute<*>, Any?>) : Attributes {
2023-11-18 22:29:59 +03:00
override fun toString(): String = "Attributes(value=${content.entries})"
2024-03-08 10:04:37 +03:00
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
}
public fun Attributes.isEmpty(): Boolean = content.isEmpty()
/**
* Get attribute value or default
*/
public fun <T> Attributes.getOrDefault(attribute: AttributeWithDefault<T>): T = get(attribute) ?: attribute.default
/**
* Check if there is an attribute that matches given key by type and adheres to [predicate].
*/
@Suppress("UNCHECKED_CAST")
2023-11-18 22:29:59 +03:00
public inline fun <T, reified A : Attribute<T>> Attributes.hasAny(predicate: (value: T) -> Boolean): Boolean =
content.any { (mapKey, mapValue) -> mapKey is A && predicate(mapValue as T) }
/**
* Check if there is an attribute of given type (subtypes included)
*/
2023-11-18 22:29:59 +03:00
public inline fun <reified A : Attribute<*>> Attributes.hasAny(): Boolean =
content.any { (mapKey, _) -> mapKey is A }
/**
* Check if [Attributes] contains a flag. Multiple keys that are instances of a flag could be present
*/
2023-11-18 22:29:59 +03:00
public inline fun <reified A : FlagAttribute> Attributes.hasFlag(): Boolean =
content.keys.any { it is A }
/**
* Create [Attributes] with an added or replaced attribute key.
*/
2023-11-11 10:19:09 +03:00
public fun <T, A : Attribute<T>> Attributes.withAttribute(
attribute: A,
attrValue: T,
2023-11-18 22:29:59 +03:00
): Attributes = AttributesImpl(content + (attribute to attrValue))
public fun <A : Attribute<Unit>> Attributes.withAttribute(attribute: A): Attributes =
withAttribute(attribute, Unit)
2023-08-13 19:13:39 +03:00
/**
* Create a new [Attributes] by modifying the current one
*/
2023-11-18 22:29:59 +03:00
public fun <T> Attributes.modify(block: AttributesBuilder<T>.() -> Unit): Attributes = Attributes<T> {
2023-08-13 19:13:39 +03:00
from(this@modify)
block()
}
/**
* Create new [Attributes] by removing [attribute] key
*/
2023-11-18 22:29:59 +03:00
public fun Attributes.withoutAttribute(attribute: Attribute<*>): Attributes = AttributesImpl(content.minus(attribute))
/**
* Add an element to a [SetAttribute]
*/
public fun <T, A : SetAttribute<T>> Attributes.withAttributeElement(
attribute: A,
attrValue: T,
): Attributes {
val currentSet: Set<T> = get(attribute) ?: emptySet()
2023-11-18 22:29:59 +03:00
return AttributesImpl(
content + (attribute to (currentSet + attrValue))
)
}
/**
* Remove an element from [SetAttribute]
*/
public fun <T, A : SetAttribute<T>> Attributes.withoutAttributeElement(
attribute: A,
attrValue: T,
): Attributes {
val currentSet: Set<T> = get(attribute) ?: emptySet()
2023-11-18 22:29:59 +03:00
return AttributesImpl(content + (attribute to (currentSet - attrValue)))
}
/**
* Create [Attributes] with a single key
*/
2023-11-11 10:19:09 +03:00
public fun <T, A : Attribute<T>> Attributes(
attribute: A,
attrValue: T,
2023-11-18 22:29:59 +03:00
): Attributes = AttributesImpl(mapOf(attribute to attrValue))
2023-11-04 11:49:31 +03:00
/**
* Create Attributes with a single [Unit] valued attribute
*/
public fun <A : Attribute<Unit>> Attributes(
2023-11-18 22:29:59 +03:00
attribute: A,
): Attributes = AttributesImpl(mapOf(attribute to Unit))
2023-11-04 11:49:31 +03:00
2023-11-18 22:29:59 +03:00
public operator fun Attributes.plus(other: Attributes): Attributes = AttributesImpl(content + other.content)