Skip to content

Commit

Permalink
Revert "Improve runtime compatibility with kotlin 1.5.31 (square#7343)"
Browse files Browse the repository at this point in the history
This reverts commit 3ff1b61
  • Loading branch information
yschimke committed Jul 23, 2022
1 parent 106b97f commit 9fe7831
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Expand Up @@ -15,6 +15,7 @@
*/
package okhttp3.internal

import kotlin.time.Duration
import kotlin.time.DurationUnit
import kotlin.time.toDuration
import okhttp3.CacheControl
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
16 changes: 6 additions & 10 deletions okhttp/src/jvmMain/kotlin/okhttp3/CacheControl.kt
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9fe7831

Please sign in to comment.