Progress -> Percentage

This commit is contained in:
InsanusMokrassar 2024-12-08 22:34:28 +06:00
parent 2718605987
commit e8273ab80c
5 changed files with 121 additions and 85 deletions

View File

@ -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`:

View File

@ -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

View File

@ -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))

View File

@ -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()

View File

@ -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
}
}
}