ifEmpty and isBlank extensions
- Introduce ifEmpty extension for Collections, Maps, Arrays, Sequences - Introduce ifEmpty and isBlank for CharSequences #KT-15695 Fixed
This commit is contained in:
@@ -53,3 +53,15 @@ public inline fun Array<*>?.isNullOrEmpty(): Boolean {
|
||||
|
||||
return this == null || this.isEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this array if it's not empty
|
||||
* or the result of calling [defaultValue] function if the array is empty.
|
||||
*
|
||||
* @sample samples.collections.Arrays.Usage.arrayIfEmpty
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@kotlin.internal.InlineOnly
|
||||
@Suppress("UPPER_BOUND_CANNOT_BE_ARRAY")
|
||||
public inline fun <C, R> C.ifEmpty(defaultValue: () -> R): R where C : Array<*>, C : R =
|
||||
if (isEmpty()) defaultValue() else this
|
||||
|
||||
@@ -197,6 +197,18 @@ public inline fun <T> Collection<T>?.orEmpty(): Collection<T> = this ?: emptyLis
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> List<T>?.orEmpty(): List<T> = this ?: emptyList()
|
||||
|
||||
/**
|
||||
* Returns this collection if it's not empty
|
||||
* or the result of calling [defaultValue] function if the collection is empty.
|
||||
*
|
||||
* @sample samples.collections.Collections.Collections.collectionIfEmpty
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <C, R> C.ifEmpty(defaultValue: () -> R): R where C : Collection<*>, C : R =
|
||||
if (isEmpty()) defaultValue() else this
|
||||
|
||||
|
||||
/**
|
||||
* Checks if all elements in the specified collection are contained in this collection.
|
||||
*
|
||||
|
||||
@@ -162,6 +162,17 @@ public inline fun <K, V> Map<out K, V>?.isNullOrEmpty(): Boolean {
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <K, V> Map<K, V>?.orEmpty(): Map<K, V> = this ?: emptyMap()
|
||||
|
||||
/**
|
||||
* Returns this map if it's not empty
|
||||
* or the result of calling [defaultValue] function if the map is empty.
|
||||
*
|
||||
* @sample samples.collections.Maps.Usage.mapIfEmpty
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <M, R> M.ifEmpty(defaultValue: () -> R): R where M : Map<*, *>, M : R =
|
||||
if (isEmpty()) defaultValue() else this
|
||||
|
||||
/**
|
||||
* Checks if the map contains the given key.
|
||||
*
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
package kotlin.sequences
|
||||
|
||||
import kotlin.*
|
||||
import kotlin.coroutines.experimental.buildSequence
|
||||
|
||||
/**
|
||||
* Given an [iterator] function constructs a [Sequence] that returns values through the [Iterator]
|
||||
@@ -55,6 +56,23 @@ private object EmptySequence : Sequence<Nothing>, DropTakeSequence<Nothing> {
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Sequence<T>?.orEmpty(): Sequence<T> = this ?: emptySequence()
|
||||
|
||||
|
||||
/**
|
||||
* Returns a sequence that iterates through the elements either of this sequence
|
||||
* or, if this sequence turns out to be empty, of the sequence returned by [defaultValue] function.
|
||||
*
|
||||
* @sample samples.collections.Sequences.Usage.sequenceIfEmpty
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public fun <T> Sequence<T>.ifEmpty(defaultValue: () -> Sequence<T>): Sequence<T> = buildSequence {
|
||||
val iterator = this@ifEmpty.iterator()
|
||||
if (iterator.hasNext()) {
|
||||
yieldAll(iterator)
|
||||
} else {
|
||||
yieldAll(defaultValue())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sequence of all elements from all sequences in this sequence.
|
||||
*
|
||||
|
||||
@@ -276,6 +276,28 @@ public operator fun CharSequence.iterator(): CharIterator = object : CharIterato
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String?.orEmpty(): String = this ?: ""
|
||||
|
||||
/**
|
||||
* Returns this char sequence if it's not empty
|
||||
* or the result of calling [defaultValue] function if the char sequence is empty.
|
||||
*
|
||||
* @sample samples.text.Strings.stringIfEmpty
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <C, R> C.ifEmpty(defaultValue: () -> R): R where C : CharSequence, C : R =
|
||||
if (isEmpty()) defaultValue() else this
|
||||
|
||||
/**
|
||||
* Returns this char sequence if it is not empty and doesn't consist solely of whitespace characters,
|
||||
* or the result of calling [defaultValue] function otherwise.
|
||||
*
|
||||
* @sample samples.text.Strings.stringIfBlank
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <C, R> C.ifBlank(defaultValue: () -> R): R where C : CharSequence, C : R =
|
||||
if (isBlank()) defaultValue() else this
|
||||
|
||||
/**
|
||||
* Returns the range of valid character indices for this char sequence.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user