commonMain.jetbrains.datalore.base.enums.EnumInfoImpl.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.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.toUpperCase()
}
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