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

commonMain.jetbrains.datalore.plot.config.OptionsSelector.kt Maven / Gradle / Ivy

There is a newer version: 4.5.3-alpha1
Show newest version
/*
 * Copyright (c) 2019. JetBrains s.r.o.
 * Use of this source code is governed by the MIT license that can be found in the LICENSE file.
 */

package jetbrains.datalore.plot.config

fun Map<*, *>.read(vararg query: String): Any? {
    return read(query.dropLast(1), query.last())
}

fun Map<*, *>.read(path: List, item: String): Any? {
    return getMap(path)?.get(item)
}

fun Map<*, *>.write(vararg query: String, value: () -> Any) {
    write(query.dropLast(1), query.last(), value())
}

fun Map<*, *>.write(path: List, item: String, value: Any) {
    provideMap(path)[item] = value
}

fun Map<*, *>.remove(vararg query: String) {
    remove(query.dropLast(1), query.last())
}

fun Map<*, *>.remove(path: List, item: String) {
    getMap(path)?.asMutable()?.remove(item)
}

fun Map<*, *>.has(vararg query: String): Boolean {
    return has(query.dropLast(1), query.last())
}

fun Map<*, *>.has(path: List, item: String): Boolean {
    return getMap(path)?.containsKey(item) ?: false
}

fun Map<*, *>.getString(vararg query: String): String? {
    return getString(query.dropLast(1), query.last())
}

fun Map<*, *>.getString(path: List, item: String): String? {
    return getMap(path)?.get(item) as? String
}

fun Map<*, *>.getDouble(vararg query: String): Double? {
    return getDouble(query.dropLast(1), query.last())
}

fun Map<*, *>.getDouble(path: List, item: String): Double? {
    return getMap(path)?.get(item) as? Double
}

fun Map<*, *>.getBool(vararg query: String): Boolean? {
    return getBool(query.dropLast(1), query.last())
}

fun Map<*, *>.getBool(path: List, item: String): Boolean? {
    return when (val v = getMap(path)?.get(item)) {
        is String -> when(v.lowercase()) {
            "1", "true" -> true
            "0", "false" -> false
            else -> throw IllegalArgumentException("Unexpected boolean value: '$v'")
        }
        is Number -> v.toInt() != 0
        is Boolean -> v
        else -> null
    }
}

fun Map<*, *>.getMap(vararg query: String): Map? {
    return getMap(query.toList())?.typed()
}

fun Map<*, *>.getMap(path: List): Map? {
    return path.fold?>(this, { section, next -> section?.read(next)?.let { it as? Map<*, *> } ?: return@fold null } )?.typed()
}

fun Map<*, *>.getList(vararg query: String): List<*>? {
    return getList(query.dropLast(1), query.last())
}

fun Map<*, *>.getList(path: List, item: String): List<*>? {
    return getMap(path)?.get(item) as? List<*>
}

fun Map<*, *>.getMaps(vararg query: String): List>? {
    return getList(*query)?.mapNotNull { it as? Map<*, *> }?.toList()
}

fun Map<*, *>.provideMap(vararg query: String): MutableMap {
    return provideMap(query.toList())
}

fun Map<*, *>.provideMap(path: List): MutableMap {
    return path.fold(
        this
    ) { acc, next ->
        acc.asMutable().getOrPut(
            key = next,
            defaultValue = { HashMap() }
        ) as Map<*, *>
    }.asMutable()
}

fun Map<*, *>.provideMaps(vararg query: String): MutableList> {
    return provideMaps(query.dropLast(1), query.last()).asMutable()
}

fun Map<*, *>.provideMaps(path: List, item: String): MutableList> {
    @Suppress("UNCHECKED_CAST")
    return provideMap(path).getOrPut(item, { mutableListOf>() }) as MutableList>
}

@Suppress("UNCHECKED_CAST")
fun Map<*, *>.asMutable(): MutableMap {
    return this as MutableMap
}

@Suppress("UNCHECKED_CAST")
fun  Map<*, *>.typed(): Map {
    return this as Map
}

@Suppress("UNCHECKED_CAST")
fun  List.asMutable(): MutableList {
    return this as MutableList
}

@Suppress("UNCHECKED_CAST")
fun List<*>.asMaps(): List> {
    return this as List>
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy