aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt
diff options
context:
space:
mode:
authorEmpa <42304516+ItsEmpa@users.noreply.github.com>2024-07-15 19:54:04 +0200
committerGitHub <noreply@github.com>2024-07-15 19:54:04 +0200
commitba091f8c3cfa665e83c789e1a5845d1ae0f26b6b (patch)
treef52c6c37addc52c27452e0a161ee0b54485e093d /src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt
parent2de249337b48f5de439afd16c6793abbe3084a25 (diff)
downloadskyhanni-ba091f8c3cfa665e83c789e1a5845d1ae0f26b6b.tar.gz
skyhanni-ba091f8c3cfa665e83c789e1a5845d1ae0f26b6b.tar.bz2
skyhanni-ba091f8c3cfa665e83c789e1a5845d1ae0f26b6b.zip
Backend: RecalculatingValue as a ReadOnlyProperty (#2151)
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt b/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt
index 76f0c0c94..6059ed662 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt
@@ -1,17 +1,28 @@
package at.hannibal2.skyhanni.utils
+import kotlin.properties.ReadOnlyProperty
+import kotlin.reflect.KProperty
import kotlin.time.Duration
-class RecalculatingValue<T>(private val expireTime: Duration, val calculation: () -> T) {
+class RecalculatingValue<T>(private val expireTime: Duration, private val calculation: () -> T) : ReadOnlyProperty<Any?, T> {
- private var currentValue = calculation()
+ private var currentValue: Any? = UNINITIALIZED_VALUE
private var lastAccessTime = SimpleTimeMark.farPast()
+ @Deprecated("use \"by RecalculatingValue\" instead")
fun getValue(): T {
if (lastAccessTime.passedSince() > expireTime) {
currentValue = calculation()
lastAccessTime = SimpleTimeMark.now()
}
- return currentValue
+ @Suppress("UNCHECKED_CAST")
+ return currentValue as T
+ }
+
+ @Suppress("DEPRECATION")
+ override fun getValue(thisRef: Any?, property: KProperty<*>): T = getValue()
+
+ companion object {
+ private val UNINITIALIZED_VALUE = Any()
}
}