Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// <https://github.com/tailwhiper/kotlin-enum-extensions>
inline fun <reified E : Enum<E>> valueOf(name: String, default: E) =
enumValues<E>().find { it.name == name } ?: default
inline fun <reified E : Enum<E>> valueOf(ordinal: Int, default: E) =
enumValues<E>().find { it.ordinal == ordinal } ?: default
inline fun <reified E : Enum<E>> valueOfWithCondition(condition: (E) -> Boolean) =
enumValues<E>().find(condition)
inline fun <reified E : Enum<E>> valueOfWithCondition(condition: (E) -> Boolean, default: E) =
enumValues<E>().find(condition) ?: default
inline fun <reified E : Enum<E>> valueOfIgnoreCase(name: String) =
enumValues<E>().find { it.name.toUpperCase() == name.toUpperCase() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment