From 46ba81a45f7cb10c7f870bbf6946a46207ee353c Mon Sep 17 00:00:00 2001 From: Alessio Leonardo Tomei <122273875+T0mexX@users.noreply.github.com> Date: Tue, 18 Mar 2025 10:31:21 +0100 Subject: Unit System v2 (#243) * Separated `Time` unit into `TimeDelta` and `TimeStamp` + small fixes Addition and subtruction between `Timestamp`s is not allowed, but any other `Unit` operation/comparison is. `TimeDelta`s can be added/subtructed to/form `Timestamp`s. Deserialization of `Timestamp`: - `Number` -> interpreted as millis from Epoch - `Instant` (string representation) -> converted to Timestamp - `Duration` (string representation) -> interpreted as duration since Epoch (warn msg is logged) Deserialization of `TimeDelta` is the same as `Time` was before, with the diference that when an `Instant` is converted to an timedelta since Epoch a warning message is logged. * Unit System v2 - Merged `BoundedPercentage` and `UnboundedPercentage` - Overrided all operation defined in `Unit` in all subclasses to avoid as much as possible value classes being boxed in bytecode. If units are used as generics (hence also functions defined in Unit) they are boxed (as double would if used as generic). - All units companions now subclass `UnitId`, and can be used as keys (e.g `Map`), while offering `max` `min` and `zero` methods. - Division between the same unit now returns `Percentage` - Added `Iterable.averageOfUnitOrNull(selector (T) -> )` - `ifNeg0ThenPos0()` now is optional and not invoked on every constructor - Now methods in `Unit` are all abstract, forcing override and avoid boxing in some cases - Added `@UnintendedOperation` and `UnitOperationException` for methods that must be defined but are not intended for use (e.g. `Timestamp` + `Timestamp`) --- .../src/main/kotlin/org/opendc/common/units/UnitSerializer.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'opendc-common/src/main/kotlin/org/opendc/common/units/UnitSerializer.kt') diff --git a/opendc-common/src/main/kotlin/org/opendc/common/units/UnitSerializer.kt b/opendc-common/src/main/kotlin/org/opendc/common/units/UnitSerializer.kt index aaf18498..45d2b3a4 100644 --- a/opendc-common/src/main/kotlin/org/opendc/common/units/UnitSerializer.kt +++ b/opendc-common/src/main/kotlin/org/opendc/common/units/UnitSerializer.kt @@ -20,6 +20,8 @@ * SOFTWARE. */ +@file:OptIn(NonInlinableUnit::class) + package org.opendc.common.units import kotlinx.serialization.KSerializer @@ -32,7 +34,7 @@ import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.JsonTransformingSerializer -import mu.KotlinLogging +import org.opendc.common.logger.logger /** * Serializer for [T]. @@ -84,8 +86,7 @@ internal open class UnitSerializer>( }, ) { companion object { - // TODO: replace with `by logger()` if pr #241 is approved - val LOG = KotlinLogging.logger(name = this::class.java.enclosingClass.simpleName) + val LOG by logger() val json = Json -- cgit v1.2.3