Make Optional extensions stable since 1.8

KT-51907, KT-53277
This commit is contained in:
Ilya Gorbunov
2022-09-14 18:33:46 +02:00
committed by Space
parent b76854eddf
commit 75e1effbc5
@@ -2,7 +2,7 @@
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
package kotlin.jvm.optionals package kotlin.jvm.optionals
import java.util.Optional import java.util.Optional
@@ -10,30 +10,30 @@ import java.util.Optional
/** /**
* Returns this [Optional]'s value if [present][Optional.isPresent], or otherwise `null`. * Returns this [Optional]'s value if [present][Optional.isPresent], or otherwise `null`.
*/ */
@SinceKotlin("1.7") @SinceKotlin("1.8")
@ExperimentalStdlibApi @WasExperimental(ExperimentalStdlibApi::class)
public fun <T : Any> Optional<T>.getOrNull(): T? = orElse(null) public fun <T : Any> Optional<T>.getOrNull(): T? = orElse(null)
/** /**
* Returns this [Optional]'s value if [present][Optional.isPresent], or otherwise [defaultValue]. * Returns this [Optional]'s value if [present][Optional.isPresent], or otherwise [defaultValue].
*/ */
@SinceKotlin("1.7") @SinceKotlin("1.8")
@ExperimentalStdlibApi @WasExperimental(ExperimentalStdlibApi::class)
public fun <T> Optional<out T & Any>.getOrDefault(defaultValue: T): T = if (isPresent) get() else defaultValue public fun <T> Optional<out T & Any>.getOrDefault(defaultValue: T): T = if (isPresent) get() else defaultValue
/** /**
* Returns this [Optional]'s value if [present][Optional.isPresent], or otherwise the result of the [defaultValue] function. * Returns this [Optional]'s value if [present][Optional.isPresent], or otherwise the result of the [defaultValue] function.
*/ */
@SinceKotlin("1.7") @SinceKotlin("1.8")
@ExperimentalStdlibApi @WasExperimental(ExperimentalStdlibApi::class)
public inline fun <T> Optional<out T & Any>.getOrElse(defaultValue: () -> T): T = public inline fun <T> Optional<out T & Any>.getOrElse(defaultValue: () -> T): T =
if (isPresent) get() else defaultValue() if (isPresent) get() else defaultValue()
/** /**
* Appends this [Optional]'s value to the given [destination] collection if [present][Optional.isPresent]. * Appends this [Optional]'s value to the given [destination] collection if [present][Optional.isPresent].
*/ */
@SinceKotlin("1.7") @SinceKotlin("1.8")
@ExperimentalStdlibApi @WasExperimental(ExperimentalStdlibApi::class)
public fun <T : Any, C : MutableCollection<in T>> Optional<T>.toCollection(destination: C): C { public fun <T : Any, C : MutableCollection<in T>> Optional<T>.toCollection(destination: C): C {
if (isPresent) { if (isPresent) {
destination.add(get()) destination.add(get())
@@ -45,8 +45,8 @@ public fun <T : Any, C : MutableCollection<in T>> Optional<T>.toCollection(desti
* Returns a new read-only list of this [Optional]'s value if [present][Optional.isPresent], or otherwise an empty list. * Returns a new read-only list of this [Optional]'s value if [present][Optional.isPresent], or otherwise an empty list.
* The returned list is serializable (JVM). * The returned list is serializable (JVM).
*/ */
@SinceKotlin("1.7") @SinceKotlin("1.8")
@ExperimentalStdlibApi @WasExperimental(ExperimentalStdlibApi::class)
public fun <T : Any> Optional<out T>.toList(): List<T> = public fun <T : Any> Optional<out T>.toList(): List<T> =
if (isPresent) listOf(get()) else emptyList() if (isPresent) listOf(get()) else emptyList()
@@ -54,8 +54,8 @@ public fun <T : Any> Optional<out T>.toList(): List<T> =
* Returns a new read-only set of this [Optional]'s value if [present][Optional.isPresent], or otherwise an empty set. * Returns a new read-only set of this [Optional]'s value if [present][Optional.isPresent], or otherwise an empty set.
* The returned set is serializable (JVM). * The returned set is serializable (JVM).
*/ */
@SinceKotlin("1.7") @SinceKotlin("1.8")
@ExperimentalStdlibApi @WasExperimental(ExperimentalStdlibApi::class)
public fun <T : Any> Optional<out T>.toSet(): Set<T> = public fun <T : Any> Optional<out T>.toSet(): Set<T> =
if (isPresent) setOf(get()) else emptySet() if (isPresent) setOf(get()) else emptySet()
@@ -63,7 +63,7 @@ public fun <T : Any> Optional<out T>.toSet(): Set<T> =
* Returns a new sequence for this [Optional]'s value if [present][Optional.isPresent], or otherwise an empty sequence. * Returns a new sequence for this [Optional]'s value if [present][Optional.isPresent], or otherwise an empty sequence.
* The returned set is serializable (JVM). * The returned set is serializable (JVM).
*/ */
@SinceKotlin("1.7") @SinceKotlin("1.8")
@ExperimentalStdlibApi @WasExperimental(ExperimentalStdlibApi::class)
public fun <T : Any> Optional<out T>.asSequence(): Sequence<T> = public fun <T : Any> Optional<out T>.asSequence(): Sequence<T> =
if (isPresent) sequenceOf(get()) else emptySequence() if (isPresent) sequenceOf(get()) else emptySequence()