CachedImpl

data class CachedImpl<T>(generator: () -> T) : Cached<T>

Default Cached implementation returned by Cached.of, memoizing the result of generator in an Optional until invalidated. Not meant to be instantiated directly; use Cached.of instead.

Constructors

Link copied to clipboard
constructor(generator: () -> T)

Properties

Link copied to clipboard
open override val isInvalid: Boolean

Whether value needs to be (re)computed, by invoking generator again, upon the next access. Always the logical negation of isValid.

Link copied to clipboard
open override val isValid: Boolean

Whether value currently holds an up-to-date, already-computed result (i.e. generator does not need to be invoked again upon the next access).

Link copied to clipboard
open override val value: T

Retrieves the cached value, computing it first (via the generator passed to Cached.of) if it is currently isInvalid.

Functions

Link copied to clipboard
open fun <R> ifValid(consumer: (T) -> R): Optional<out R>

Applies consumer to the cached value and returns the result wrapped in Optional.Some, but only if the value is currently isValid; otherwise, returns Optional.None without forcing a (re)computation.

Link copied to clipboard
open override fun invalidate()

Marks the cached value as isInvalid, so that it is recomputed upon the next access to value.

Link copied to clipboard
open override fun regenerate()

Ensures value holds an up-to-date result, computing it via the generator passed to Cached.of if it is currently isInvalid. Does nothing if the value is already isValid.

Link copied to clipboard
open fun <R> regenerating(consumer: (T) -> R): R

Ensures the cached value is up-to-date (see regenerate), then applies consumer to it, returning the result of consumer.