From c82d0b07033e02f7a6a23adbc1607ab3d41afdad Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sat, 28 Dec 2024 15:52:17 +0100 Subject: feat: Add draonic sacrifice tracker --- src/main/kotlin/moe/nea/ledger/ExpiringValue.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/main/kotlin/moe/nea/ledger/ExpiringValue.kt') diff --git a/src/main/kotlin/moe/nea/ledger/ExpiringValue.kt b/src/main/kotlin/moe/nea/ledger/ExpiringValue.kt index dac4751..b50b14e 100644 --- a/src/main/kotlin/moe/nea/ledger/ExpiringValue.kt +++ b/src/main/kotlin/moe/nea/ledger/ExpiringValue.kt @@ -7,8 +7,10 @@ class ExpiringValue(private val value: T) { val lastSeenAt: Long = System.nanoTime() val age get() = (System.nanoTime() - lastSeenAt).nanoseconds var taken = false + private set + fun get(expiry: Duration): T? { - return if (!taken && expiry > age) value + return if (!taken && age < expiry) value else null } @@ -21,7 +23,7 @@ class ExpiringValue(private val value: T) { } } - fun consume(expiry: Duration): T? = get(expiry).also { take() } + fun consume(expiry: Duration): T? = get(expiry)?.also { take() } fun take() { taken = true } -- cgit