Skip to content

Instantly share code, notes, and snippets.

@jiffle
Created September 9, 2021 08:09
Show Gist options
  • Save jiffle/dabae9409b7edc39597aa304a6e78b00 to your computer and use it in GitHub Desktop.
Save jiffle/dabae9409b7edc39597aa304a6e78b00 to your computer and use it in GitHub Desktop.
Refactor-Safe Enum Pattern in Kotlin
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