From f79b7faeafeafab51a5c6432be0255d5c3e14251 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 5 Jun 2024 12:15:27 +0300 Subject: [PATCH] Simplify inheritance logic in `MutableTypedMeta` --- CHANGELOG.md | 1 + .../space/kscience/dataforge/meta/MutableMeta.kt | 14 ++++++++++++-- .../kscience/dataforge/meta/ObservableMeta.kt | 16 ---------------- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 788a7374..43e6bf4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added ### Changed +- Simplify inheritance logic in `MutableTypedMeta` ### Deprecated - MetaProvider `spec` is replaced by `readable`. `listOfSpec` replaced with `listOfReadable` diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMeta.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMeta.kt index 7be16cc9..274671bf 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMeta.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MutableMeta.kt @@ -159,7 +159,17 @@ public interface MutableTypedMeta> : TypedMeta, Mutab */ @DFExperimental public fun attach(name: Name, node: M) - override fun get(name: Name): M? + + override fun get(name: Name): M? { + tailrec fun M.find(name: Name): M? = if (name.isEmpty()) { + self + } else { + items[name.firstOrNull()!!]?.find(name.cutFirst()) + } + + return self.find(name) + } + override fun getOrCreate(name: Name): M } @@ -388,7 +398,7 @@ public fun MutableMeta.reset(newMeta: Meta) { (items.keys - newMeta.items.keys).forEach { remove(it.asName()) } - newMeta.items.forEach { (token, item)-> + newMeta.items.forEach { (token, item) -> set(token, item) } } diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/ObservableMeta.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/ObservableMeta.kt index 7cd28746..20388a92 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/ObservableMeta.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/ObservableMeta.kt @@ -2,9 +2,6 @@ package space.kscience.dataforge.meta import space.kscience.dataforge.misc.ThreadSafe import space.kscience.dataforge.names.Name -import space.kscience.dataforge.names.cutFirst -import space.kscience.dataforge.names.firstOrNull -import space.kscience.dataforge.names.isEmpty internal data class MetaListener( @@ -39,20 +36,7 @@ public interface ObservableMeta : Meta { * A [Meta] which is both observable and mutable */ public interface ObservableMutableMeta : ObservableMeta, MutableMeta, MutableTypedMeta { - override val self: ObservableMutableMeta get() = this - - override fun getOrCreate(name: Name): ObservableMutableMeta - - override fun get(name: Name): ObservableMutableMeta? { - tailrec fun ObservableMutableMeta.find(name: Name): ObservableMutableMeta? = if (name.isEmpty()) { - this - } else { - items[name.firstOrNull()!!]?.find(name.cutFirst()) - } - - return find(name) - } } internal abstract class AbstractObservableMeta : ObservableMeta {