All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hnau.base.data.helpers.Outdatable.kt Maven / Gradle / Ivy

There is a newer version: 2.1.9
Show newest version
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
            )
        }
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy