From e15ca5435c197e33ebe2a8624555a62b094df415 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Fri, 20 Dec 2024 19:36:18 +0100 Subject: [PATCH] Add more Tristate.of methods --- .../engine/base/type/Tristate.kt | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/base/src/main/kotlin/de/staropensource/engine/base/type/Tristate.kt b/base/src/main/kotlin/de/staropensource/engine/base/type/Tristate.kt index 72e441f..e04d67d 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/type/Tristate.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/type/Tristate.kt @@ -70,6 +70,94 @@ enum class Tristate { else FALSE } + /** + * Converts the specified [Byte] into a [Tristate]. + * + * `0` represents [FALSE], + * `1` represents [TRUE] and + * `2` represents [UNSET]. + * + * @param value [Byte] to convert + * @return [Tristate] representation of the specified [Byte] or `null` if not in range `0` to `2` + * @since v1-alpha10 + */ + @JvmStatic + @Throws(IndexOutOfBoundsException::class) + fun of(value: Byte): Tristate? { + return when (value) { + 0.toByte() -> FALSE + 1.toByte() -> TRUE + 2.toByte() -> UNSET + else -> null + } + } + + /** + * Converts the specified [UByte] into a [Tristate]. + * + * `0` represents [FALSE], + * `1` represents [TRUE] and + * `2` represents [UNSET]. + * + * @param value [UByte] to convert + * @return [Tristate] representation of the specified [UByte] or `null` if not in range `0` to `2` + * @since v1-alpha10 + */ + @JvmStatic + @Throws(IndexOutOfBoundsException::class) + fun of(value: UByte): Tristate? { + return when (value) { + 0u.toUByte() -> FALSE + 1u.toUByte() -> TRUE + 2u.toUByte() -> UNSET + else -> null + } + } + + /** + * Converts the specified [Short] into a [Tristate]. + * + * `0` represents [FALSE], + * `1` represents [TRUE] and + * `2` represents [UNSET]. + * + * @param value [Short] to convert + * @return [Tristate] representation of the specified [Short] or `null` if not in range `0` to `2` + * @since v1-alpha10 + */ + @JvmStatic + @Throws(IndexOutOfBoundsException::class) + fun of(value: Short): Tristate? { + return when (value) { + 0.toShort() -> FALSE + 1.toShort() -> TRUE + 2.toShort() -> UNSET + else -> null + } + } + + /** + * Converts the specified [UShort] into a [Tristate]. + * + * `0` represents [FALSE], + * `1` represents [TRUE] and + * `2` represents [UNSET]. + * + * @param value [UShort] to convert + * @return [Tristate] representation of the specified [UShort] or `null` if not in range `0` to `2` + * @since v1-alpha10 + */ + @JvmStatic + @Throws(IndexOutOfBoundsException::class) + fun of(value: UShort): Tristate? { + return when (value) { + 0u.toUShort() -> FALSE + 1u.toUShort() -> TRUE + 2u.toUShort() -> UNSET + else -> null + } + } + /** * Converts the specified [Int] into a [Tristate]. * @@ -91,6 +179,72 @@ enum class Tristate { else -> null } } + + /** + * Converts the specified [UInt] into a [Tristate]. + * + * `0` represents [FALSE], + * `1` represents [TRUE] and + * `2` represents [UNSET]. + * + * @param value [UInt] to convert + * @return [Tristate] representation of the specified [UInt] or `null` if not in range `0` to `2` + * @since v1-alpha10 + */ + @JvmStatic + @Throws(IndexOutOfBoundsException::class) + fun of(value: UInt): Tristate? { + return when (value) { + 0u -> FALSE + 1u -> TRUE + 2u -> UNSET + else -> null + } + } + + /** + * Converts the specified [Long] into a [Tristate]. + * + * `0` represents [FALSE], + * `1` represents [TRUE] and + * `2` represents [UNSET]. + * + * @param value [Long] to convert + * @return [Tristate] representation of the specified [Long] or `null` if not in range `0` to `2` + * @since v1-alpha10 + */ + @JvmStatic + @Throws(IndexOutOfBoundsException::class) + fun of(value: Long): Tristate? { + return when (value) { + 0L -> FALSE + 1L -> TRUE + 2L -> UNSET + else -> null + } + } + + /** + * Converts the specified [ULong] into a [Tristate]. + * + * `0` represents [FALSE], + * `1` represents [TRUE] and + * `2` represents [UNSET]. + * + * @param value [ULong] to convert + * @return [Tristate] representation of the specified [ULong] or `null` if not in range `0` to `2` + * @since v1-alpha10 + */ + @JvmStatic + @Throws(IndexOutOfBoundsException::class) + fun of(value: ULong): Tristate? { + return when (value) { + 0UL -> FALSE + 1UL -> TRUE + 2UL -> UNSET + else -> null + } + } } /**