Add ValueProvider.kt

This commit is contained in:
Alexander Nozik 2021-08-10 21:30:40 +03:00
parent c1065c2885
commit 66c708d9fb
5 changed files with 40 additions and 10 deletions

View File

@ -4,7 +4,7 @@ plugins {
allprojects { allprojects {
group = "space.kscience" group = "space.kscience"
version = "0.5.0-dev-10" version = "0.5.0-dev-11"
} }
subprojects { subprojects {

View File

@ -19,8 +19,10 @@ public interface MetaRepr {
/** /**
* A container for meta nodes * A container for meta nodes
*/ */
public fun interface MetaProvider { public fun interface MetaProvider : ValueProvider {
public fun getMeta(name: Name): Meta? public fun getMeta(name: Name): Meta?
override fun getValue(name: Name): Value? = getMeta(name)?.value
} }
/** /**

View File

@ -4,6 +4,7 @@ import kotlinx.serialization.Serializable
import space.kscience.dataforge.misc.DFExperimental import space.kscience.dataforge.misc.DFExperimental
import space.kscience.dataforge.names.* import space.kscience.dataforge.names.*
import space.kscience.dataforge.values.EnumValue import space.kscience.dataforge.values.EnumValue
import space.kscience.dataforge.values.MutableValueProvider
import space.kscience.dataforge.values.Value import space.kscience.dataforge.values.Value
import space.kscience.dataforge.values.asValue import space.kscience.dataforge.values.asValue
import kotlin.js.JsName import kotlin.js.JsName
@ -19,10 +20,12 @@ public annotation class MetaBuilder
/** /**
* A generic interface that gives access to getting and setting meta notes and values * A generic interface that gives access to getting and setting meta notes and values
*/ */
public interface MutableMetaProvider : MetaProvider { public interface MutableMetaProvider : MetaProvider, MutableValueProvider {
override fun getMeta(name: Name): MutableMeta? override fun getMeta(name: Name): MutableMeta?
public fun setMeta(name: Name, node: Meta?) public fun setMeta(name: Name, node: Meta?)
public fun setValue(name: Name, value: Value?) override fun setValue(name: Name, value: Value?) {
getMeta(name)?.value = value
}
} }
/** /**

View File

@ -12,8 +12,14 @@ import kotlin.jvm.Synchronized
*/ */
public open class Scheme : Described, MetaRepr, MutableMetaProvider, Configurable { public open class Scheme : Described, MetaRepr, MutableMetaProvider, Configurable {
/**
* Meta to be mutated by this schme
*/
private var targetMeta: MutableMeta = MutableMeta() private var targetMeta: MutableMeta = MutableMeta()
/**
* Default values provided by this scheme
*/
private var defaultMeta: Meta? = null private var defaultMeta: Meta? = null
final override val meta: ObservableMutableMeta = SchemeMeta(Name.EMPTY) final override val meta: ObservableMutableMeta = SchemeMeta(Name.EMPTY)
@ -25,7 +31,7 @@ public open class Scheme : Described, MetaRepr, MutableMetaProvider, Configurabl
newMeta: MutableMeta, newMeta: MutableMeta,
preserveDefault: Boolean = false preserveDefault: Boolean = false
) { ) {
if(preserveDefault){ if (preserveDefault) {
defaultMeta = targetMeta.seal() defaultMeta = targetMeta.seal()
} }
targetMeta = newMeta targetMeta = newMeta
@ -62,12 +68,14 @@ public open class Scheme : Described, MetaRepr, MutableMetaProvider, Configurabl
private inner class SchemeMeta(val pathName: Name) : ObservableMutableMeta { private inner class SchemeMeta(val pathName: Name) : ObservableMutableMeta {
override var value: Value? override var value: Value?
get() = targetMeta[pathName]?.value ?: defaultMeta?.get(pathName)?.value get() = targetMeta[pathName]?.value
?: defaultMeta?.get(pathName)?.value
?: descriptor?.get(pathName)?.defaultValue
set(value) { set(value) {
val oldValue = targetMeta[pathName]?.value val oldValue = targetMeta[pathName]?.value
targetMeta[pathName] = value targetMeta[pathName] = value
if (oldValue != value) { if (oldValue != value) {
invalidate(pathName) invalidate(Name.EMPTY)
} }
} }
@ -84,9 +92,9 @@ public open class Scheme : Described, MetaRepr, MutableMetaProvider, Configurabl
@Synchronized @Synchronized
override fun onChange(owner: Any?, callback: Meta.(name: Name) -> Unit) { override fun onChange(owner: Any?, callback: Meta.(name: Name) -> Unit) {
listeners.add(MetaListener(owner) { changedeName -> listeners.add(MetaListener(owner) { changedName ->
if (changedeName.startsWith(pathName)) { if (changedName.startsWith(pathName)) {
this@Scheme.meta.callback(changedeName.removeHeadOrNull(pathName)!!) this@Scheme.meta.callback(changedName.removeHeadOrNull(pathName)!!)
} }
}) })
} }

View File

@ -0,0 +1,17 @@
package space.kscience.dataforge.values
import space.kscience.dataforge.names.Name
/**
* An object that could provide values
*/
public fun interface ValueProvider {
public fun getValue(name: Name): Value?
}
/**
* An object that could consume values
*/
public interface MutableValueProvider: ValueProvider{
public fun setValue(name: Name, value: Value?)
}