From 11722db3c827227b666e25636db012f5b65df2f1 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 8 Mar 2024 10:04:37 +0300 Subject: [PATCH] Add Attributes container equality --- .../kotlin/space/kscience/attributes/Attributes.kt | 14 ++++++++++---- .../space/kscience/attributes/AttributesBuilder.kt | 4 ++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt index 86d196f74..ff5d7f145 100644 --- a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt +++ b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/Attributes.kt @@ -5,8 +5,6 @@ package space.kscience.attributes -import kotlin.jvm.JvmInline - /** * 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") public operator fun get(attribute: Attribute): T? = content[attribute] as? T + override fun toString(): String + override fun equals(other: Any?): Boolean + override fun hashCode(): Int + public companion object { 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 value class AttributesImpl(override val content: Map, Any?>) : Attributes { +internal class AttributesImpl(override val content: Map, Any?>) : Attributes { 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() diff --git a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt index 0acf4e004..0082ba143 100644 --- a/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt +++ b/attributes-kt/src/commonMain/kotlin/space/kscience/attributes/AttributesBuilder.kt @@ -16,6 +16,10 @@ public class AttributesBuilder internal constructor( 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, Any?> get() = map public operator fun set(attribute: Attribute, value: T?) {