ListenableMap and ListenableSet runnable callback
This commit is contained in:
parent
84d17349b7
commit
f4543919e5
@ -4,7 +4,7 @@ kotlin.code.style=official
|
||||
specifyKotlinAsDependency=false
|
||||
|
||||
projectGroup=ru.dbotthepony.kommons
|
||||
projectVersion=2.9.3
|
||||
projectVersion=2.9.4
|
||||
|
||||
guavaDepVersion=33.0.0
|
||||
gsonDepVersion=2.8.9
|
||||
|
@ -13,6 +13,20 @@ open class ListenableMap<K, V>(protected val backingMap: MutableMap<K, V> = Hash
|
||||
fun onValueRemoved(key: K, value: V)
|
||||
}
|
||||
|
||||
data class RunnableAdapter<K, V>(private val parent: Runnable) : MapListener<K, V> {
|
||||
override fun onClear() {
|
||||
parent.run()
|
||||
}
|
||||
|
||||
override fun onValueAdded(key: K, value: V) {
|
||||
parent.run()
|
||||
}
|
||||
|
||||
override fun onValueRemoved(key: K, value: V) {
|
||||
parent.run()
|
||||
}
|
||||
}
|
||||
|
||||
protected open fun onClear() {
|
||||
listeners.forEach { it.callback.onClear() }
|
||||
}
|
||||
@ -41,6 +55,10 @@ open class ListenableMap<K, V>(protected val backingMap: MutableMap<K, V> = Hash
|
||||
return Listener(listener)
|
||||
}
|
||||
|
||||
fun addListener(listener: Runnable): Listenable.L {
|
||||
return Listener(RunnableAdapter(listener))
|
||||
}
|
||||
|
||||
fun clearListeners() {
|
||||
listeners.clear()
|
||||
}
|
||||
|
@ -13,6 +13,20 @@ open class ListenableSet<E>(protected val backingSet: MutableSet<E>) : MutableSe
|
||||
fun onValueRemoved(element: E)
|
||||
}
|
||||
|
||||
data class RunnableAdapter<E>(private val parent: Runnable) : SetListener<E> {
|
||||
override fun onClear() {
|
||||
parent.run()
|
||||
}
|
||||
|
||||
override fun onValueAdded(element: E) {
|
||||
parent.run()
|
||||
}
|
||||
|
||||
override fun onValueRemoved(element: E) {
|
||||
parent.run()
|
||||
}
|
||||
}
|
||||
|
||||
protected open fun onClear() {
|
||||
listeners.forEach { it.callback.onClear() }
|
||||
}
|
||||
@ -41,6 +55,10 @@ open class ListenableSet<E>(protected val backingSet: MutableSet<E>) : MutableSe
|
||||
return Listener(listener)
|
||||
}
|
||||
|
||||
fun addListener(listener: Runnable): Listenable.L {
|
||||
return Listener(RunnableAdapter(listener))
|
||||
}
|
||||
|
||||
fun clearListeners() {
|
||||
listeners.clear()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user