From e8273ab80cff49f43206554ce7aa4cf1bc14114b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 8 Dec 2024 22:34:28 +0600 Subject: [PATCH] Progress -> Percentage --- CHANGELOG.md | 4 + .../common/{Progress.kt => Percentage.kt} | 13 +-- .../common/PercentageOperations.kt | 80 +++++++++++++++++++ .../micro_utils/common/ProgressOperations.kt | 80 ------------------- .../micro_utils/common/PercentageTests.kt | 29 +++++++ 5 files changed, 121 insertions(+), 85 deletions(-) rename common/src/commonMain/kotlin/dev/inmo/micro_utils/common/{Progress.kt => Percentage.kt} (60%) create mode 100644 common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PercentageOperations.kt delete mode 100644 common/src/commonMain/kotlin/dev/inmo/micro_utils/common/ProgressOperations.kt create mode 100644 common/src/commonTest/kotlin/dev/inmo/micro_utils/common/PercentageTests.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index cbdc514e88e..838d5fa1a6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.24.0 +* `Common`: + * Rename `Progress` to more common `Percentage`. `Progress` now is typealias + * Fix of `Progress.compareTo` extension + ## 0.23.2 * `Versions`: diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Progress.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Percentage.kt similarity index 60% rename from common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Progress.kt rename to common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Percentage.kt index dbce92508f0..607ac9b2b33 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Progress.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Percentage.kt @@ -5,7 +5,7 @@ import kotlin.jvm.JvmInline @Serializable @JvmInline -value class Progress private constructor( +value class Percentage private constructor( val of1: Double ) { val of1Float @@ -26,12 +26,15 @@ value class Progress private constructor( companion object { val rangeOfValues = 0.0 .. 1.0 - val START = Progress(rangeOfValues.start) - val COMPLETED = Progress(rangeOfValues.endInclusive) + val START = Percentage(rangeOfValues.start) + val COMPLETED = Percentage(rangeOfValues.endInclusive) - operator fun invoke(of1: Double) = Progress(of1.coerceIn(rangeOfValues)) - operator fun invoke(part: Number, total: Number) = Progress( + operator fun invoke(of1: Double) = Percentage(of1.coerceIn(rangeOfValues)) + operator fun invoke(part: Number, total: Number) = Percentage( part.toDouble() / total.toDouble() ) + fun of100(of100: Double) = Percentage(of1 = of100 / 100) } } + +typealias Progress = Percentage diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PercentageOperations.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PercentageOperations.kt new file mode 100644 index 00000000000..f12051aa371 --- /dev/null +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PercentageOperations.kt @@ -0,0 +1,80 @@ +@file:Suppress( + "RemoveRedundantCallsOfConversionMethods", + "RedundantVisibilityModifier", +) + +package dev.inmo.micro_utils.common + +import kotlin.Byte +import kotlin.Double +import kotlin.Float +import kotlin.Int +import kotlin.Long +import kotlin.Short +import kotlin.Suppress + +public operator fun Percentage.plus(other: Percentage): Percentage = Percentage(of1 + other.of1) + +public operator fun Percentage.minus(other: Percentage): Percentage = Percentage(of1 - other.of1) + +public operator fun Percentage.plus(i: Byte): Percentage = Percentage((of1 + i).toDouble()) + +public operator fun Percentage.minus(i: Byte): Percentage = Percentage((of1 - i).toDouble()) + +public operator fun Percentage.times(i: Byte): Percentage = Percentage((of1 * i).toDouble()) + +public operator fun Percentage.div(i: Byte): Percentage = Percentage((of1 / i).toDouble()) + +public operator fun Percentage.rem(i: Byte): Percentage = Percentage((of1 % i).toDouble()) + +public operator fun Percentage.plus(i: Short): Percentage = Percentage((of1 + i).toDouble()) + +public operator fun Percentage.minus(i: Short): Percentage = Percentage((of1 - i).toDouble()) + +public operator fun Percentage.times(i: Short): Percentage = Percentage((of1 * i).toDouble()) + +public operator fun Percentage.div(i: Short): Percentage = Percentage((of1 / i).toDouble()) + +public operator fun Percentage.rem(i: Short): Percentage = Percentage((of1 % i).toDouble()) + +public operator fun Percentage.plus(i: Int): Percentage = Percentage((of1 + i).toDouble()) + +public operator fun Percentage.minus(i: Int): Percentage = Percentage((of1 - i).toDouble()) + +public operator fun Percentage.times(i: Int): Percentage = Percentage((of1 * i).toDouble()) + +public operator fun Percentage.div(i: Int): Percentage = Percentage((of1 / i).toDouble()) + +public operator fun Percentage.rem(i: Int): Percentage = Percentage((of1 % i).toDouble()) + +public operator fun Percentage.plus(i: Long): Percentage = Percentage((of1 + i).toDouble()) + +public operator fun Percentage.minus(i: Long): Percentage = Percentage((of1 - i).toDouble()) + +public operator fun Percentage.times(i: Long): Percentage = Percentage((of1 * i).toDouble()) + +public operator fun Percentage.div(i: Long): Percentage = Percentage((of1 / i).toDouble()) + +public operator fun Percentage.rem(i: Long): Percentage = Percentage((of1 % i).toDouble()) + +public operator fun Percentage.plus(i: Float): Percentage = Percentage((of1 + i).toDouble()) + +public operator fun Percentage.minus(i: Float): Percentage = Percentage((of1 - i).toDouble()) + +public operator fun Percentage.times(i: Float): Percentage = Percentage((of1 * i).toDouble()) + +public operator fun Percentage.div(i: Float): Percentage = Percentage((of1 / i).toDouble()) + +public operator fun Percentage.rem(i: Float): Percentage = Percentage((of1 % i).toDouble()) + +public operator fun Percentage.plus(i: Double): Percentage = Percentage((of1 + i).toDouble()) + +public operator fun Percentage.minus(i: Double): Percentage = Percentage((of1 - i).toDouble()) + +public operator fun Percentage.times(i: Double): Percentage = Percentage((of1 * i).toDouble()) + +public operator fun Percentage.div(i: Double): Percentage = Percentage((of1 / i).toDouble()) + +public operator fun Percentage.rem(i: Double): Percentage = Percentage((of1 % i).toDouble()) + +public operator fun Percentage.compareTo(other: Percentage): Int = (of1.compareTo(other.of1)) diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/ProgressOperations.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/ProgressOperations.kt deleted file mode 100644 index 0d71c3325cc..00000000000 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/ProgressOperations.kt +++ /dev/null @@ -1,80 +0,0 @@ -@file:Suppress( - "RemoveRedundantCallsOfConversionMethods", - "RedundantVisibilityModifier", -) - -package dev.inmo.micro_utils.common - -import kotlin.Byte -import kotlin.Double -import kotlin.Float -import kotlin.Int -import kotlin.Long -import kotlin.Short -import kotlin.Suppress - -public operator fun Progress.plus(other: Progress): Progress = Progress(of1 + other.of1) - -public operator fun Progress.minus(other: Progress): Progress = Progress(of1 - other.of1) - -public operator fun Progress.plus(i: Byte): Progress = Progress((of1 + i).toDouble()) - -public operator fun Progress.minus(i: Byte): Progress = Progress((of1 - i).toDouble()) - -public operator fun Progress.times(i: Byte): Progress = Progress((of1 * i).toDouble()) - -public operator fun Progress.div(i: Byte): Progress = Progress((of1 / i).toDouble()) - -public operator fun Progress.rem(i: Byte): Progress = Progress((of1 % i).toDouble()) - -public operator fun Progress.plus(i: Short): Progress = Progress((of1 + i).toDouble()) - -public operator fun Progress.minus(i: Short): Progress = Progress((of1 - i).toDouble()) - -public operator fun Progress.times(i: Short): Progress = Progress((of1 * i).toDouble()) - -public operator fun Progress.div(i: Short): Progress = Progress((of1 / i).toDouble()) - -public operator fun Progress.rem(i: Short): Progress = Progress((of1 % i).toDouble()) - -public operator fun Progress.plus(i: Int): Progress = Progress((of1 + i).toDouble()) - -public operator fun Progress.minus(i: Int): Progress = Progress((of1 - i).toDouble()) - -public operator fun Progress.times(i: Int): Progress = Progress((of1 * i).toDouble()) - -public operator fun Progress.div(i: Int): Progress = Progress((of1 / i).toDouble()) - -public operator fun Progress.rem(i: Int): Progress = Progress((of1 % i).toDouble()) - -public operator fun Progress.plus(i: Long): Progress = Progress((of1 + i).toDouble()) - -public operator fun Progress.minus(i: Long): Progress = Progress((of1 - i).toDouble()) - -public operator fun Progress.times(i: Long): Progress = Progress((of1 * i).toDouble()) - -public operator fun Progress.div(i: Long): Progress = Progress((of1 / i).toDouble()) - -public operator fun Progress.rem(i: Long): Progress = Progress((of1 % i).toDouble()) - -public operator fun Progress.plus(i: Float): Progress = Progress((of1 + i).toDouble()) - -public operator fun Progress.minus(i: Float): Progress = Progress((of1 - i).toDouble()) - -public operator fun Progress.times(i: Float): Progress = Progress((of1 * i).toDouble()) - -public operator fun Progress.div(i: Float): Progress = Progress((of1 / i).toDouble()) - -public operator fun Progress.rem(i: Float): Progress = Progress((of1 % i).toDouble()) - -public operator fun Progress.plus(i: Double): Progress = Progress((of1 + i).toDouble()) - -public operator fun Progress.minus(i: Double): Progress = Progress((of1 - i).toDouble()) - -public operator fun Progress.times(i: Double): Progress = Progress((of1 * i).toDouble()) - -public operator fun Progress.div(i: Double): Progress = Progress((of1 / i).toDouble()) - -public operator fun Progress.rem(i: Double): Progress = Progress((of1 % i).toDouble()) - -public operator fun Progress.compareTo(other: Progress): Int = (of1 - other.of1).toInt() diff --git a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/PercentageTests.kt b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/PercentageTests.kt new file mode 100644 index 00000000000..f3ec98ebb89 --- /dev/null +++ b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/PercentageTests.kt @@ -0,0 +1,29 @@ +package dev.inmo.micro_utils.common + +import kotlin.test.Test +import kotlin.test.assertEquals + +class PercentageTests { + @Test + fun testCompareTo() { + val step = 0.01 + + var i = Percentage.START.of1 + while (i <= Percentage.COMPLETED.of1) { + val percentageI = Percentage(i) + + var j = Percentage.START.of1 + while (j <= Percentage.COMPLETED.of1) { + val percentageJ = Percentage(j) + + assertEquals(percentageI.of1.compareTo(percentageJ.of1), percentageI.compareTo(percentageJ)) + assertEquals(percentageI.of1 > percentageJ.of1, percentageI > percentageJ) + assertEquals(percentageI.of1 < percentageJ.of1, percentageI < percentageJ) + + j += step + } + + i += step + } + } +} \ No newline at end of file