Skip to content

Commit 86df216

Browse files
committed
Add orEmpty extensions for immutable collections
1 parent fe7b163 commit 86df216

File tree

6 files changed

+45
-0
lines changed

6 files changed

+45
-0
lines changed

core/api/kotlinx-collections-immutable.api

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public final class kotlinx/collections/immutable/ExtensionsKt {
2727
public static final fun mutate (Lkotlinx/collections/immutable/PersistentList;Lkotlin/jvm/functions/Function1;)Lkotlinx/collections/immutable/PersistentList;
2828
public static final fun mutate (Lkotlinx/collections/immutable/PersistentMap;Lkotlin/jvm/functions/Function1;)Lkotlinx/collections/immutable/PersistentMap;
2929
public static final fun mutate (Lkotlinx/collections/immutable/PersistentSet;Lkotlin/jvm/functions/Function1;)Lkotlinx/collections/immutable/PersistentSet;
30+
public static final fun orEmpty (Lkotlinx/collections/immutable/ImmutableList;)Lkotlinx/collections/immutable/ImmutableList;
31+
public static final fun orEmpty (Lkotlinx/collections/immutable/ImmutableMap;)Lkotlinx/collections/immutable/ImmutableMap;
32+
public static final fun orEmpty (Lkotlinx/collections/immutable/ImmutableSet;)Lkotlinx/collections/immutable/ImmutableSet;
3033
public static final fun persistentHashMapOf ()Lkotlinx/collections/immutable/PersistentMap;
3134
public static final fun persistentHashMapOf ([Lkotlin/Pair;)Lkotlinx/collections/immutable/PersistentMap;
3235
public static final fun persistentHashSetOf ()Lkotlinx/collections/immutable/PersistentSet;

core/api/kotlinx-collections-immutable.klib.api

+3
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ final fun (kotlin/CharSequence).kotlinx.collections.immutable/toPersistentSet():
170170
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlin.collections/Map<#A, #B>).kotlinx.collections.immutable/toImmutableMap(): kotlinx.collections.immutable/ImmutableMap<#A, #B> // kotlinx.collections.immutable/toImmutableMap|[email protected]<0:0,0:1>(){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
171171
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlin.collections/Map<#A, #B>).kotlinx.collections.immutable/toPersistentHashMap(): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/toPersistentHashMap|[email protected]<0:0,0:1>(){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
172172
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlin.collections/Map<#A, #B>).kotlinx.collections.immutable/toPersistentMap(): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/toPersistentMap|[email protected]<0:0,0:1>(){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
173+
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/ImmutableMap<#A, #B>?).kotlinx.collections.immutable/orEmpty(): kotlinx.collections.immutable/ImmutableMap<#A, #B> // kotlinx.collections.immutable/orEmpty|[email protected]<0:0,0:1>?(){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
173174
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap<out #A, #B>).kotlinx.collections.immutable/minus(#A): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/minus|[email protected]<out|0:0,0:1>(0:0){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
174175
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap<out #A, #B>).kotlinx.collections.immutable/minus(kotlin.collections/Iterable<#A>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/minus|[email protected]<out|0:0,0:1>(kotlin.collections.Iterable<0:0>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
175176
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap<out #A, #B>).kotlinx.collections.immutable/minus(kotlin.sequences/Sequence<#A>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/minus|[email protected]<out|0:0,0:1>(kotlin.sequences.Sequence<0:0>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
@@ -236,6 +237,8 @@ final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutab
236237
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap<out #A, #B>).kotlinx.collections.immutable/plus(kotlin.sequences/Sequence<kotlin/Pair<#A, #B>>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/plus|[email protected]<out|0:0,0:1>(kotlin.sequences.Sequence<kotlin.Pair<0:0,0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
237238
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap<out #A, #B>).kotlinx.collections.immutable/plus(kotlin/Array<out kotlin/Pair<#A, #B>>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/plus|[email protected]<out|0:0,0:1>(kotlin.Array<out|kotlin.Pair<0:0,0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
238239
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap<out #A, #B>).kotlinx.collections.immutable/plus(kotlin/Pair<#A, #B>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/plus|[email protected]<out|0:0,0:1>(kotlin.Pair<0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
240+
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/ImmutableList<#A>?).kotlinx.collections.immutable/orEmpty(): kotlinx.collections.immutable/ImmutableList<#A> // kotlinx.collections.immutable/orEmpty|[email protected]<0:0>?(){0§<kotlin.Any?>}[0]
241+
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/ImmutableSet<#A>?).kotlinx.collections.immutable/orEmpty(): kotlinx.collections.immutable/ImmutableSet<#A> // kotlinx.collections.immutable/orEmpty|[email protected]<0:0>?(){0§<kotlin.Any?>}[0]
239242
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentCollection<#A>).kotlinx.collections.immutable/minus(#A): kotlinx.collections.immutable/PersistentCollection<#A> // kotlinx.collections.immutable/minus|[email protected]<0:0>(0:0){0§<kotlin.Any?>}[0]
240243
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentCollection<#A>).kotlinx.collections.immutable/plus(#A): kotlinx.collections.immutable/PersistentCollection<#A> // kotlinx.collections.immutable/plus|[email protected]<0:0>(0:0){0§<kotlin.Any?>}[0]
241244
final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/PersistentList<#A>).kotlinx.collections.immutable/minus(#A): kotlinx.collections.immutable/PersistentList<#A> // kotlinx.collections.immutable/minus|[email protected]<0:0>(0:0){0§<kotlin.Any?>}[0]

core/commonMain/src/extensions.kt

+13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ import kotlinx.collections.immutable.implementations.persistentOrderedSet.Persis
3030
*/
3131
public inline fun <T> PersistentSet<T>.mutate(mutator: (MutableSet<T>) -> Unit): PersistentSet<T> = builder().apply(mutator).build()
3232

33+
/**
34+
* Returns this [ImmutableSet] if it's not `null` and the empty [ImmutableSet] otherwise.
35+
*/
36+
public inline fun <T> ImmutableSet<T>?.orEmpty(): ImmutableSet<T> = this ?: persistentSetOf()
37+
3338
/**
3439
* Returns the result of applying the provided modifications on this list.
3540
*
@@ -52,6 +57,10 @@ public inline fun <T> PersistentList<T>.mutate(mutator: (MutableList<T>) -> Unit
5257
public inline fun <K, V> PersistentMap<out K, V>.mutate(mutator: (MutableMap<K, V>) -> Unit): PersistentMap<K, V> =
5358
(this as PersistentMap<K, V>).builder().apply(mutator).build()
5459

60+
/**
61+
* Returns this [ImmutableList] if it's not `null` and the empty [ImmutableList] otherwise.
62+
*/
63+
public inline fun <T> ImmutableList<T>?.orEmpty(): ImmutableList<T> = this ?: persistentListOf()
5564

5665
/**
5766
* Returns the result of adding the specified [element] to this collection.
@@ -440,6 +449,10 @@ public operator fun <K, V> PersistentMap<out K, V>.minus(keys: Array<out K>): Pe
440449
public operator fun <K, V> PersistentMap<out K, V>.minus(keys: Sequence<K>): PersistentMap<K, V>
441450
= mutate { it.minusAssign(keys) }
442451

452+
/**
453+
* Returns the [ImmutableMap] if it's not `null`, or the empty [ImmutableMap] otherwise.
454+
*/
455+
public fun <K, V> ImmutableMap<K, V>?.orEmpty(): ImmutableMap<K, V> = this ?: persistentMapOf()
443456

444457
/**
445458
* Returns a new persistent list of the specified elements.

core/commonTest/src/contract/list/ImmutableListTest.kt

+8
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,12 @@ class ImmutableListTest {
224224

225225
assertEquals<List<*>>(listOf("x", null, 1), listAny)
226226
}
227+
228+
@Test fun immutableListOrEmpty() {
229+
val emptyList = (null as ImmutableList<String>?).orEmpty()
230+
val notEmptyList = persistentListOf("a")
231+
232+
assertEquals(emptyList, persistentListOf())
233+
assertEquals(notEmptyList, notEmptyList.orEmpty())
234+
}
227235
}

core/commonTest/src/contract/map/ImmutableMapTest.kt

+9
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,13 @@ abstract class ImmutableMapTest {
392392

393393
testEquality(data, changed)
394394
}
395+
396+
@Test
397+
fun immutableMapOrEmpty() {
398+
val emptyMap = (null as PersistentMap<String, Int>?).orEmpty()
399+
val notEmptyMap = persistentMapOf("a" to 1)
400+
401+
assertEquals(emptyMap, persistentMapOf())
402+
assertEquals(notEmptyMap, notEmptyMap.orEmpty())
403+
}
395404
}

core/commonTest/src/contract/set/ImmutableSetTest.kt

+9
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,13 @@ abstract class ImmutableSetTestBase {
404404

405405
testEquality(data, changed)
406406
}
407+
408+
@Test
409+
fun immutableSetOrEmpty() {
410+
val emptySet = (null as ImmutableSet<String>?).orEmpty()
411+
val notEmptySet = persistentSetOf("a")
412+
413+
assertEquals(emptySet, persistentSetOf())
414+
assertEquals(notEmptySet, notEmptySet.orEmpty())
415+
}
407416
}

0 commit comments

Comments
 (0)