Rename withComponents to toComponents and add two overloads of them
This commit is contained in:
@@ -64,15 +64,17 @@ public inline class Duration internal constructor(internal val _value: Double) :
|
||||
|
||||
// splitting to components
|
||||
|
||||
// problem: withComponents can be confused with 'wither' function
|
||||
// perhaps better name would be 'letComponents'
|
||||
|
||||
inline fun <T> withComponents(action: (hours: Int, minutes: Int, seconds: Int, nanoseconds: Int) -> T): T =
|
||||
action(inHours.toInt(), minutesComponent, secondsComponent, nanosecondsComponent)
|
||||
|
||||
inline fun <T> withComponents(action: (days: Int, hours: Int, minutes: Int, seconds: Int, nanoseconds: Int) -> T): T =
|
||||
inline fun <T> toComponents(action: (days: Int, hours: Int, minutes: Int, seconds: Int, nanoseconds: Int) -> T): T =
|
||||
action(inDays.toInt(), hoursComponent, minutesComponent, secondsComponent, nanosecondsComponent)
|
||||
|
||||
inline fun <T> toComponents(action: (hours: Int, minutes: Int, seconds: Int, nanoseconds: Int) -> T): T =
|
||||
action(inHours.toInt(), minutesComponent, secondsComponent, nanosecondsComponent)
|
||||
|
||||
inline fun <T> toComponents(action: (minutes: Int, seconds: Int, nanoseconds: Int) -> T): T =
|
||||
action(inMinutes.toInt(), secondsComponent, nanosecondsComponent)
|
||||
|
||||
inline fun <T> toComponents(action: (seconds: Long, nanoseconds: Int) -> T): T =
|
||||
action(inSeconds.toLong(), nanosecondsComponent)
|
||||
|
||||
@PublishedApi
|
||||
internal val hoursComponent: Int get() = (inHours % 24).toInt()
|
||||
@@ -151,7 +153,7 @@ public inline class Duration internal constructor(internal val _value: Double) :
|
||||
fun toIsoString(): String = buildString {
|
||||
if (isNegative()) append('-')
|
||||
append('P')
|
||||
absoluteValue().withComponents { days, hours, minutes, seconds, nanoseconds ->
|
||||
absoluteValue().toComponents { days, hours, minutes, seconds, nanoseconds ->
|
||||
if (days != 0)
|
||||
append(days).append('D')
|
||||
|
||||
|
||||
@@ -136,13 +136,22 @@ class DurationTest {
|
||||
val s = Random.nextInt(60)
|
||||
val ns = Random.nextInt(1e9.toInt())
|
||||
(h.hours + m.minutes + s.seconds + ns.nanoseconds).run {
|
||||
withComponents { hours, minutes, seconds, nanoseconds ->
|
||||
toComponents { seconds, nanoseconds ->
|
||||
assertEquals(h.toLong() * 3600 + m * 60 + s, seconds)
|
||||
assertEquals(ns, nanoseconds)
|
||||
}
|
||||
toComponents { minutes, seconds, nanoseconds ->
|
||||
assertEquals(h * 60 + m, minutes)
|
||||
assertEquals(s, seconds)
|
||||
assertEquals(ns, nanoseconds)
|
||||
}
|
||||
toComponents { hours, minutes, seconds, nanoseconds ->
|
||||
assertEquals(h, hours)
|
||||
assertEquals(m, minutes)
|
||||
assertEquals(s, seconds)
|
||||
assertEquals(ns, nanoseconds, "ns component of duration ${this.toIsoString()} differs too much, expected: $ns, actual: $nanoseconds")
|
||||
assertEquals(ns, nanoseconds, "ns component of duration ${toIsoString()} differs too much, expected: $ns, actual: $nanoseconds")
|
||||
}
|
||||
withComponents { days, hours, minutes, seconds, nanoseconds ->
|
||||
toComponents { days, hours, minutes, seconds, nanoseconds ->
|
||||
assertEquals(0, days)
|
||||
assertEquals(h, hours)
|
||||
assertEquals(m, minutes)
|
||||
@@ -156,7 +165,7 @@ class DurationTest {
|
||||
@Test
|
||||
fun componentsOfCarriedSum() {
|
||||
(36.hours + 90.minutes + 90.seconds + 1500.milliseconds).run {
|
||||
withComponents { days, hours, minutes, seconds, nanoseconds ->
|
||||
toComponents { days, hours, minutes, seconds, nanoseconds ->
|
||||
assertEquals(1, days)
|
||||
assertEquals(13, hours)
|
||||
assertEquals(31, minutes)
|
||||
|
||||
Reference in New Issue
Block a user