Skip to content

Commit 06a0529

Browse files
authored
Fix public APIs for kotlin.time.Duration (#8355)
* Fix public APIs for kotlin.time.Duration Use this as our preferred API for accepting a duration in OkHttpClient and CacheControl. Also hide these functions from the Java API. * Code review feedback * Spotless * apiDump
1 parent 360006a commit 06a0529

4 files changed

Lines changed: 29 additions & 57 deletions

File tree

okhttp/api/okhttp.api

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ public final class okhttp3/CacheControl$Builder {
130130
public final fun build ()Lokhttp3/CacheControl;
131131
public final fun immutable ()Lokhttp3/CacheControl$Builder;
132132
public final fun maxAge (ILjava/util/concurrent/TimeUnit;)Lokhttp3/CacheControl$Builder;
133-
public final fun maxAge (ILkotlin/time/DurationUnit;)Lokhttp3/CacheControl$Builder;
133+
public final fun maxAge-LRDsOJo (J)Lokhttp3/CacheControl$Builder;
134134
public final fun maxStale (ILjava/util/concurrent/TimeUnit;)Lokhttp3/CacheControl$Builder;
135-
public final fun maxStale (ILkotlin/time/DurationUnit;)Lokhttp3/CacheControl$Builder;
135+
public final fun maxStale-LRDsOJo (J)Lokhttp3/CacheControl$Builder;
136136
public final fun minFresh (ILjava/util/concurrent/TimeUnit;)Lokhttp3/CacheControl$Builder;
137-
public final fun minFresh (ILkotlin/time/DurationUnit;)Lokhttp3/CacheControl$Builder;
137+
public final fun minFresh-LRDsOJo (J)Lokhttp3/CacheControl$Builder;
138138
public final fun noCache ()Lokhttp3/CacheControl$Builder;
139139
public final fun noStore ()Lokhttp3/CacheControl$Builder;
140140
public final fun noTransform ()Lokhttp3/CacheControl$Builder;

okhttp/src/main/kotlin/okhttp3/CacheControl.kt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616
package okhttp3
1717

1818
import java.util.concurrent.TimeUnit
19-
import kotlin.time.DurationUnit
19+
import kotlin.time.Duration
2020
import okhttp3.internal.commonBuild
2121
import okhttp3.internal.commonClampToInt
2222
import okhttp3.internal.commonForceCache
2323
import okhttp3.internal.commonForceNetwork
2424
import okhttp3.internal.commonImmutable
25-
import okhttp3.internal.commonMaxAge
26-
import okhttp3.internal.commonMaxStale
27-
import okhttp3.internal.commonMinFresh
2825
import okhttp3.internal.commonNoCache
2926
import okhttp3.internal.commonNoStore
3027
import okhttp3.internal.commonNoTransform
@@ -186,26 +183,29 @@ class CacheControl internal constructor(
186183
* Sets the maximum age of a cached response. If the cache response's age exceeds [maxAge], it
187184
* will not be used and a network request will be made.
188185
*
189-
* @param maxAge a non-negative integer. This is stored and transmitted with [TimeUnit.SECONDS]
186+
* @param maxAge a non-negative duration. This is stored and transmitted with [TimeUnit.SECONDS]
190187
* precision; finer precision will be lost.
191188
*/
192-
@ExperimentalOkHttpApi
193-
fun maxAge(
194-
maxAge: Int,
195-
timeUnit: DurationUnit,
196-
) = commonMaxAge(maxAge, timeUnit)
189+
fun maxAge(maxAge: Duration) =
190+
apply {
191+
val maxAgeSeconds = maxAge.inWholeSeconds
192+
require(maxAgeSeconds >= 0) { "maxAge < 0: $maxAgeSeconds" }
193+
this.maxAgeSeconds = maxAgeSeconds.commonClampToInt()
194+
}
197195

198-
@ExperimentalOkHttpApi
199-
fun maxStale(
200-
maxStale: Int,
201-
timeUnit: DurationUnit,
202-
) = commonMaxStale(maxStale, timeUnit)
196+
fun maxStale(maxStale: Duration) =
197+
apply {
198+
val maxStaleSeconds = maxStale.inWholeSeconds
199+
require(maxStaleSeconds >= 0) { "maxStale < 0: $maxStaleSeconds" }
200+
this.maxStaleSeconds = maxStaleSeconds.commonClampToInt()
201+
}
203202

204-
@ExperimentalOkHttpApi
205-
fun minFresh(
206-
minFresh: Int,
207-
timeUnit: DurationUnit,
208-
) = commonMinFresh(minFresh, timeUnit)
203+
fun minFresh(minFresh: Duration) =
204+
apply {
205+
val minFreshSeconds = minFresh.inWholeSeconds
206+
require(minFreshSeconds >= 0) { "minFresh < 0: $minFreshSeconds" }
207+
this.minFreshSeconds = minFreshSeconds.commonClampToInt()
208+
}
209209

210210
/**
211211
* Sets the maximum age of a cached response. If the cache response's age exceeds [maxAge], it

okhttp/src/main/kotlin/okhttp3/internal/-CacheControlCommon.kt

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
package okhttp3.internal
1919

20-
import kotlin.time.DurationUnit
21-
import kotlin.time.toDuration
20+
import kotlin.time.Duration.Companion.seconds
2221
import okhttp3.CacheControl
2322
import okhttp3.Headers
2423

@@ -47,33 +46,6 @@ internal fun CacheControl.commonToString(): String {
4746
return result
4847
}
4948

50-
internal fun CacheControl.Builder.commonMaxAge(
51-
maxAge: Int,
52-
timeUnit: DurationUnit,
53-
) = apply {
54-
require(maxAge >= 0) { "maxAge < 0: $maxAge" }
55-
val maxAgeSecondsLong = maxAge.toDuration(timeUnit).inWholeSeconds
56-
this.maxAgeSeconds = maxAgeSecondsLong.commonClampToInt()
57-
}
58-
59-
internal fun CacheControl.Builder.commonMaxStale(
60-
maxStale: Int,
61-
timeUnit: DurationUnit,
62-
) = apply {
63-
require(maxStale >= 0) { "maxStale < 0: $maxStale" }
64-
val maxStaleSecondsLong = maxStale.toDuration(timeUnit).inWholeSeconds
65-
this.maxStaleSeconds = maxStaleSecondsLong.commonClampToInt()
66-
}
67-
68-
internal fun CacheControl.Builder.commonMinFresh(
69-
minFresh: Int,
70-
timeUnit: DurationUnit,
71-
) = apply {
72-
require(minFresh >= 0) { "minFresh < 0: $minFresh" }
73-
val minFreshSecondsLong = minFresh.toDuration(timeUnit).inWholeSeconds
74-
this.minFreshSeconds = minFreshSecondsLong.commonClampToInt()
75-
}
76-
7749
internal fun Long.commonClampToInt(): Int {
7850
return when {
7951
this > Int.MAX_VALUE -> Int.MAX_VALUE
@@ -89,7 +61,7 @@ internal fun CacheControl.Companion.commonForceNetwork() =
8961
internal fun CacheControl.Companion.commonForceCache() =
9062
CacheControl.Builder()
9163
.onlyIfCached()
92-
.maxStale(Int.MAX_VALUE, DurationUnit.SECONDS)
64+
.maxStale(Int.MAX_VALUE.seconds)
9365
.build()
9466

9567
internal fun CacheControl.Builder.commonBuild(): CacheControl {

okhttp/src/test/java/okhttp3/CacheControlTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import assertk.assertions.isEqualTo
2020
import assertk.assertions.isFalse
2121
import assertk.assertions.isTrue
2222
import kotlin.test.Test
23-
import kotlin.time.DurationUnit
23+
import kotlin.time.Duration.Companion.seconds
2424

2525
class CacheControlTest {
2626
@Test
@@ -48,9 +48,9 @@ class CacheControlTest {
4848
CacheControl.Builder()
4949
.noCache()
5050
.noStore()
51-
.maxAge(1, DurationUnit.SECONDS)
52-
.maxStale(2, DurationUnit.SECONDS)
53-
.minFresh(3, DurationUnit.SECONDS)
51+
.maxAge(1.seconds)
52+
.maxStale(2.seconds)
53+
.minFresh(3.seconds)
5454
.onlyIfCached()
5555
.noTransform()
5656
.immutable()

0 commit comments

Comments
 (0)