24 lines
569 B
Kotlin

package net.sergecyh.diwan.tools
import kotlin.time.Clock
import kotlin.time.Duration
import kotlin.time.Instant
/**
* Value with expiration.
*/
@Suppress("unused")
class Expiring<T>(
val value: T,
val expiresAt: Instant,
) {
constructor(value: T, expiresIn: Duration) : this(value, Clock.System.now() + expiresIn)
/**
* @return value if not expired, null otherwise
*/
fun valueOrNull(): T? = if( isExpired ) value else null
val isExpired: Boolean get() = expiresAt < Clock.System.now()
val isOk: Boolean get() = !isExpired
}