update dependencies and add kdocs for percentage

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

View File

@@ -3,26 +3,45 @@ package dev.inmo.micro_utils.common
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmInline
/**
* Contains [of1] as main value, where 100% of percentage is when of1 == 1
*
* @see invoke
* @see partOfTotal
* @see of100
*/
@Serializable
@JvmInline
value class Percentage private constructor(
/**
* Value of percentage. When it equals to 1, means 100%
*/
val of1: Double
) {
/**
* Same as [of1], but float (using [Double.toFloat])
*/
val of1Float
get() = of1.toFloat()
/**
* Represent this percentage as common percentage where 100% is 100%
*/
val of100
get() = of1 * 100
/**
* Same as [of100], but float (using [Double.toFloat])
*/
val of100Float
get() = of100.toFloat()
/**
* Same as [of100], but int (using [Double.toInt])
*/
val of100Int
get() = of100.toInt()
init {
require(of1 in rangeOfValues) {
"Progress main value should be in $rangeOfValues, but incoming value is $of1"
}
}
companion object {
val rangeOfValues = 0.0 .. 1.0
@@ -33,8 +52,25 @@ value class Percentage private constructor(
operator fun invoke(part: Number, total: Number) = Percentage(
part.toDouble() / total.toDouble()
)
fun of1(of1: Double) = Percentage(of1 = of1)
fun of100(of100: Double) = Percentage(of1 = of100 / 100)
fun partOfTotal(part: Number, total: Number) = Percentage(part = part, total = total)
}
}
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
}