Created
September 9, 2021 08:09
-
-
Save jiffle/dabae9409b7edc39597aa304a6e78b00 to your computer and use it in GitHub Desktop.
Refactor-Safe Enum Pattern in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonValue | |
enum class AccountStatus(@JsonValue val text: String) { | |
PENDING("Pending"), | |
ACTIVE("Active"), | |
EXPIRED("Expired"), | |
CANCELLED("Cancelled"); | |
companion object { | |
private val valuesByText = values().associateBy { it.text } | |
fun of(text: String?): AccountStatus? = text?.let { valuesByText[it] } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment