From 86df21689efa5f4000289b9c55e322ab15bcd588 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 15 May 2024 15:43:35 +0800 Subject: [PATCH] Add orEmpty extensions for immutable collections --- core/api/kotlinx-collections-immutable.api | 3 +++ core/api/kotlinx-collections-immutable.klib.api | 3 +++ core/commonMain/src/extensions.kt | 13 +++++++++++++ .../src/contract/list/ImmutableListTest.kt | 8 ++++++++ .../commonTest/src/contract/map/ImmutableMapTest.kt | 9 +++++++++ .../commonTest/src/contract/set/ImmutableSetTest.kt | 9 +++++++++ 6 files changed, 45 insertions(+) diff --git a/core/api/kotlinx-collections-immutable.api b/core/api/kotlinx-collections-immutable.api index 09231a36..96c4aac1 100644 --- a/core/api/kotlinx-collections-immutable.api +++ b/core/api/kotlinx-collections-immutable.api @@ -27,6 +27,9 @@ public final class kotlinx/collections/immutable/ExtensionsKt { public static final fun mutate (Lkotlinx/collections/immutable/PersistentList;Lkotlin/jvm/functions/Function1;)Lkotlinx/collections/immutable/PersistentList; public static final fun mutate (Lkotlinx/collections/immutable/PersistentMap;Lkotlin/jvm/functions/Function1;)Lkotlinx/collections/immutable/PersistentMap; public static final fun mutate (Lkotlinx/collections/immutable/PersistentSet;Lkotlin/jvm/functions/Function1;)Lkotlinx/collections/immutable/PersistentSet; + public static final fun orEmpty (Lkotlinx/collections/immutable/ImmutableList;)Lkotlinx/collections/immutable/ImmutableList; + public static final fun orEmpty (Lkotlinx/collections/immutable/ImmutableMap;)Lkotlinx/collections/immutable/ImmutableMap; + public static final fun orEmpty (Lkotlinx/collections/immutable/ImmutableSet;)Lkotlinx/collections/immutable/ImmutableSet; public static final fun persistentHashMapOf ()Lkotlinx/collections/immutable/PersistentMap; public static final fun persistentHashMapOf ([Lkotlin/Pair;)Lkotlinx/collections/immutable/PersistentMap; public static final fun persistentHashSetOf ()Lkotlinx/collections/immutable/PersistentSet; diff --git a/core/api/kotlinx-collections-immutable.klib.api b/core/api/kotlinx-collections-immutable.klib.api index ac692d0d..07f6139f 100644 --- a/core/api/kotlinx-collections-immutable.klib.api +++ b/core/api/kotlinx-collections-immutable.klib.api @@ -170,6 +170,7 @@ final fun (kotlin/CharSequence).kotlinx.collections.immutable/toPersistentSet(): 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|toImmutableMap@kotlin.collections.Map<0:0,0:1>(){0§;1§}[0] 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|toPersistentHashMap@kotlin.collections.Map<0:0,0:1>(){0§;1§}[0] 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|toPersistentMap@kotlin.collections.Map<0:0,0:1>(){0§;1§}[0] +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|orEmpty@kotlinx.collections.immutable.ImmutableMap<0:0,0:1>?(){0§;1§}[0] final fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap).kotlinx.collections.immutable/minus(#A): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/minus|minus@kotlinx.collections.immutable.PersistentMap(0:0){0§;1§}[0] final fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap).kotlinx.collections.immutable/minus(kotlin.collections/Iterable<#A>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/minus|minus@kotlinx.collections.immutable.PersistentMap(kotlin.collections.Iterable<0:0>){0§;1§}[0] final fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap).kotlinx.collections.immutable/minus(kotlin.sequences/Sequence<#A>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/minus|minus@kotlinx.collections.immutable.PersistentMap(kotlin.sequences.Sequence<0:0>){0§;1§}[0] @@ -236,6 +237,8 @@ final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutab final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap).kotlinx.collections.immutable/plus(kotlin.sequences/Sequence>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/plus|plus@kotlinx.collections.immutable.PersistentMap(kotlin.sequences.Sequence>){0§;1§}[0] final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap).kotlinx.collections.immutable/plus(kotlin/Array>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/plus|plus@kotlinx.collections.immutable.PersistentMap(kotlin.Array>){0§;1§}[0] final inline fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlinx.collections.immutable/PersistentMap).kotlinx.collections.immutable/plus(kotlin/Pair<#A, #B>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/plus|plus@kotlinx.collections.immutable.PersistentMap(kotlin.Pair<0:0,0:1>){0§;1§}[0] +final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/ImmutableList<#A>?).kotlinx.collections.immutable/orEmpty(): kotlinx.collections.immutable/ImmutableList<#A> // kotlinx.collections.immutable/orEmpty|orEmpty@kotlinx.collections.immutable.ImmutableList<0:0>?(){0§}[0] +final inline fun <#A: kotlin/Any?> (kotlinx.collections.immutable/ImmutableSet<#A>?).kotlinx.collections.immutable/orEmpty(): kotlinx.collections.immutable/ImmutableSet<#A> // kotlinx.collections.immutable/orEmpty|orEmpty@kotlinx.collections.immutable.ImmutableSet<0:0>?(){0§}[0] 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|minus@kotlinx.collections.immutable.PersistentCollection<0:0>(0:0){0§}[0] 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|plus@kotlinx.collections.immutable.PersistentCollection<0:0>(0:0){0§}[0] 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|minus@kotlinx.collections.immutable.PersistentList<0:0>(0:0){0§}[0] diff --git a/core/commonMain/src/extensions.kt b/core/commonMain/src/extensions.kt index 57d289fa..96dbe636 100644 --- a/core/commonMain/src/extensions.kt +++ b/core/commonMain/src/extensions.kt @@ -30,6 +30,11 @@ import kotlinx.collections.immutable.implementations.persistentOrderedSet.Persis */ public inline fun PersistentSet.mutate(mutator: (MutableSet) -> Unit): PersistentSet = builder().apply(mutator).build() +/** + * Returns this [ImmutableSet] if it's not `null` and the empty [ImmutableSet] otherwise. + */ +public inline fun ImmutableSet?.orEmpty(): ImmutableSet = this ?: persistentSetOf() + /** * Returns the result of applying the provided modifications on this list. * @@ -52,6 +57,10 @@ public inline fun PersistentList.mutate(mutator: (MutableList) -> Unit public inline fun PersistentMap.mutate(mutator: (MutableMap) -> Unit): PersistentMap = (this as PersistentMap).builder().apply(mutator).build() +/** + * Returns this [ImmutableList] if it's not `null` and the empty [ImmutableList] otherwise. + */ +public inline fun ImmutableList?.orEmpty(): ImmutableList = this ?: persistentListOf() /** * Returns the result of adding the specified [element] to this collection. @@ -440,6 +449,10 @@ public operator fun PersistentMap.minus(keys: Array): Pe public operator fun PersistentMap.minus(keys: Sequence): PersistentMap = mutate { it.minusAssign(keys) } +/** + * Returns the [ImmutableMap] if it's not `null`, or the empty [ImmutableMap] otherwise. + */ +public fun ImmutableMap?.orEmpty(): ImmutableMap = this ?: persistentMapOf() /** * Returns a new persistent list of the specified elements. diff --git a/core/commonTest/src/contract/list/ImmutableListTest.kt b/core/commonTest/src/contract/list/ImmutableListTest.kt index 76883a04..178aebe2 100644 --- a/core/commonTest/src/contract/list/ImmutableListTest.kt +++ b/core/commonTest/src/contract/list/ImmutableListTest.kt @@ -224,4 +224,12 @@ class ImmutableListTest { assertEquals>(listOf("x", null, 1), listAny) } + + @Test fun immutableListOrEmpty() { + val emptyList = (null as ImmutableList?).orEmpty() + val notEmptyList = persistentListOf("a") + + assertEquals(emptyList, persistentListOf()) + assertEquals(notEmptyList, notEmptyList.orEmpty()) + } } \ No newline at end of file diff --git a/core/commonTest/src/contract/map/ImmutableMapTest.kt b/core/commonTest/src/contract/map/ImmutableMapTest.kt index 599f2c1c..0f53208b 100644 --- a/core/commonTest/src/contract/map/ImmutableMapTest.kt +++ b/core/commonTest/src/contract/map/ImmutableMapTest.kt @@ -392,4 +392,13 @@ abstract class ImmutableMapTest { testEquality(data, changed) } + + @Test + fun immutableMapOrEmpty() { + val emptyMap = (null as PersistentMap?).orEmpty() + val notEmptyMap = persistentMapOf("a" to 1) + + assertEquals(emptyMap, persistentMapOf()) + assertEquals(notEmptyMap, notEmptyMap.orEmpty()) + } } \ No newline at end of file diff --git a/core/commonTest/src/contract/set/ImmutableSetTest.kt b/core/commonTest/src/contract/set/ImmutableSetTest.kt index f017ccae..8488d1cb 100644 --- a/core/commonTest/src/contract/set/ImmutableSetTest.kt +++ b/core/commonTest/src/contract/set/ImmutableSetTest.kt @@ -404,4 +404,13 @@ abstract class ImmutableSetTestBase { testEquality(data, changed) } + + @Test + fun immutableSetOrEmpty() { + val emptySet = (null as ImmutableSet?).orEmpty() + val notEmptySet = persistentSetOf("a") + + assertEquals(emptySet, persistentSetOf()) + assertEquals(notEmptySet, notEmptySet.orEmpty()) + } } \ No newline at end of file