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

ru.hnau.jutils.helpers.Holder.kt Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package ru.hnau.jutils.helpers


abstract class Holder {

    private val locks = LinkedHashSet()

    fun hold(lock: T) = synchronized(locks) {
        val added = locks.add(lock)
        if (added && locks.size == 1) {
            onFirstHolt()
        }
    }

    fun release(lock: T) = synchronized(this) {
        val removed = locks.remove(lock)
        if (removed && locks.isEmpty()) {
            onLastReleased()
        }
    }

    protected abstract fun onFirstHolt()

    protected abstract fun onLastReleased()

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy