From 63f591bde9fd68b35ec63c3d5e8c3b0a9d9e7d0a Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 31 Jan 2017 22:07:44 +0300 Subject: [PATCH] Deprecate some utils and provide sensible replacements from stdlib. --- .../org/jetbrains/kotlin/utils/addToStdlib.kt | 16 ++++++++-------- .../org/jetbrains/kotlin/utils/collections.kt | 5 ++++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/core/util.runtime/src/org/jetbrains/kotlin/utils/addToStdlib.kt b/core/util.runtime/src/org/jetbrains/kotlin/utils/addToStdlib.kt index 86a30e2edad..4c32e44173c 100644 --- a/core/util.runtime/src/org/jetbrains/kotlin/utils/addToStdlib.kt +++ b/core/util.runtime/src/org/jetbrains/kotlin/utils/addToStdlib.kt @@ -20,10 +20,13 @@ import java.lang.reflect.Modifier import java.util.* import java.util.concurrent.ConcurrentHashMap +@Deprecated("Use listOfNotNull(this) or this.let(::listOfNotNull) instead", ReplaceWith("listOfNotNull(this)")) fun T?.singletonOrEmptyList(): List = if (this != null) Collections.singletonList(this) else Collections.emptyList() +@Deprecated("Use listOf(this) or this.let(::listOf) instead", ReplaceWith("listOf(this)")) fun T.singletonList(): List = Collections.singletonList(this) +@Deprecated("Use this?.let(::setOf).orEmpty() instead", ReplaceWith("this?.let(::setOf).orEmpty()")) fun T?.singletonOrEmptySet(): Set = if (this != null) Collections.singleton(this) else Collections.emptySet() inline fun Sequence<*>.firstIsInstanceOrNull(): T? { @@ -76,6 +79,7 @@ fun sequenceOfLazyValues(vararg elements: () -> T): Sequence = elements.a fun Pair.swap(): Pair = Pair(second, first) +@Deprecated("Use takeIf() instead.", ReplaceWith("this.takeIf(predicate)")) fun T.check(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null inline fun Any?.safeAs(): T? = this as? T @@ -100,15 +104,11 @@ fun constant(calculator: () -> T): T { private val constantMap = ConcurrentHashMap, Any>() -fun String.indexOfOrNull(char: Char, startIndex: Int = 0, ignoreCase: Boolean = false): Int? { - val index = indexOf(char, startIndex, ignoreCase) - return if (index >= 0) index else null -} +fun String.indexOfOrNull(char: Char, startIndex: Int = 0, ignoreCase: Boolean = false): Int? = + indexOf(char, startIndex, ignoreCase).takeIf { it >= 0 } -fun String.lastIndexOfOrNull(char: Char, startIndex: Int = 0, ignoreCase: Boolean = false): Int? { - val index = lastIndexOf(char, startIndex, ignoreCase) - return if (index >= 0) index else null -} +fun String.lastIndexOfOrNull(char: Char, startIndex: Int = 0, ignoreCase: Boolean = false): Int? = + lastIndexOf(char, startIndex, ignoreCase).takeIf { it >= 0 } inline fun Iterable.firstNotNullResult(transform: (T) -> R?): R? { for (element in this) { diff --git a/core/util.runtime/src/org/jetbrains/kotlin/utils/collections.kt b/core/util.runtime/src/org/jetbrains/kotlin/utils/collections.kt index 3e8e769c23d..c2af8056e65 100644 --- a/core/util.runtime/src/org/jetbrains/kotlin/utils/collections.kt +++ b/core/util.runtime/src/org/jetbrains/kotlin/utils/collections.kt @@ -56,6 +56,7 @@ inline fun > C.ifEmpty(body: () -> C): C = if (isEmpty()) bo inline fun Array.ifEmpty(body: () -> Array): Array = if (isEmpty()) body() else this +@Deprecated("Use listOfNotNull instead", ReplaceWith("listOfNotNull(item)")) fun emptyOrSingletonList(item: T?): List = listOfNotNull(item) fun MutableCollection.addIfNotNull(t: T?) { @@ -77,6 +78,7 @@ fun newLinkedHashSetWithExpectedSize(expectedSize: Int): LinkedHashSet = private fun capacity(expectedSize: Int): Int = if (expectedSize < 3) 3 else expectedSize + expectedSize / 3 + 1 +@Deprecated("Use toList(), it provides the same behavior for Collection", ReplaceWith("this.toList()")) fun Collection.toReadOnlyList(): List = when (size) { 0 -> emptyList() @@ -88,9 +90,10 @@ fun ArrayList.compactIfPossible(): List = when (size) { 0 -> emptyList() 1 -> listOf(first()) - else -> apply(ArrayList::trimToSize) + else -> apply { trimToSize() } } +@Deprecated("Use listOfNotNull(this) or this.let(::listOfNotNull) instead", ReplaceWith("listOfNotNull(this)")) fun T?.singletonOrEmptyList(): List = if (this != null) listOf(this) else emptyList()