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

com.lightningkite.lightningserver.settings.Pluggable.kt Maven / Gradle / Ivy

The newest version!
package com.lightningkite.lightningserver.settings

abstract class Pluggable {
    private val handlers = HashMap T>()
    val options: Set get() = handlers.keys
    fun register(key: String, handler: (S) -> T) {
        handlers[key] = handler
    }

    fun parse(key: String, setting: S): T {
        val h = handlers[key]
            ?: throw IllegalArgumentException("No handler $key for ${this::class.qualifiedName} - available handlers are ${options.joinToString()}")
        return h(setting)
    }

    fun parseParameterString(params: String): Map> = params
        .takeIf { it.isNotBlank() }
        ?.split("&")
        ?.filter { it.isNotBlank() }
        ?.map {
            it.substringBefore('=') to it.substringAfter('=', "")
        }
        ?.groupBy { it.first }
        ?.mapValues { it.value.map { it.second } }
        ?: emptyMap()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy