aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt
blob: fae86fac57cfe901c4faac3b992ded58cdf04242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package at.hannibal2.skyhanni.utils

import kotlin.time.Duration

class TimeLimitedSet<T: Any>(
    expireAfterWrite: Duration,
    private val removalListener: (T) -> Unit = {},
) {

    private val cache = TimeLimitedCache<T, Unit>(expireAfterWrite) { key, _ -> key?.let { removalListener(it) } }

    fun add(element: T) = cache.put(element, Unit)

    operator fun contains(element: T): Boolean = cache.containsKey(element)

    fun clear() = cache.clear()

    fun toSet(): Set<T> = cache.keys().toSet()
}