Skip to content

Commit

Permalink
Add orEmpty extensions for immutable collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed May 15, 2024
1 parent 1d18389 commit d9aabf8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/api/kotlinx-collections-immutable.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions core/commonMain/src/extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ import kotlinx.collections.immutable.implementations.persistentOrderedSet.Persis
*/
public inline fun <T> PersistentSet<T>.mutate(mutator: (MutableSet<T>) -> Unit): PersistentSet<T> = builder().apply(mutator).build()

/**
* Returns this [ImmutableSet] if it's not `null` and the empty [ImmutableSet] otherwise.
*/
public inline fun <T> ImmutableSet<T>?.orEmpty(): ImmutableSet<T> = this ?: persistentSetOf()

/**
* Returns the result of applying the provided modifications on this list.
*
Expand All @@ -52,6 +57,10 @@ public inline fun <T> PersistentList<T>.mutate(mutator: (MutableList<T>) -> Unit
public inline fun <K, V> PersistentMap<out K, V>.mutate(mutator: (MutableMap<K, V>) -> Unit): PersistentMap<K, V> =
(this as PersistentMap<K, V>).builder().apply(mutator).build()

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

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

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

/**
* Returns a new persistent list of the specified elements.
Expand Down
8 changes: 8 additions & 0 deletions core/commonTest/src/contract/list/ImmutableListTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,12 @@ class ImmutableListTest {

assertEquals<List<*>>(listOf("x", null, 1), listAny)
}

@Test fun immutableListOrEmpty() {
val emptyList = (null as ImmutableList<String>?).orEmpty()
val notEmptyList = persistentListOf("a")

assertEquals(emptyList, persistentListOf())
assertEquals(notEmptyList, notEmptyList.orEmpty())
}
}
9 changes: 9 additions & 0 deletions core/commonTest/src/contract/map/ImmutableMapTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,13 @@ abstract class ImmutableMapTest {

testEquality(data, changed)
}

@Test
fun immutableMapOrEmpty() {
val emptyMap = (null as PersistentMap<String, Int>?).orEmpty()
val notEmptyMap = persistentMapOf("a" to 1)

assertEquals(emptyMap, persistentMapOf())
assertEquals(notEmptyMap, notEmptyMap.orEmpty())
}
}
9 changes: 9 additions & 0 deletions core/commonTest/src/contract/set/ImmutableSetTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,13 @@ abstract class ImmutableSetTestBase {

testEquality(data, changed)
}

@Test
fun immutableSetOrEmpty() {
val emptySet = (null as ImmutableSet<String>?).orEmpty()
val notEmptySet = persistentSetOf("a")

assertEquals(emptySet, persistentSetOf())
assertEquals(notEmptySet, notEmptySet.orEmpty())
}
}

0 comments on commit d9aabf8

Please sign in to comment.