From 9fe783180c7c88cf69c7f3a1671f829622b2c1c9 Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sat, 23 Jul 2022 11:39:25 +0100 Subject: [PATCH] Revert "Improve runtime compatibility with kotlin 1.5.31 (#7343)" This reverts commit 3ff1b617 --- .../okhttp3/internal/-CacheControlCommon.kt | 20 +++++++++++++------ .../jvmMain/kotlin/okhttp3/CacheControl.kt | 16 ++++++--------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/okhttp/src/commonMain/kotlin/okhttp3/internal/-CacheControlCommon.kt b/okhttp/src/commonMain/kotlin/okhttp3/internal/-CacheControlCommon.kt index 456b578d12c1..5462dfe8148e 100644 --- a/okhttp/src/commonMain/kotlin/okhttp3/internal/-CacheControlCommon.kt +++ b/okhttp/src/commonMain/kotlin/okhttp3/internal/-CacheControlCommon.kt @@ -15,6 +15,7 @@ */ package okhttp3.internal +import kotlin.time.Duration import kotlin.time.DurationUnit import kotlin.time.toDuration import okhttp3.CacheControl @@ -46,20 +47,20 @@ internal fun CacheControl.commonToString(): String { internal fun CacheControl.Builder.commonMaxAge(maxAge: Int, timeUnit: DurationUnit) = apply { require(maxAge >= 0) { "maxAge < 0: $maxAge" } - val maxAgeSecondsLong = maxAge.toDuration(timeUnit).inWholeSeconds - this.maxAgeSeconds = maxAgeSecondsLong.commonClampToInt() + val maxAgeSecondsDouble = Duration.convert(maxAge.toDouble(), timeUnit, DurationUnit.SECONDS) + this.maxAgeSeconds = maxAgeSecondsDouble.commonClampToInt() } internal fun CacheControl.Builder.commonMaxStale(maxStale: Int, timeUnit: DurationUnit) = apply { require(maxStale >= 0) { "maxStale < 0: $maxStale" } - val maxStaleSecondsLong = maxStale.toDuration(timeUnit).inWholeSeconds - this.maxStaleSeconds = maxStaleSecondsLong.commonClampToInt() + val maxStaleSecondsDouble = Duration.convert(maxStale.toDouble(), timeUnit, DurationUnit.SECONDS) + this.maxStaleSeconds = maxStaleSecondsDouble.commonClampToInt() } internal fun CacheControl.Builder.commonMinFresh(minFresh: Int, timeUnit: DurationUnit) = apply { require(minFresh >= 0) { "minFresh < 0: $minFresh" } - val minFreshSecondsLong = minFresh.toDuration(timeUnit).inWholeSeconds - this.minFreshSeconds = minFreshSecondsLong.commonClampToInt() + val minFreshSecondsDouble = Duration.convert(minFresh.toDouble(), timeUnit, DurationUnit.SECONDS) + this.minFreshSeconds = minFreshSecondsDouble.commonClampToInt() } internal fun Long.commonClampToInt(): Int { @@ -69,6 +70,13 @@ internal fun Long.commonClampToInt(): Int { } } +internal fun Double.commonClampToInt(): Int { + return when { + this > Int.MAX_VALUE -> Int.MAX_VALUE + else -> toInt() + } +} + internal fun CacheControl.Companion.commonForceNetwork() = CacheControl.Builder() .noCache() .build() diff --git a/okhttp/src/jvmMain/kotlin/okhttp3/CacheControl.kt b/okhttp/src/jvmMain/kotlin/okhttp3/CacheControl.kt index 0ccc22d7d39d..bbd45b5e5011 100644 --- a/okhttp/src/jvmMain/kotlin/okhttp3/CacheControl.kt +++ b/okhttp/src/jvmMain/kotlin/okhttp3/CacheControl.kt @@ -22,6 +22,9 @@ import okhttp3.internal.commonClampToInt import okhttp3.internal.commonForceCache import okhttp3.internal.commonForceNetwork import okhttp3.internal.commonImmutable +import okhttp3.internal.commonMaxAge +import okhttp3.internal.commonMaxStale +import okhttp3.internal.commonMinFresh import okhttp3.internal.commonNoCache import okhttp3.internal.commonNoStore import okhttp3.internal.commonNoTransform @@ -147,18 +150,11 @@ actual class CacheControl internal actual constructor( actual fun immutable() = commonImmutable() - // We are compiling with kotlin 1.6 but need to run with older versions at runtime. For - // maximum compat we therefore need to handle both the case where DurationUnit is typealiased - // to TimeUnit (as it was pre-1.6) and where it is a distinct wrapper enum (in 1.6). We also - // can't use durationUnit.toTimeUnit() because that doesn't exist in the typealiased case. - // This function should work in either case. - internal fun toJavaTimeUnit(durationUnit: DurationUnit) = TimeUnit.valueOf(durationUnit.name) + actual fun maxAge(maxAge: Int, timeUnit: DurationUnit) = commonMaxAge(maxAge, timeUnit) - actual fun maxAge(maxAge: Int, timeUnit: DurationUnit) = maxAge(maxAge, toJavaTimeUnit(timeUnit)) + actual fun maxStale(maxStale: Int, timeUnit: DurationUnit) = commonMaxStale(maxStale, timeUnit) - actual fun maxStale(maxStale: Int, timeUnit: DurationUnit) = maxStale(maxStale, toJavaTimeUnit(timeUnit)) - - actual fun minFresh(minFresh: Int, timeUnit: DurationUnit) = minFresh(minFresh, toJavaTimeUnit(timeUnit)) + actual fun minFresh(minFresh: Int, timeUnit: DurationUnit) = commonMinFresh(minFresh, timeUnit) /** * Sets the maximum age of a cached response. If the cache response's age exceeds [maxAge], it