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

commonMain.jetbrains.datalore.base.enums.EnumInfoImpl.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.base.enums

class EnumInfoImpl>(enumConstants: Array) : EnumInfo {

    private val myNormalizedValueMap: Map
    private val myOriginalNames: List

    override val originalNames: List
        get() = myOriginalNames

    private fun toNormalizedName(name: String): String {
        return name.uppercase()
    }

    init {
        val numConstants = enumConstants.size
        val valueMap = HashMap(numConstants)
        val originalNames = ArrayList(numConstants)
        for (value in enumConstants) {
            val originalName = value.toString()
            originalNames.add(originalName)
            val normalizedName = toNormalizedName(originalName)
            val oldValue = valueMap.put(normalizedName, value)
            if (oldValue != null) {
                throw IllegalArgumentException("duplicate values: '$value', '$oldValue'")
            }
        }
        myOriginalNames = originalNames
        myNormalizedValueMap = valueMap
    }

    override fun safeValueOf(name: String?, defaultValue: EnumT): EnumT {
        val value = safeValueOf(name)
        return value ?: defaultValue
    }

    override fun safeValueOf(name: String?): EnumT? {
        val result: EnumT?
        if (hasValue(name)) {
            result = myNormalizedValueMap[toNormalizedName(name!!)]
        } else {
            result = null
        }
        return result
    }

    override fun hasValue(name: String?): Boolean {
        return name != null && myNormalizedValueMap.containsKey(toNormalizedName(name))
    }

    override fun unsafeValueOf(name: String): EnumT {
        return safeValueOf(name) ?: throw IllegalArgumentException("name not found: '$name'")
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy