io.github.freya022.botcommands.internal.components.repositories.EphemeralHandlers.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of BotCommands Show documentation
Show all versions of BotCommands Show documentation
A Kotlin-first (and Java) framework that makes creating Discord bots a piece of cake, using the JDA library.
package io.github.freya022.botcommands.internal.components.repositories
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
internal abstract class EphemeralHandlers {
private val lock = ReentrantLock()
private val map = hashMapOf()
private var currentId: Int = 0
operator fun get(handlerId: Int): T? = map[handlerId]
fun put(handler: T) = lock.withLock {
val id = currentId++
map[id] = handler
return@withLock id
}
fun remove(handlerId: Int) = lock.withLock {
map.remove(handlerId)
}
}