Refactor ItemProvider hierarchy

This commit is contained in:
Alexander Nozik 2021-07-08 12:16:00 +03:00
parent 73b3bbe7fc
commit d2ea1a975e
2 changed files with 10 additions and 9 deletions

View File

@ -1,6 +1,6 @@
package space.kscience.dataforge.properties
import space.kscience.dataforge.meta.Config
import space.kscience.dataforge.meta.ObservableMeta
import space.kscience.dataforge.meta.get
import space.kscience.dataforge.meta.set
import space.kscience.dataforge.meta.transformations.MetaConverter
@ -11,25 +11,25 @@ import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.startsWith
@DFExperimental
public class ConfigProperty<T : Any>(
public val config: Config,
public class MetaProperty<T : Any>(
public val meta: ObservableMeta,
public val name: Name,
public val converter: MetaConverter<T>,
) : Property<T?> {
override var value: T?
get() = converter.nullableItemToObject(config[name])
get() = converter.nullableItemToObject(meta[name])
set(value) {
config[name] = converter.nullableObjectToMetaItem(value)
meta[name] = converter.nullableObjectToMetaItem(value)
}
override fun onChange(owner: Any?, callback: (T?) -> Unit) {
config.onChange(owner) { name, oldItem, newItem ->
meta.onChange(owner) { name, oldItem, newItem ->
if (name.startsWith(this.name) && oldItem != newItem) callback(converter.nullableItemToObject(newItem))
}
}
override fun removeChangeListener(owner: Any?) {
config.removeListener(owner)
meta.removeListener(owner)
}
}

View File

@ -1,13 +1,14 @@
package space.kscience.dataforge.properties
import space.kscience.dataforge.meta.ItemPropertyProvider
import space.kscience.dataforge.meta.ObservableItemProvider
import space.kscience.dataforge.misc.DFExperimental
import space.kscience.dataforge.names.startsWith
import space.kscience.dataforge.names.toName
import kotlin.reflect.KMutableProperty1
@DFExperimental
public fun <P : ItemPropertyProvider, T : Any> P.property(property: KMutableProperty1<P, T?>): Property<T?> =
public fun <P : ObservableItemProvider, T : Any> P.property(property: KMutableProperty1<P, T?>): Property<T?> =
object : Property<T?> {
override var value: T?
get() = property.get(this@property)