Add Attributes container equality

This commit is contained in:
Alexander Nozik 2024-03-08 10:04:37 +03:00
parent fcb7e2fa7d
commit 11722db3c8
2 changed files with 14 additions and 4 deletions

View File

@ -5,8 +5,6 @@
package space.kscience.attributes package space.kscience.attributes
import kotlin.jvm.JvmInline
/** /**
* A set of attributes. The implementation must guarantee that [content] keys correspond to its value types. * A set of attributes. The implementation must guarantee that [content] keys correspond to its value types.
*/ */
@ -27,14 +25,22 @@ public interface Attributes {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
public operator fun <T> get(attribute: Attribute<T>): T? = content[attribute] as? T public operator fun <T> get(attribute: Attribute<T>): T? = content[attribute] as? T
override fun toString(): String
override fun equals(other: Any?): Boolean
override fun hashCode(): Int
public companion object { public companion object {
public val EMPTY: Attributes = AttributesImpl(emptyMap()) public val EMPTY: Attributes = AttributesImpl(emptyMap())
public fun equals(a1: Attributes, a2: Attributes): Boolean =
a1.keys == a2.keys && a1.keys.all { a1[it] == a2[it] }
} }
} }
@JvmInline internal class AttributesImpl(override val content: Map<out Attribute<*>, Any?>) : Attributes {
internal value class AttributesImpl(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 hashCode(): Int = content.hashCode()
} }
public fun Attributes.isEmpty(): Boolean = content.isEmpty() public fun Attributes.isEmpty(): Boolean = content.isEmpty()

View File

@ -16,6 +16,10 @@ public class AttributesBuilder<out O> internal constructor(
public constructor() : this(mutableMapOf()) public constructor() : this(mutableMapOf())
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()
override val content: Map<out Attribute<*>, Any?> get() = map override val content: Map<out Attribute<*>, Any?> get() = map
public operator fun <T> set(attribute: Attribute<T>, value: T?) { public operator fun <T> set(attribute: Attribute<T>, value: T?) {