From 50e438a16bbcd69c81ccc514349f4f6bda1ebd13 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Fri, 20 Dec 2024 00:35:43 +0100 Subject: [PATCH] Add Tristate --- .../engine/base/logging/Logger.kt | 2 +- .../engine/base/type/Tristate.kt | 265 ++++++++++++++++++ .../engine/base/utility/Environment.kt | 2 +- .../engine/base/utility/FileAccess.kt | 2 +- .../base/utility/dnihbd/BuildInformation.kt | 2 +- settings.gradle.kts | 7 +- 6 files changed, 275 insertions(+), 5 deletions(-) create mode 100644 base/src/main/kotlin/de/staropensource/engine/base/type/Tristate.kt diff --git a/base/src/main/kotlin/de/staropensource/engine/base/logging/Logger.kt b/base/src/main/kotlin/de/staropensource/engine/base/logging/Logger.kt index 6bd72f0..5877d98 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/logging/Logger.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/logging/Logger.kt @@ -64,7 +64,7 @@ class Logger { * @since v1-alpha10 */ @JvmStatic - @Suppress("unused") + @Suppress("Unused") val instance = Logger() } 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 new file mode 100644 index 0000000..ab4cdd7 --- /dev/null +++ b/base/src/main/kotlin/de/staropensource/engine/base/type/Tristate.kt @@ -0,0 +1,265 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU Affero General Public License v3 + * with an exception allowing classpath linking. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.type + +/** + * Defines a data type which can have + * three valid states: [UNSET], [TRUE] + * and [FALSE]. + * + * @since v1-alpha10 + */ +@Suppress("Unused") +enum class Tristate { + /** + * Defines an unset state. + * + * @since v1-alpha10 + */ + UNSET, + + /** + * Defines a true state. + * + * @since v1-alpha10 + */ + TRUE, + + /** + * Defines a false state. + * + * @since v1-alpha10 + */ + FALSE; + + /** + * Companion object of [Tristate]. + * + * @since v1-alpha10 + */ + companion object { + /** + * Converts the specified [Boolean] into a [Tristate]. + * + * @param value [Boolean] to convert + * @return [Tristate] representation of the specified [Boolean] + * @since v1-alpha10 + */ + @JvmStatic + fun toTristate(value: Boolean): Tristate { + return if (value) TRUE + else FALSE + } + + /** + * Converts the specified [Int] + * into a [Tristate]. + * + * `0` represents [FALSE], + * `1` represents [TRUE] and + * `2` represents [UNSET]. + * + * @param value [Int] to convert + * @return [Tristate] representation of the specified [Int] or `null` if not in range `0` to `2` + * @since v1-alpha10 + */ + @JvmStatic + @Throws(IndexOutOfBoundsException::class) + fun toTristate(value: Int): Tristate? { + return when (value) { + 0 -> FALSE + 1 -> TRUE + 2 -> UNSET + else -> null + } + } + } + + /** + * Converts this [Tristate] into + * a [Boolean]. + * + * If this [Tristate] is set to + * [UNSET], `null` will be returned. + * + * @return [Boolean] representation of this [Tristate] or `null` if [UNSET] + * @since v1-alpha10 + */ + fun toBoolean(): Boolean? { + return when (this) { + TRUE -> true + FALSE -> false + UNSET -> null + } + } + + /** + * Converts this [Tristate] into + * a [Byte]. + * + * [FALSE] represents `0`, + * [TRUE] represents `1` and + * [UNSET] represents `2`. + * + * @return [Byte] representation of this [Tristate] or `null` if [UNSET] + * @since v1-alpha10 + */ + fun toByte(): Byte { + return when (this) { + FALSE -> 0 + TRUE -> 1 + UNSET -> 2 + } + } + + /** + * Converts this [Tristate] into + * an [UByte]. + * + * [FALSE] represents `0`, + * [TRUE] represents `1` and + * [UNSET] represents `2`. + * + * @return [UByte] representation of this [Tristate] or `null` if [UNSET] + * @since v1-alpha10 + */ + fun toUByte(): UByte { + return when (this) { + FALSE -> 0u + TRUE -> 1u + UNSET -> 2u + } + } + + /** + * Converts this [Tristate] into + * a [Short]. + * + * [FALSE] represents `0`, + * [TRUE] represents `1` and + * [UNSET] represents `2`. + * + * @return [Short] representation of this [Tristate] or `null` if [UNSET] + * @since v1-alpha10 + */ + fun toShort(): Short { + return when (this) { + FALSE -> 0 + TRUE -> 1 + UNSET -> 2 + } + } + + /** + * Converts this [Tristate] into + * an [UShort]. + * + * [FALSE] represents `0`, + * [TRUE] represents `1` and + * [UNSET] represents `2`. + * + * @return [UShort] representation of this [Tristate] or `null` if [UNSET] + * @since v1-alpha10 + */ + fun toUShort(): UShort { + return when (this) { + FALSE -> 0u + TRUE -> 1u + UNSET -> 2u + } + } + + /** + * Converts this [Tristate] into + * an [Int]. + * + * [FALSE] represents `0`, + * [TRUE] represents `1` and + * [UNSET] represents `2`. + * + * @return [Int] representation of this [Tristate] or `null` if [UNSET] + * @since v1-alpha10 + */ + fun toInt(): Int { + return when (this) { + FALSE -> 0 + TRUE -> 1 + UNSET -> 2 + } + } + + /** + * Converts this [Tristate] into + * an [UInt]. + * + * [FALSE] represents `0`, + * [TRUE] represents `1` and + * [UNSET] represents `2`. + * + * @return [UInt] representation of this [Tristate] or `null` if [UNSET] + * @since v1-alpha10 + */ + fun toUInt(): UInt { + return when (this) { + FALSE -> 0u + TRUE -> 1u + UNSET -> 2u + } + } + + /** + * Converts this [Tristate] into + * a [Long]. + * + * [FALSE] represents `0`, + * [TRUE] represents `1` and + * [UNSET] represents `2`. + * + * @return [Long] representation of this [Tristate] or `null` if [UNSET] + * @since v1-alpha10 + */ + fun toLong(): Long { + return when (this) { + FALSE -> 0L + TRUE -> 1L + UNSET -> 2L + } + } + + /** + * Converts this [Tristate] into + * an [ULong]. + * + * [FALSE] represents `0`, + * [TRUE] represents `1` and + * [UNSET] represents `2`. + * + * @return [ULong] representation of this [Tristate] or `null` if [UNSET] + * @since v1-alpha10 + */ + fun toULong(): ULong { + return when (this) { + FALSE -> 0UL + TRUE -> 1UL + UNSET -> 2UL + } + } +} diff --git a/base/src/main/kotlin/de/staropensource/engine/base/utility/Environment.kt b/base/src/main/kotlin/de/staropensource/engine/base/utility/Environment.kt index d718869..07acb6a 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/utility/Environment.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/utility/Environment.kt @@ -33,7 +33,7 @@ import oshi.hardware.HardwareAbstractionLayer * * @since v1-alpha10 */ -@Suppress("unused") +@Suppress("Unused") class Environment private constructor() { /** * Companion class of [Environment]. diff --git a/base/src/main/kotlin/de/staropensource/engine/base/utility/FileAccess.kt b/base/src/main/kotlin/de/staropensource/engine/base/utility/FileAccess.kt index fb17eb1..61c3df0 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/utility/FileAccess.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/utility/FileAccess.kt @@ -42,7 +42,7 @@ import java.nio.file.attribute.PosixFilePermissions * * @since v1-alpha10 */ -@Suppress("unused") +@Suppress("Unused") class FileAccess { /** * Companion object of [FileAccess]. diff --git a/base/src/main/kotlin/de/staropensource/engine/base/utility/dnihbd/BuildInformation.kt b/base/src/main/kotlin/de/staropensource/engine/base/utility/dnihbd/BuildInformation.kt index 23da71d..a15572f 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/utility/dnihbd/BuildInformation.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/utility/dnihbd/BuildInformation.kt @@ -58,7 +58,7 @@ import java.util.* // this: properties & getter methods, constructors, // methods. This change was made for a better, // less chaotic class source code layout. -@Suppress("unused") +@Suppress("Unused") open class BuildInformation @Throws(RuntimeException::class) constructor(val loadPrefix: String, val loadLocation: FileAccess? = null) { diff --git a/settings.gradle.kts b/settings.gradle.kts index 819f6a5..d892cb8 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -22,7 +22,12 @@ rootProject.name = "sos!engine" // Subprojects -include("") // scan root +// -> Root +include("") + +// -> Base include("base") + +// -> Testing include("testapp") include("testing")