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

org.jetbrains.android.anko.config.ConfigurationKey.kt Maven / Gradle / Ivy

The newest version!
package org.jetbrains.android.anko.config

interface ConfigurationKey {
    val name: String
    val defaultValue: T?
}

fun  ConfigurationKey(
    name: String,
    defaultValue: T? = null
): ConfigurationKey = ConfigurationKeyImpl(name, defaultValue)

private open class ConfigurationKeyImpl(
    override val name: String,
    override val defaultValue: T? = null
) : ConfigurationKey

class CliConfigurationKey(
    name: String,
    val cliName: String = name,
    defaultValue: T? = null,
    val mapper: (String) -> T?
) : ConfigurationKey by ConfigurationKeyImpl(name, defaultValue)

interface Options {
    operator fun  get(key: ConfigurationKey): T = opt(key)!!
    fun  opt(key: ConfigurationKey): T?

    operator fun contains(key: ConfigurationKey<*>) = opt(key) != null
}

interface MutableOptions : Options {
    operator fun  set(key: ConfigurationKey, value: T)

    companion object {
        fun create(): MutableOptions = OptionsImpl()
    }
}

fun  MutableOptions.setCliOption(key: CliConfigurationKey, stringValue: String) {
    val value = key.mapper(stringValue)
    if (value != null) {
        this[key] = value
    }
}

private class OptionsImpl : MutableOptions {
    private val options = mutableMapOf, Any>()

    @Suppress("UNCHECKED_CAST")
    override fun  opt(key: ConfigurationKey) = options[key] as T? ?: key.defaultValue

    override fun  set(key: ConfigurationKey, value: T) {
        options[key] = value
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy