All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.projecttl.inventory.util.ObservableHashMap.kt Maven / Gradle / Ivy

There is a newer version: 4.6.0
Show newest version
package net.projecttl.inventory.util

/**
 * An observable type of hashmaps. Triggers observers on data input.
 */
class ObservableHashMap: HashMap() {
    private val observers = ArrayList<(Pair) -> Unit>()

    fun trigger(key: K, value: V) {
        observers.forEach {
            it.invoke(key to value)
        }
    }

    fun addObserver(observer: (Pair) -> Unit) {
        observers.add(observer)
    }

    override fun put(key: K, value: V): V? {
        val result = super.put(key, value)
        trigger(key, value)
        return result
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy