From 0cf0081ef65ab93bdfcbbf7bff343475a4f40324 Mon Sep 17 00:00:00 2001 From: Arseniy Svechkarev <30600345+arsvechkarev@users.noreply.github.com> Date: Tue, 25 May 2021 10:49:16 +0300 Subject: [PATCH] Fix issue #168: Change inline classes to value classes (#171) --- klock/src/commonMain/kotlin/com/soywiz/klock/Date.kt | 6 ++++-- klock/src/commonMain/kotlin/com/soywiz/klock/DateTime.kt | 4 +++- klock/src/commonMain/kotlin/com/soywiz/klock/Frequency.kt | 4 +++- klock/src/commonMain/kotlin/com/soywiz/klock/MonthSpan.kt | 4 +++- .../src/commonMain/kotlin/com/soywiz/klock/NumberOfTimes.kt | 5 ++++- klock/src/commonMain/kotlin/com/soywiz/klock/Time.kt | 4 +++- klock/src/commonMain/kotlin/com/soywiz/klock/TimeSpan.kt | 6 ++++-- .../commonMain/kotlin/com/soywiz/klock/TimezoneOffset.kt | 4 +++- klock/src/commonMain/kotlin/com/soywiz/klock/Year.kt | 4 +++- klock/src/commonMain/kotlin/com/soywiz/klock/YearMonth.kt | 6 ++++-- .../src/commonMain/kotlin/com/soywiz/klock/hr/HRTimeSpan.kt | 4 +++- .../kotlin/com/soywiz/klock/internal/GenericBinarySearch.kt | 5 ++++- .../src/commonMain/kotlin/com/soywiz/klock/wrapped/WDate.kt | 4 ++-- .../commonMain/kotlin/com/soywiz/klock/wrapped/WDateTime.kt | 2 +- .../kotlin/com/soywiz/klock/wrapped/WMonthSpan.kt | 2 +- .../src/commonMain/kotlin/com/soywiz/klock/wrapped/WTime.kt | 2 +- .../commonMain/kotlin/com/soywiz/klock/wrapped/WTimeSpan.kt | 4 ++-- .../kotlin/com/soywiz/klock/wrapped/WTimezoneOffset.kt | 2 +- .../src/commonMain/kotlin/com/soywiz/klock/wrapped/WYear.kt | 2 +- .../kotlin/com/soywiz/klock/wrapped/WYearMonth.kt | 2 +- 20 files changed, 51 insertions(+), 25 deletions(-) diff --git a/klock/src/commonMain/kotlin/com/soywiz/klock/Date.kt b/klock/src/commonMain/kotlin/com/soywiz/klock/Date.kt index 52b87db8..66f29c50 100644 --- a/klock/src/commonMain/kotlin/com/soywiz/klock/Date.kt +++ b/klock/src/commonMain/kotlin/com/soywiz/klock/Date.kt @@ -1,14 +1,16 @@ package com.soywiz.klock import com.soywiz.klock.internal.Serializable +import kotlin.jvm.JvmInline import kotlin.math.abs /** * Represents a triple of [year], [month] and [day]. * - * It is packed in an inline class wrapping an Int to prevent allocations. + * It is packed in a value class wrapping an Int to prevent allocations. */ -inline class Date(val encoded: Int) : Comparable, Serializable { +@JvmInline +value class Date(val encoded: Int) : Comparable, Serializable { companion object { @Suppress("MayBeConstant", "unused") private const val serialVersionUID = 1L diff --git a/klock/src/commonMain/kotlin/com/soywiz/klock/DateTime.kt b/klock/src/commonMain/kotlin/com/soywiz/klock/DateTime.kt index 888b39c0..3774ec18 100644 --- a/klock/src/commonMain/kotlin/com/soywiz/klock/DateTime.kt +++ b/klock/src/commonMain/kotlin/com/soywiz/klock/DateTime.kt @@ -1,6 +1,7 @@ package com.soywiz.klock import com.soywiz.klock.internal.* +import kotlin.jvm.JvmInline import kotlin.math.* /** @@ -11,7 +12,8 @@ import kotlin.math.* * - Thu Aug 10 -140744 07:15:45 GMT-0014 (Central European Summer Time) * - Wed May 23 144683 18:29:30 GMT+0200 (Central European Summer Time) */ -inline class DateTime( +@JvmInline +value class DateTime( /** Number of milliseconds since UNIX [EPOCH] */ val unixMillis: Double ) : Comparable, Serializable { diff --git a/klock/src/commonMain/kotlin/com/soywiz/klock/Frequency.kt b/klock/src/commonMain/kotlin/com/soywiz/klock/Frequency.kt index 2c3d97bd..5719cf61 100644 --- a/klock/src/commonMain/kotlin/com/soywiz/klock/Frequency.kt +++ b/klock/src/commonMain/kotlin/com/soywiz/klock/Frequency.kt @@ -1,6 +1,7 @@ package com.soywiz.klock import com.soywiz.klock.hr.hr +import kotlin.jvm.JvmInline val TimeSpan.hz get() = timesPerSecond val Int.hz get() = timesPerSecond @@ -12,7 +13,8 @@ val TimeSpan.timesPerSecond get() = Frequency(1.0 / this.seconds) val Int.timesPerSecond get() = Frequency(this.toDouble()) val Double.timesPerSecond get() = Frequency(this) -inline class Frequency(val hertz: Double) { +@JvmInline +value class Frequency(val hertz: Double) { companion object { fun from(timeSpan: TimeSpan) = timeSpan.toFrequency() } diff --git a/klock/src/commonMain/kotlin/com/soywiz/klock/MonthSpan.kt b/klock/src/commonMain/kotlin/com/soywiz/klock/MonthSpan.kt index 20f16c15..231e8eb2 100644 --- a/klock/src/commonMain/kotlin/com/soywiz/klock/MonthSpan.kt +++ b/klock/src/commonMain/kotlin/com/soywiz/klock/MonthSpan.kt @@ -1,6 +1,7 @@ package com.soywiz.klock import com.soywiz.klock.internal.Serializable +import kotlin.jvm.JvmInline /** * Creates a [MonthSpan] representing these years. @@ -15,7 +16,8 @@ inline val Int.months get() = MonthSpan(this) /** * Represents a number of years and months temporal distance. */ -inline class MonthSpan( +@JvmInline +value class MonthSpan( /** Total months of this [MonthSpan] as integer */ val totalMonths: Int ) : Comparable, Serializable { diff --git a/klock/src/commonMain/kotlin/com/soywiz/klock/NumberOfTimes.kt b/klock/src/commonMain/kotlin/com/soywiz/klock/NumberOfTimes.kt index d27cb21f..98c6ad30 100644 --- a/klock/src/commonMain/kotlin/com/soywiz/klock/NumberOfTimes.kt +++ b/klock/src/commonMain/kotlin/com/soywiz/klock/NumberOfTimes.kt @@ -1,9 +1,12 @@ package com.soywiz.klock +import kotlin.jvm.JvmInline + val infiniteTimes get() = NumberOfTimes.INFINITE inline val Int.times get() = NumberOfTimes(this) -inline class NumberOfTimes(val count: Int) { +@JvmInline +value class NumberOfTimes(val count: Int) { companion object { val ZERO = NumberOfTimes(0) val ONE = NumberOfTimes(1) diff --git a/klock/src/commonMain/kotlin/com/soywiz/klock/Time.kt b/klock/src/commonMain/kotlin/com/soywiz/klock/Time.kt index d8e7d240..400fff41 100644 --- a/klock/src/commonMain/kotlin/com/soywiz/klock/Time.kt +++ b/klock/src/commonMain/kotlin/com/soywiz/klock/Time.kt @@ -1,12 +1,14 @@ package com.soywiz.klock import com.soywiz.klock.internal.Serializable +import kotlin.jvm.JvmInline import kotlin.math.abs /** * Represents a union of [millisecond], [second], [minute] and [hour]. */ -inline class Time(val encoded: TimeSpan) : Comparable