1+ package com.omega_r.base.enitity
2+
3+ import com.omega_r.base.enitity.Percent.Companion.DEFAULT_PERCENT_MODEL
4+ import com.omega_r.base.enitity.Percent.Companion.FLOAT_PERCENT_MODEL
5+ import java.io.Serializable
6+
7+ /* *
8+ * Created by Anton Knyazev on 20.07.2023.
9+ * Copyright (c) 2023 Omega https://omega-r.com
10+ */
11+ @JvmInline
12+ value class Percent private constructor(private val value : Float ): Serializable {
13+
14+ companion object {
15+
16+ val FLOAT_PERCENT_MODEL = PercentModel (0f , 1f )
17+
18+ val DEFAULT_PERCENT_MODEL = PercentModel (0 , 100 )
19+
20+ val MAX = Percent (1.0f )
21+ val MIN = Percent (0.0f )
22+
23+ fun calc (current : Number , max : Number ): Percent = (current.toFloat() / max.toFloat()).toPercent(FLOAT_PERCENT_MODEL )
24+
25+ }
26+
27+ constructor (value: Float , model: PercentModel ) : this (value, model.min, model.max)
28+
29+
30+ constructor (value: Number , min: Number , max: Number ): this (value.toFloat(), min.toFloat(), max.toFloat())
31+
32+ constructor (value: Float , min: Float , max: Float ) : this (
33+ when {
34+ value < min -> MIN .value
35+ value > max -> MAX .value
36+ else -> (value - min) / (max - min)
37+ }
38+ )
39+
40+ fun toInt (model : PercentModel = DEFAULT_PERCENT_MODEL ): Int = toFloat(model).toInt()
41+
42+ fun toInt (min : Int , max : Int ): Int = toFloat(min.toFloat(), max.toFloat()).toInt()
43+
44+ fun toFloat (model : PercentModel = FLOAT_PERCENT_MODEL ): Float = toFloat(model.min, model.max)
45+
46+ fun toFloat (min : Float , max : Float ): Float = when {
47+ value > MAX .value -> max
48+ value < MIN .value -> min
49+ else -> value * (max - min) + min
50+ }
51+
52+ fun isMin () = value <= MIN .value
53+
54+ fun isMax () = value >= MAX .value
55+
56+ operator fun compareTo (percent : Percent ): Int {
57+ return value.compareTo(percent.value)
58+ }
59+
60+ operator fun times (number : Number ): Percent = Percent (value * number.toFloat())
61+
62+ operator fun div (number : Number ): Percent = Percent (value / number.toFloat())
63+
64+ operator fun times (percent : Percent ): Percent = Percent (value * percent.value)
65+
66+ operator fun div (percent : Percent ): Percent = Percent (value / percent.value)
67+
68+ operator fun plus (percent : Percent ): Percent = Percent (value + percent.value)
69+
70+ operator fun minus (percent : Percent ): Percent = Percent (value - percent.value)
71+
72+ override fun toString (): String = " ${value * 100 } %"
73+
74+ }
75+
76+ data class PercentModel (val min : Float , val max : Float ) {
77+ constructor (min: Number , max: Number ) : this (min.toFloat(), max.toFloat())
78+ }
79+
80+ fun Number.toPercent (model : PercentModel ) = Percent (toFloat(), model)
81+
82+ fun Double.toPercent (model : PercentModel = FLOAT_PERCENT_MODEL ) = Percent (toFloat(), model)
83+
84+ fun Int.toPercent (model : PercentModel = DEFAULT_PERCENT_MODEL ) = Percent (toFloat(), model)
85+
86+ fun Long.toPercent (model : PercentModel = DEFAULT_PERCENT_MODEL ) = Percent (toFloat(), model)
87+
88+ fun Number.toPercent (min : Number , max : Number ) = Percent (this , min, max)
89+
90+ fun Float.toPercent (model : PercentModel = FLOAT_PERCENT_MODEL ) = Percent (this , model)
91+
92+ fun Float.toPercent (min : Float , max : Float ) = Percent (this , min, max)
0 commit comments