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

p-sim-ecu.doip-sim-ecu-dsl.0.14.0.source-code.DataStorage.kt Maven / Gradle / Ivy

Go to download

This is a kotlin based domain specific language (dsl), to quickly and intuitively write custom DoIP ECU simulations.

There is a newer version: 0.15.1
Show newest version
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

public class StoragePropertyDelegate(
    private val storage: MutableMap,
    private val initialValue: () -> T) : ReadWriteProperty {

    override operator fun getValue(thisRef: Nothing?, property: KProperty<*>): T {
        if (!storage.containsKey(property.name)) {
            storage[property.name] = initialValue.invoke()
        }
        @Suppress("UNCHECKED_CAST")
        return storage[property.name] as T
    }

    override operator fun setValue(thisRef: Nothing?, property: KProperty<*>, value: T) {
        storage[property.name] = value
    }
}

public open class DataStorage {
    private val internalDataStorage: MutableMap = mutableMapOf()

    /**
     * Create a persistent property by means of delegation, with an initial value
     * calculated by initialValue
     */
    public fun  storedProperty(initialValue: () -> T): StoragePropertyDelegate =
        StoragePropertyDelegate(this.internalDataStorage, initialValue)

    /**
     * Clear all stored properties
     */
    public fun clearStoredProperties(): Unit =
        internalDataStorage.clear()
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy