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 {
group = "space.kscience"
version = "0.5.0-dev-10"
version = "0.5.0-dev-11"
}
subprojects {

View File

@ -19,8 +19,10 @@ public interface MetaRepr {
/**
* A container for meta nodes
*/
public fun interface MetaProvider {
public fun interface MetaProvider : ValueProvider {
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.names.*
import space.kscience.dataforge.values.EnumValue
import space.kscience.dataforge.values.MutableValueProvider
import space.kscience.dataforge.values.Value
import space.kscience.dataforge.values.asValue
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
*/
public interface MutableMetaProvider : MetaProvider {
public interface MutableMetaProvider : MetaProvider, MutableValueProvider {
override fun getMeta(name: Name): MutableMeta?
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 {
/**
* Meta to be mutated by this schme
*/
private var targetMeta: MutableMeta = MutableMeta()
/**
* Default values provided by this scheme
*/
private var defaultMeta: Meta? = null
final override val meta: ObservableMutableMeta = SchemeMeta(Name.EMPTY)
@ -25,7 +31,7 @@ public open class Scheme : Described, MetaRepr, MutableMetaProvider, Configurabl
newMeta: MutableMeta,
preserveDefault: Boolean = false
) {
if(preserveDefault){
if (preserveDefault) {
defaultMeta = targetMeta.seal()
}
targetMeta = newMeta
@ -62,12 +68,14 @@ public open class Scheme : Described, MetaRepr, MutableMetaProvider, Configurabl
private inner class SchemeMeta(val pathName: Name) : ObservableMutableMeta {
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) {
val oldValue = targetMeta[pathName]?.value
targetMeta[pathName] = value
if (oldValue != value) {
invalidate(pathName)
invalidate(Name.EMPTY)
}
}
@ -84,9 +92,9 @@ public open class Scheme : Described, MetaRepr, MutableMetaProvider, Configurabl
@Synchronized
override fun onChange(owner: Any?, callback: Meta.(name: Name) -> Unit) {
listeners.add(MetaListener(owner) { changedeName ->
if (changedeName.startsWith(pathName)) {
this@Scheme.meta.callback(changedeName.removeHeadOrNull(pathName)!!)
listeners.add(MetaListener(owner) { changedName ->
if (changedName.startsWith(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?)
}