dictionary - 可在 map 上观察以检测何时添加、更新或删除条目

标签 dictionary kotlin delegates observable concurrenthashmap

我有一张 map ,在我的例子中是 ConcurrentHashMap<String, Device>在 websocket 上接收某些事件时正在更新。我想在这个 map 上实现一个 observable,以了解何时添加、更新或删除条目。我试过 ObservableProperty但 map 更改时不会调用任何方法。

var deviceCache : ConcurrentHashMap<String, Device> by MyObservable(ConcurrentHashMap())

 class MyObservable<T, V>(initialValue: ConcurrentHashMap<T, V>) : ObservableProperty<ConcurrentHashMap<T, V>>(initialValue) {
override fun afterChange(property: KProperty<*>, oldValue: ConcurrentHashMap<T, V>, newValue: ConcurrentHashMap<T, V>) {
  super.afterChange(property, oldValue, newValue)
  log.e("new value is $newValue")
}

override fun beforeChange(property: KProperty<*>, oldValue: ConcurrentHashMap<T, V>, newValue: ConcurrentHashMap<T, V>): Boolean {
  log.e("beforeChange called")
  return super.beforeChange(property, oldValue, newValue)
}

}

谁能帮我解决这个问题?

最佳答案

问题是 Map不是属性,您不能以这种方式使用属性委托(delegate)。你要做的就是为 Map 写一个装饰器。像这样:

class ObservableMap<K, V>(private val map: MutableMap<K, V>) : MutableMap<K, V> by map {

    override fun put(key: K, value: V): V? {
        TODO("not implemented")
    }

    override fun putAll(from: Map<out K, V>) {
        TODO("not implemented")
    }

    override fun remove(key: K): V? {
        TODO("not implemented")
    }

}

这里我们将所有操作委托(delegate)给后台map在上述方法中添加/删除时,您只需要实现您的逻辑。

我不确定您所说的 update 是什么意思但是如果你的意思是“ map 中的一个值被覆盖”,那么你可以在 put 中处理它。 .

你可以用这个ObservableMap像这样:
val map = ObservableMap(ConcurrentHashMap<String, String>())

注意如果要支持ConcurrentHashMap的操作您还需要包含 overrides对于 AbstractMap<K,V>ConcurrentMap<K,V>因为他们添加了一些您可能想要跟踪的新操作。上面的代码只是一个例子。

关于dictionary - 可在 map 上观察以检测何时添加、更新或删除条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56440641/

相关文章:

kotlin - 带有请求队列的 Kotlin 服务

c# - 注入(inject)通用的 getter 和 setter 以获得比反射更好的性能

c# - 帮助将小型 C# WCF 应用程序转换为 Visual Basic(第 2 部分)

python - 什么决定了 Python 字典中数据的顺序?

iOS 后台 MKPointAnnotation

generics - 使用通用函数不适用于使用 jackson 加载 YAML 列表

java - 用gradle排除compileOnly依赖项中已经包含的包

Python:如何处理不可订阅的对象?

c# - 以类为键的字典

c# - C#中的委托(delegate)问题