ot.mapmemory.mapmemory.2.0.source-code.SharedMapMemoryProperty.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapmemory Show documentation
Show all versions of mapmemory Show documentation
Simple in-memory cache conception built on Map
package com.redmadrobot.mapmemory
import com.redmadrobot.mapmemory.internal.getWithNullabilityInference
import com.redmadrobot.mapmemory.internal.putNotNull
import kotlin.reflect.KProperty
/**
* Returns property delegate to access shared value in memory associated with the given [key].
* ```
* var host: String by memory.shared("selectedServer")
* ```
*/
public inline fun MapMemory.shared(key: String): MapMemoryProperty {
return object : MapMemoryProperty() {
override fun getValue(key: String): T = getWithNullabilityInference(key)
override fun setValue(key: String, value: T) = putNotNull(key, value)
}.shared(key)
}
/**
* Makes property delegate shared to access value in memory associated with the given [key].
* ```
* var answer: Int by memory { 42 }.shared("ultimateAnswer")
* ```
*/
public fun MapMemoryProperty.shared(key: String): MapMemoryProperty {
return if (this is SharedMapMemoryProperty) {
property.shared(key)
} else {
SharedMapMemoryProperty(key, this)
}
}
private class SharedMapMemoryProperty(
val key: String,
val property: MapMemoryProperty,
) : MapMemoryProperty() {
override fun getValue(thisRef: Any?, property: KProperty<*>): V = getValue(key)
override fun setValue(thisRef: Any?, property: KProperty<*>, value: V) = setValue(key, value)
override fun getValue(key: String): V = property.getValue(key)
override fun setValue(key: String, value: V) = property.setValue(key, value)
}