org.hnau.base.data.helpers.Outdatable.kt Maven / Gradle / Ivy
package org.hnau.base.data.helpers
import org.hnau.base.extensions.boolean.checkTruth
import org.hnau.base.extensions.checkNotNull
@Suppress("DEPRECATION")
class Outdatable(
@Deprecated("Should have been private, but inline fun fun checkActual() expected using only public variables")
val lifetime: Time? = null,
@Deprecated("Should have been private, but inline fun fun checkActual() expected using only public variables")
val initialValue: T
) {
@Deprecated("Should have been private, but inline fun fun checkActual() expected using only public variables")
val timestamp = Time.now()
}
@Suppress("DEPRECATION")
inline fun Outdatable?.checkActual(
ifActual: (T) -> R,
ifOutdated: () -> R
) = this.checkNotNull(
ifNull = ifOutdated,
ifNotNull = { outdatable ->
val lifetime = outdatable.lifetime
val timestamp = outdatable.timestamp
val isActual = lifetime == null || timestamp + lifetime >= Time.now()
isActual.checkTruth(
ifTrue = { ifActual(outdatable.initialValue) },
ifFalse = ifOutdated
)
}
)