Is there a bettery way for doing this? (I want to create several enums that have an ID, then I want to be able to access those enums by ID fast and easily). My current approach is the following:
interface IEnumId { val id: Int }
open class EnumById<T : IEnumId>(val values: Array<T>) {
private val valuesById = values.associateBy { it.id }
operator fun get(id: Int): T = valuesById[id] ?: invalidOp("Invalid cap style!")
}
enum class LineCapsStyle(override val id: Int, val str: String) : IEnumId {
ROUND(0, "round"), NONE(1, "none"), SQUARE(2, "square");
companion object : EnumById<LineCapsStyle>(LineCapsStyle.values())
}
enum class LineJointStyle(override val id: Int, val str: String) : IEnumId {
ROUND(0, "round"), BEVEL(1, "bevel"), MITER(2, "miter");
companion object : EnumById<LineJointStyle>(LineJointStyle.values())
}