Add isNullOrEmpty() to Array, Collection, and Map. Fixes KT-23279
This commit is contained in:
@@ -32,6 +32,18 @@ class Arrays {
|
||||
val array: Array<Char>? = arrayOf('a', 'b', 'c')
|
||||
assertPrints(array.orEmpty().contentToString(), "[a, b, c]")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun arrayIsNullOrEmpty() {
|
||||
val nullArray: Array<Any>? = null
|
||||
assertTrue(nullArray.isNullOrEmpty())
|
||||
|
||||
val emptyArray = emptyArray<Any>()
|
||||
assertTrue(emptyArray.isNullOrEmpty())
|
||||
|
||||
val array = arrayOf('a', 'b', 'c')
|
||||
assertFalse(array.isNullOrEmpty())
|
||||
}
|
||||
}
|
||||
|
||||
class Transformations {
|
||||
|
||||
@@ -53,13 +53,13 @@ class Collections {
|
||||
|
||||
@Sample
|
||||
fun collectionIsNullOrEmpty() {
|
||||
val nullCollection: Collection<Any>? = null
|
||||
assertTrue(nullCollection.isNullOrEmpty())
|
||||
val nullList: List<Any>? = null
|
||||
assertTrue(nullList.isNullOrEmpty())
|
||||
|
||||
val emptyCollection: Collection<Any>? = listOf()
|
||||
assertTrue(emptyCollection.isNullOrEmpty())
|
||||
val empty: List<Any>? = emptyList<Any>()
|
||||
assertTrue(empty.isNullOrEmpty())
|
||||
|
||||
val collection: Collection<Char>? = listOf('a', 'b', 'c')
|
||||
val collection: List<Char>? = listOf('a', 'b', 'c')
|
||||
assertFalse(collection.isNullOrEmpty())
|
||||
}
|
||||
|
||||
|
||||
@@ -37,3 +37,9 @@ public fun <T, R> Array<out Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
|
||||
}
|
||||
return listT to listR
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if this array is null or empty.
|
||||
* @sample samples.collections.Arrays.Usage.arrayIsNullOrEmpty
|
||||
*/
|
||||
public fun Array<*>?.isNullOrEmpty(): Boolean = this == null || this.isEmpty()
|
||||
|
||||
@@ -140,6 +140,10 @@ private const val INT_MAX_POWER_OF_TWO: Int = Int.MAX_VALUE / 2 + 1
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <K, V> Map<out K, V>.isNotEmpty(): Boolean = !isEmpty()
|
||||
|
||||
/** Returns `true` if this map is null or empty. */
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <K, V> Map<out K, V>?.isNullOrEmpty(): Boolean = this == null || isEmpty()
|
||||
|
||||
/**
|
||||
* Returns the [Map] if its not `null`, or the empty [Map] otherwise.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user