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

ot.mapmemory.mapmemory.2.0.source-code.SharedMapMemoryProperty.kt Maven / Gradle / Ivy

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy