如何结合Kotlin委托属性:可观察,否决和“按地图"? [英] How to combine kotlin delegated property: observable, vetoable, and "by map"?

查看:138
本文介绍了如何结合Kotlin委托属性:可观察,否决和“按地图"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并 delegates/observable vetoable (其中在查看源代码kotlin.properties.Delegates.kt之后,这不是问题,但是在尝试同时在地图中存储属性.

I'm trying to combine delegates/observable with vetoable (which isn't a problem after looking at the source kotlin.properties.Delegates.kt), but things got hairy when trying to also store the properties in a map.

或者换句话说,如何结合这三个:

Or in other words, how to combine these three:

var k1: Int by Delegates.observable(0) { property, oldValue, newValue ->
    println("Hi from k1 observer")
}

var k2:Int by Delegates.vetoable(0) {property, oldValue, newValue ->
    println("Hi from k2 more-than check")
     oldValue > newValue
}

val myMap = mutableMapOf<String, Int>()
var k3 by myMap

推荐答案

没有简单的方法可以编写委托,但是您可以编写自己的委托,例如:

There is no easy way to compose delegates but you can write your own delegate, for example:

inline fun <T> mapVetoObserver(
        map: MutableMap<String, T>,
        absentValue: T,
        crossinline veto: ((property: KProperty<*>, oldValue: T, newValue: T) -> Boolean),
        crossinline observe: ((property: KProperty<*>, oldValue: T, newValue: T) -> Unit))
        : ReadWriteProperty<Any?, T> {

    return object : ReadWriteProperty<Any?, T> {

        // there is no good way to set the map value to some initial value upon delegate
        // construction since the property name is unknown until getValue/setValue are called
        // => absent value rather than initial value

        override fun getValue(thisRef: Any?, property: KProperty<*>): T {
            return map[property.name] ?: absentValue
        }

        override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
            val oldValue = getValue(thisRef, property)
            if (!veto(property, oldValue, value)) {
                map[property.name] = value
                observe(property, oldValue, value)
            }
        }
    }
}

然后像

val myMap = mutableMapOf<String, Int>()
var k1: Int by mapVetoObserver(myMap, 0,
        { _, oldValue, newValue ->
            (oldValue > newValue).also {
                println("veto: $oldValue => $newValue = $it")
            }
        },
        { property, oldValue, newValue ->
            println("${property.name} changed from $oldValue to $newValue")
        })
var k2: Int by mapVetoObserver(myMap, 42,
        { _, oldValue, newValue ->
            (oldValue > newValue).also {
                println("veto: $oldValue => $newValue = $it")
            }
        },
        { property, oldValue, newValue ->
            println("${property.name} changed from $oldValue to $newValue")
        })

一些用法示例:

println("k1: $k1") // k1: 0
println("k2: $k2") // k2: 42
// absentValue isn't in map
println(myMap)     // {}

k1 = 5             // veto: 0 => 5 = false
                   // k1 changed from 0 to 5
println("k1: $k1") // k1: 5
k1 = 3             // veto: 5 => 3 = true
println("k1: $k1") // k1: 5
// changes propagate to the map
println(myMap)     // {k1=5}
// this circumvents veto
myMap["k1"] = 3
println("k1: $k1") // k1: 3
println(myMap)     // {k1=3}

注意:如果要在地图中使用默认值,请自己将它们放在此处

Note: if you want default values in the map, put them there yourself

这篇关于如何结合Kotlin委托属性:可观察,否决和“按地图"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆