aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/ExpiringValue.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-12-28 15:52:17 +0100
committerLinnea Gräf <nea@nea.moe>2024-12-28 15:52:17 +0100
commitc82d0b07033e02f7a6a23adbc1607ab3d41afdad (patch)
treebb48f950d7c4ed1ca96e087824ab6653500e0de4 /src/main/kotlin/moe/nea/ledger/ExpiringValue.kt
parent4d0de990e38632da9ad5c8b2d6ff90d259b2fcc6 (diff)
downloadLocalTransactionLedger-c82d0b07033e02f7a6a23adbc1607ab3d41afdad.tar.gz
LocalTransactionLedger-c82d0b07033e02f7a6a23adbc1607ab3d41afdad.tar.bz2
LocalTransactionLedger-c82d0b07033e02f7a6a23adbc1607ab3d41afdad.zip
feat: Add draonic sacrifice tracker
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger/ExpiringValue.kt')
-rw-r--r--src/main/kotlin/moe/nea/ledger/ExpiringValue.kt6
1 files changed, 4 insertions, 2 deletions
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<T>(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<T>(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
}