andriesfc / business-mapped-enum-sample

Sample how to use declare custom `static` lookups on enums in Kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handling business mapped values as Enums

Sample how to use and declare custom lookups on enums based business codes.

This enables code patterns such as:

fun main() {
    println(LinkMode.fromCode("bi"))
    println(LinkMode.fromPosition(2))
}

enum class LinkMode(private val code: String, private val position: Int) {

    UNI_DIRECTION("uni", 1),
    NONE("none", 0),
    BI_DIRECTION("bi", 3);

    companion object  {
        val fromPosition = enumFromMapped(LinkMode::position)
        val fromCode = enumFromMapped(LinkMode::code)
    }
}

About

Sample how to use declare custom `static` lookups on enums in Kotlin

License:GNU General Public License v3.0


Languages

Language:Kotlin 100.0%