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

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

The newest version!
package net.projecttl.inventory.util

/**
 * An observable type of hashmaps. Triggers observers on data input.
 */
class ObservableHashMap: HashMap() {
    private val observers = arrayListOf<(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