diff options
author | Empa <42304516+ItsEmpa@users.noreply.github.com> | 2024-07-15 19:54:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-15 19:54:04 +0200 |
commit | ba091f8c3cfa665e83c789e1a5845d1ae0f26b6b (patch) | |
tree | f52c6c37addc52c27452e0a161ee0b54485e093d /src/main/java/at/hannibal2/skyhanni/utils/RecalculatingValue.kt | |
parent | 2de249337b48f5de439afd16c6793abbe3084a25 (diff) | |
download | skyhanni-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.kt | 17 |
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() } } |