update dependencies and add kdocs for percentage

This commit is contained in:
InsanusMokrassar 2024-12-21 09:02:31 +06:00
parent e8273ab80c
commit 8718c5e310
3 changed files with 51 additions and 10 deletions

View File

@ -2,6 +2,11 @@
## 0.24.0 ## 0.24.0
* `Versions`:
* `Coroutines`: `1.9.0` -> `1.10.1`
* `KSLog`: `1.3.6` -> `1.4.0`
* `Compose`: `1.7.1` -> `1.7.3`
* `Ktor`: `3.0.2` -> `3.0.3`
* `Common`: * `Common`:
* Rename `Progress` to more common `Percentage`. `Progress` now is typealias * Rename `Progress` to more common `Percentage`. `Progress` now is typealias
* Fix of `Progress.compareTo` extension * Fix of `Progress.compareTo` extension

View File

@ -3,26 +3,45 @@ package dev.inmo.micro_utils.common
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlin.jvm.JvmInline import kotlin.jvm.JvmInline
/**
* Contains [of1] as main value, where 100% of percentage is when of1 == 1
*
* @see invoke
* @see partOfTotal
* @see of100
*/
@Serializable @Serializable
@JvmInline @JvmInline
value class Percentage private constructor( value class Percentage private constructor(
/**
* Value of percentage. When it equals to 1, means 100%
*/
val of1: Double val of1: Double
) { ) {
/**
* Same as [of1], but float (using [Double.toFloat])
*/
val of1Float val of1Float
get() = of1.toFloat() get() = of1.toFloat()
/**
* Represent this percentage as common percentage where 100% is 100%
*/
val of100 val of100
get() = of1 * 100 get() = of1 * 100
/**
* Same as [of100], but float (using [Double.toFloat])
*/
val of100Float val of100Float
get() = of100.toFloat() get() = of100.toFloat()
/**
* Same as [of100], but int (using [Double.toInt])
*/
val of100Int val of100Int
get() = of100.toInt() get() = of100.toInt()
init {
require(of1 in rangeOfValues) {
"Progress main value should be in $rangeOfValues, but incoming value is $of1"
}
}
companion object { companion object {
val rangeOfValues = 0.0 .. 1.0 val rangeOfValues = 0.0 .. 1.0
@ -33,8 +52,25 @@ value class Percentage private constructor(
operator fun invoke(part: Number, total: Number) = Percentage( operator fun invoke(part: Number, total: Number) = Percentage(
part.toDouble() / total.toDouble() part.toDouble() / total.toDouble()
) )
fun of1(of1: Double) = Percentage(of1 = of1)
fun of100(of100: Double) = Percentage(of1 = of100 / 100) fun of100(of100: Double) = Percentage(of1 = of100 / 100)
fun partOfTotal(part: Number, total: Number) = Percentage(part = part, total = total)
} }
} }
typealias Progress = Percentage typealias Progress = Percentage
/**
* Will return [this] [Progress] if [Percentage.of1] in `0 .. 1` range
*/
fun Progress.ensureStrictOrNull(): Progress? = if (of1 in Percentage.rangeOfValues) this else null
/**
* Will return [this] [Progress] if [Percentage.of1] in `0 .. 1` range. Otherwise, will throw error
* [IllegalArgumentException] due to [require] failure
*/
fun Progress.ensureStrictOrThrow(): Progress {
require(of1 in Percentage.rangeOfValues) {
"For strict checks value of percentage must be in ${Percentage.rangeOfValues}, but actual value is $of1"
}
return this
}

View File

@ -2,11 +2,11 @@
kt = "2.1.0" kt = "2.1.0"
kt-serialization = "1.7.3" kt-serialization = "1.7.3"
kt-coroutines = "1.9.0" kt-coroutines = "1.10.1"
kslog = "1.3.6" kslog = "1.4.0"
jb-compose = "1.7.1" jb-compose = "1.7.3"
jb-exposed = "0.57.0" jb-exposed = "0.57.0"
jb-dokka = "1.9.20" jb-dokka = "1.9.20"
@ -15,7 +15,7 @@ sqlite = "3.47.1.0"
korlibs = "5.4.0" korlibs = "5.4.0"
uuid = "0.8.4" uuid = "0.8.4"
ktor = "3.0.2" ktor = "3.0.3"
gh-release = "2.5.2" gh-release = "2.5.2"