From 9deac2a5913e3b2427bff115e2d7e749566dbb6c Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sat, 25 May 2019 14:57:14 +0300 Subject: [PATCH] Rename withComponents to toComponents and add two overloads of them --- libraries/stdlib/src/kotlin/time/Duration.kt | 18 ++++++++++-------- libraries/stdlib/test/time/DurationTest.kt | 17 +++++++++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/libraries/stdlib/src/kotlin/time/Duration.kt b/libraries/stdlib/src/kotlin/time/Duration.kt index 9ca6a94d3bb..ece0feb918e 100644 --- a/libraries/stdlib/src/kotlin/time/Duration.kt +++ b/libraries/stdlib/src/kotlin/time/Duration.kt @@ -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 withComponents(action: (hours: Int, minutes: Int, seconds: Int, nanoseconds: Int) -> T): T = - action(inHours.toInt(), minutesComponent, secondsComponent, nanosecondsComponent) - - inline fun withComponents(action: (days: Int, hours: Int, minutes: Int, seconds: Int, nanoseconds: Int) -> T): T = + inline fun toComponents(action: (days: Int, hours: Int, minutes: Int, seconds: Int, nanoseconds: Int) -> T): T = action(inDays.toInt(), hoursComponent, minutesComponent, secondsComponent, nanosecondsComponent) + inline fun toComponents(action: (hours: Int, minutes: Int, seconds: Int, nanoseconds: Int) -> T): T = + action(inHours.toInt(), minutesComponent, secondsComponent, nanosecondsComponent) + + inline fun toComponents(action: (minutes: Int, seconds: Int, nanoseconds: Int) -> T): T = + action(inMinutes.toInt(), secondsComponent, nanosecondsComponent) + + inline fun 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') diff --git a/libraries/stdlib/test/time/DurationTest.kt b/libraries/stdlib/test/time/DurationTest.kt index 95eb9e7b4b0..c3c1bb9158f 100644 --- a/libraries/stdlib/test/time/DurationTest.kt +++ b/libraries/stdlib/test/time/DurationTest.kt @@ -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)