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

internal.IntenalUtils.kt Maven / Gradle / Ivy

There is a newer version: 2.1
Show newest version
package com.redmadrobot.mapmemory.internal

import com.redmadrobot.mapmemory.MapMemory
import com.redmadrobot.mapmemory.MapMemoryProperty
import kotlin.reflect.KProperty

@Suppress("NOTHING_TO_INLINE")
@PublishedApi
internal inline fun MapMemory.putNotNull(key: String, value: Any?) {
    if (value == null) {
        remove(key)
    } else {
        put(key, value)
    }
}

@PublishedApi
internal inline fun  MapMemory.getWithNullabilityInference(key: String): V {
    return if (null is V) {
        get(key)
    } else {
        getOrElse(key) { throw NoSuchElementException("Key $key is missing in the map.") }
    } as V
}

@PublishedApi
internal inline fun  MapMemory.getOrPutProperty(
    crossinline defaultValue: () -> V,
): MapMemoryProperty {
    return object : MapMemoryProperty() {
        override fun getValue(key: String): V = getOrPut(key) { defaultValue() } as V
        override fun setValue(key: String, value: V) = putNotNull(key, value)
    }
}

@PublishedApi
internal fun keyOf(thisRef: Any?, property: KProperty<*>): String {
    return if (thisRef != null) {
        "${thisRef.javaClass.name}#${property.name}"
    } else {
        property.name
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy