From 6aa6095cd03fa210c0efb7ba4a089fa15b434871 Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Thu, 26 Sep 2024 16:23:48 +0200 Subject: [PATCH] refactor: Slightly improve enum extensions and usage (#948) Use `enumEntries` instead of `enumValues`, which is now recommended. Replace a `.getOrNull(...) ?: other` with `.getOrElse(...) { other }`. --- .../app/pachli/core/common/extensions/EnumExtensions.kt | 8 +++++--- .../main/kotlin/app/pachli/core/network/model/Status.kt | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/core/common/src/main/kotlin/app/pachli/core/common/extensions/EnumExtensions.kt b/core/common/src/main/kotlin/app/pachli/core/common/extensions/EnumExtensions.kt index 0786de171..6a90c9b5b 100644 --- a/core/common/src/main/kotlin/app/pachli/core/common/extensions/EnumExtensions.kt +++ b/core/common/src/main/kotlin/app/pachli/core/common/extensions/EnumExtensions.kt @@ -17,6 +17,8 @@ package app.pachli.core.common.extensions +import kotlin.enums.enumEntries + // Note: Technically these don't have to be extension methods on // Enum.Companion. You could remove that and write: // @@ -38,17 +40,17 @@ package app.pachli.core.common.extensions * * @see [kotlin.Array.get] */ -inline fun > Enum.Companion.get(ordinal: Int) = enumValues()[ordinal] +inline fun > Enum.Companion.get(ordinal: Int) = enumEntries()[ordinal] /** * Returns the enum constant with the given [ordinal] value or the result * of calling the [defaultValue] function if the [ordinal] is out of bounds of * this enum. */ -inline fun > Enum.Companion.getOrElse(ordinal: Int, defaultValue: (Int) -> E) = enumValues().getOrElse(ordinal, defaultValue) +inline fun > Enum.Companion.getOrElse(ordinal: Int, defaultValue: (Int) -> E) = enumEntries().getOrElse(ordinal, defaultValue) /** * Returns the enum constant with the given [ordinal] value or `null` if the * [ordinal] is out of bounds of this enum */ -inline fun > Enum.Companion.getOrNull(ordinal: Int) = enumValues().getOrNull(ordinal) +inline fun > Enum.Companion.getOrNull(ordinal: Int) = enumEntries().getOrNull(ordinal) diff --git a/core/network/src/main/kotlin/app/pachli/core/network/model/Status.kt b/core/network/src/main/kotlin/app/pachli/core/network/model/Status.kt index 62ad960e0..2f13d71c5 100644 --- a/core/network/src/main/kotlin/app/pachli/core/network/model/Status.kt +++ b/core/network/src/main/kotlin/app/pachli/core/network/model/Status.kt @@ -19,7 +19,7 @@ package app.pachli.core.network.model import android.text.SpannableStringBuilder import android.text.style.URLSpan -import app.pachli.core.common.extensions.getOrNull +import app.pachli.core.common.extensions.getOrElse import app.pachli.core.network.json.Default import app.pachli.core.network.json.HasDefault import app.pachli.core.network.parseAsMastodonHtml @@ -101,7 +101,7 @@ data class Status( companion object { @JvmStatic - fun getOrUnknown(index: Int) = Enum.getOrNull(index) ?: UNKNOWN + fun getOrUnknown(index: Int) = Enum.getOrElse(index) { UNKNOWN } @JvmStatic fun byString(s: String): Visibility {