aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt
diff options
context:
space:
mode:
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()
}
}