Deprecate some utils and provide sensible replacements from stdlib.
This commit is contained in:
@@ -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: Any> T?.singletonOrEmptyList(): List<T> = if (this != null) Collections.singletonList(this) else Collections.emptyList()
|
||||
|
||||
@Deprecated("Use listOf(this) or this.let(::listOf) instead", ReplaceWith("listOf(this)"))
|
||||
fun <T> T.singletonList(): List<T> = Collections.singletonList(this)
|
||||
|
||||
@Deprecated("Use this?.let(::setOf).orEmpty() instead", ReplaceWith("this?.let(::setOf).orEmpty()"))
|
||||
fun <T: Any> T?.singletonOrEmptySet(): Set<T> = if (this != null) Collections.singleton(this) else Collections.emptySet()
|
||||
|
||||
inline fun <reified T : Any> Sequence<*>.firstIsInstanceOrNull(): T? {
|
||||
@@ -76,6 +79,7 @@ fun <T> sequenceOfLazyValues(vararg elements: () -> T): Sequence<T> = elements.a
|
||||
|
||||
fun <T1, T2> Pair<T1, T2>.swap(): Pair<T2, T1> = Pair(second, first)
|
||||
|
||||
@Deprecated("Use takeIf() instead.", ReplaceWith("this.takeIf(predicate)"))
|
||||
fun <T: Any> T.check(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null
|
||||
|
||||
inline fun <reified T : Any> Any?.safeAs(): T? = this as? T
|
||||
@@ -100,15 +104,11 @@ fun <T : Any> constant(calculator: () -> T): T {
|
||||
|
||||
private val constantMap = ConcurrentHashMap<Function0<*>, 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 <T, R : Any> Iterable<T>.firstNotNullResult(transform: (T) -> R?): R? {
|
||||
for (element in this) {
|
||||
|
||||
@@ -56,6 +56,7 @@ inline fun <T, C: Collection<T>> C.ifEmpty(body: () -> C): C = if (isEmpty()) bo
|
||||
|
||||
inline fun <T> Array<out T>.ifEmpty(body: () -> Array<out T>): Array<out T> = if (isEmpty()) body() else this
|
||||
|
||||
@Deprecated("Use listOfNotNull instead", ReplaceWith("listOfNotNull(item)"))
|
||||
fun <T: Any> emptyOrSingletonList(item: T?): List<T> = listOfNotNull(item)
|
||||
|
||||
fun <T: Any> MutableCollection<T>.addIfNotNull(t: T?) {
|
||||
@@ -77,6 +78,7 @@ fun <E> newLinkedHashSetWithExpectedSize(expectedSize: Int): LinkedHashSet<E> =
|
||||
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 <T> Collection<T>.toReadOnlyList(): List<T> =
|
||||
when (size) {
|
||||
0 -> emptyList()
|
||||
@@ -88,9 +90,10 @@ fun <T> ArrayList<T>.compactIfPossible(): List<T> =
|
||||
when (size) {
|
||||
0 -> emptyList()
|
||||
1 -> listOf(first())
|
||||
else -> apply(ArrayList<T>::trimToSize)
|
||||
else -> apply { trimToSize() }
|
||||
}
|
||||
|
||||
@Deprecated("Use listOfNotNull(this) or this.let(::listOfNotNull) instead", ReplaceWith("listOfNotNull(this)"))
|
||||
fun <T: Any> T?.singletonOrEmptyList(): List<T> =
|
||||
if (this != null) listOf(this) else emptyList()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user