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

main.wisp.lease.Extensions.kt Maven / Gradle / Ivy

There is a newer version: 2024.09.17.200749-4708422
Show newest version
package wisp.lease

/** Converts a [lease] into an [AutoCloseable] resource. */
class AutoCloseableLease(private val lease: Lease) : Lease by lease, AutoCloseable {
    override fun close() {
        lease.release()
    }
}

/**
 * Attempts to acquire an [AutoCloseableLease].
 *
 * Use like
 *
 * ```
 * leaseManager.acquireOrNull("some-lease")?.use { lease ->
 *   // Do something with the lease.
 * }
 * ```
 */
fun LeaseManager.acquireOrNull(name: String): AutoCloseableLease? {
    val lease = requestLease(name)
    return if (lease.acquire()) AutoCloseableLease(lease) else null
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy