commonMain.jetbrains.datalore.plot.config.OptionsSelector.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lets-plot-common Show documentation
Show all versions of lets-plot-common Show documentation
Lets-Plot JVM package without rendering part
/*
* 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
import jetbrains.datalore.base.enums.EnumInfoFactory
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 getNumber(path, item)?.toDouble()
}
fun Map<*, *>.getInt(vararg query: String): Int? {
return getInt(query.dropLast(1), query.last())
}
fun Map<*, *>.getInt(path: List, item: String): Int? {
return getNumber(path, item)?.toInt()
}
fun Map<*, *>.getNumber(vararg query: String): Number? {
return getNumber(query.dropLast(1), query.last())
}
fun Map<*, *>.getNumber(path: List, item: String): Number? {
return getMap(path)?.get(item) as? Number
}
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
}
}
inline fun >Map<*, *>.getEnum(path: List, item: String): EnumT? {
val name = getString(path, item) ?: return null
val enumInfo = EnumInfoFactory.createEnumInfo()
val value = enumInfo.safeValueOf(name)
require(value != null) {
"Unknown value \'$name\'. Expected: " + enumInfo.originalNames.joinToString(prefix = " [", separator = "|", postfix = "]") { "'${it.lowercase()}'"}
}
return value
}
inline fun >Map<*, *>.getEnum(vararg query: String): EnumT? {
return getEnum(query.dropLast(1), query.last())
}
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy