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')
|
val array: Array<Char>? = arrayOf('a', 'b', 'c')
|
||||||
assertPrints(array.orEmpty().contentToString(), "[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 {
|
class Transformations {
|
||||||
|
|||||||
@@ -53,13 +53,13 @@ class Collections {
|
|||||||
|
|
||||||
@Sample
|
@Sample
|
||||||
fun collectionIsNullOrEmpty() {
|
fun collectionIsNullOrEmpty() {
|
||||||
val nullCollection: Collection<Any>? = null
|
val nullList: List<Any>? = null
|
||||||
assertTrue(nullCollection.isNullOrEmpty())
|
assertTrue(nullList.isNullOrEmpty())
|
||||||
|
|
||||||
val emptyCollection: Collection<Any>? = listOf()
|
val empty: List<Any>? = emptyList<Any>()
|
||||||
assertTrue(emptyCollection.isNullOrEmpty())
|
assertTrue(empty.isNullOrEmpty())
|
||||||
|
|
||||||
val collection: Collection<Char>? = listOf('a', 'b', 'c')
|
val collection: List<Char>? = listOf('a', 'b', 'c')
|
||||||
assertFalse(collection.isNullOrEmpty())
|
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
|
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
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun <K, V> Map<out K, V>.isNotEmpty(): Boolean = !isEmpty()
|
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.
|
* Returns the [Map] if its not `null`, or the empty [Map] otherwise.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user