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

ru.hnau.jutils.cache.AutoPossibleCache.kt Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package ru.hnau.jutils.cache

import ru.hnau.jutils.possible.Possible


open class AutoPossibleCache(
        private val getter: (K) -> Possible,
        capacity: Int
) : Cache(
        capacity
) {

    operator fun get(key: K): Possible = synchronized(this) {
        val existenceValue = getValue(key)
        if (existenceValue != null) {
            return@synchronized Possible.success(existenceValue)
        }
        val possibleValue = getter.invoke(key)
        val possibleData = possibleValue.data
        if (possibleData != null) {
            putValue(key, possibleData)
        }
        return@synchronized possibleValue
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy