Rename Duration storage property to 'value'
This commit is contained in:
@@ -24,7 +24,7 @@ private val storageUnit = DurationUnit.NANOSECONDS
|
|||||||
* or the properties [inHours], [inMinutes], [inSeconds], [inNanoseconds] and so on.
|
* or the properties [inHours], [inMinutes], [inSeconds], [inNanoseconds] and so on.
|
||||||
*/
|
*/
|
||||||
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
|
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
|
||||||
public inline class Duration internal constructor(internal val _value: Double) : Comparable<Duration> {
|
public inline class Duration internal constructor(internal val value: Double) : Comparable<Duration> {
|
||||||
// TODO: backend fails on init block, wait for KT-28055
|
// TODO: backend fails on init block, wait for KT-28055
|
||||||
|
|
||||||
// init {
|
// init {
|
||||||
@@ -38,28 +38,28 @@ public inline class Duration internal constructor(internal val _value: Double) :
|
|||||||
|
|
||||||
// arithmetic operators
|
// arithmetic operators
|
||||||
|
|
||||||
operator fun unaryMinus(): Duration = Duration(-_value)
|
operator fun unaryMinus(): Duration = Duration(-value)
|
||||||
operator fun plus(other: Duration): Duration = Duration(_value + other._value)
|
operator fun plus(other: Duration): Duration = Duration(value + other.value)
|
||||||
operator fun minus(other: Duration): Duration = Duration(_value - other._value)
|
operator fun minus(other: Duration): Duration = Duration(value - other.value)
|
||||||
|
|
||||||
// should we declare symmetric extension operators?
|
// should we declare symmetric extension operators?
|
||||||
|
|
||||||
operator fun times(scale: Int): Duration = Duration(_value * scale)
|
operator fun times(scale: Int): Duration = Duration(value * scale)
|
||||||
operator fun times(scale: Double): Duration = Duration(_value * scale)
|
operator fun times(scale: Double): Duration = Duration(value * scale)
|
||||||
|
|
||||||
operator fun div(scale: Int): Duration = Duration(_value / scale)
|
operator fun div(scale: Int): Duration = Duration(value / scale)
|
||||||
operator fun div(scale: Double): Duration = Duration(_value / scale)
|
operator fun div(scale: Double): Duration = Duration(value / scale)
|
||||||
|
|
||||||
operator fun div(other: Duration): Double = this._value / other._value
|
operator fun div(other: Duration): Double = this.value / other.value
|
||||||
|
|
||||||
fun isNegative(): Boolean = _value < 0
|
fun isNegative(): Boolean = value < 0
|
||||||
fun isInfinite(): Boolean = _value.isInfinite()
|
fun isInfinite(): Boolean = value.isInfinite()
|
||||||
fun isFinite(): Boolean = _value.isFinite()
|
fun isFinite(): Boolean = value.isFinite()
|
||||||
|
|
||||||
val absoluteValue: Duration get() = if (isNegative()) -this else this
|
val absoluteValue: Duration get() = if (isNegative()) -this else this
|
||||||
|
|
||||||
|
|
||||||
override fun compareTo(other: Duration): Int = this._value.compareTo(other._value)
|
override fun compareTo(other: Duration): Int = this.value.compareTo(other.value)
|
||||||
|
|
||||||
|
|
||||||
// splitting to components
|
// splitting to components
|
||||||
@@ -88,7 +88,7 @@ public inline class Duration internal constructor(internal val _value: Double) :
|
|||||||
|
|
||||||
// conversion to units
|
// conversion to units
|
||||||
|
|
||||||
fun toDouble(unit: DurationUnit): Double = convertDurationUnit(_value, storageUnit, unit)
|
fun toDouble(unit: DurationUnit): Double = convertDurationUnit(value, storageUnit, unit)
|
||||||
fun toLong(unit: DurationUnit): Long = toDouble(unit).toLong()
|
fun toLong(unit: DurationUnit): Long = toDouble(unit).toLong()
|
||||||
fun toInt(unit: DurationUnit): Int = toDouble(unit).toInt()
|
fun toInt(unit: DurationUnit): Int = toDouble(unit).toInt()
|
||||||
|
|
||||||
@@ -109,7 +109,7 @@ public inline class Duration internal constructor(internal val _value: Double) :
|
|||||||
|
|
||||||
override fun toString(): String = buildString {
|
override fun toString(): String = buildString {
|
||||||
if (isInfinite()) {
|
if (isInfinite()) {
|
||||||
append(_value)
|
append(value)
|
||||||
} else {
|
} else {
|
||||||
val absNs = absoluteValue.inNanoseconds
|
val absNs = absoluteValue.inNanoseconds
|
||||||
var scientific = false
|
var scientific = false
|
||||||
@@ -145,7 +145,7 @@ public inline class Duration internal constructor(internal val _value: Double) :
|
|||||||
|
|
||||||
fun toString(unit: DurationUnit, decimals: Int = 0): String {
|
fun toString(unit: DurationUnit, decimals: Int = 0): String {
|
||||||
require(decimals >= 0) { "decimals must be not negative, but was $decimals" }
|
require(decimals >= 0) { "decimals must be not negative, but was $decimals" }
|
||||||
if (isInfinite()) return _value.toString()
|
if (isInfinite()) return value.toString()
|
||||||
return formatToExactDecimals(toDouble(unit), decimals) + unit.shortName()
|
return formatToExactDecimals(toDouble(unit), decimals) + unit.shortName()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ class DurationTest {
|
|||||||
val value = Random.nextInt(1_000_000)
|
val value = Random.nextInt(1_000_000)
|
||||||
val unit = units.random()
|
val unit = units.random()
|
||||||
val expected = convertDurationUnit(value.toDouble(), unit, expectStorageUnit)
|
val expected = convertDurationUnit(value.toDouble(), unit, expectStorageUnit)
|
||||||
assertEquals(expected, value.toDuration(unit)._value)
|
assertEquals(expected, value.toDuration(unit).value)
|
||||||
assertEquals(expected, value.toLong().toDuration(unit)._value)
|
assertEquals(expected, value.toLong().toDuration(unit).value)
|
||||||
assertEquals(expected, value.toDouble().toDuration(unit)._value)
|
assertEquals(expected, value.toDouble().toDuration(unit).value)
|
||||||
}
|
}
|
||||||
|
|
||||||
todo {
|
todo {
|
||||||
|
|||||||
Reference in New Issue
Block a user