Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add orEmpty extensions for immutable collections #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions core/api/kotlinx-collections-immutable.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -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§<kotlin.Any?>;1§<kotlin.Any?>}[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§<kotlin.Any?>;1§<kotlin.Any?>}[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§<kotlin.Any?>;1§<kotlin.Any?>}[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§<kotlin.Any?>;1§<kotlin.Any?>}[0]
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|minus@kotlinx.collections.immutable.PersistentMap<out|0:0,0:1>(0:0){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
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|minus@kotlinx.collections.immutable.PersistentMap<out|0:0,0:1>(kotlin.collections.Iterable<0:0>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
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|minus@kotlinx.collections.immutable.PersistentMap<out|0:0,0:1>(kotlin.sequences.Sequence<0:0>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
Expand Down Expand Up @@ -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<out #A, #B>).kotlinx.collections.immutable/plus(kotlin.sequences/Sequence<kotlin/Pair<#A, #B>>): kotlinx.collections.immutable/PersistentMap<#A, #B> // kotlinx.collections.immutable/plus|plus@kotlinx.collections.immutable.PersistentMap<out|0:0,0:1>(kotlin.sequences.Sequence<kotlin.Pair<0:0,0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
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|plus@kotlinx.collections.immutable.PersistentMap<out|0:0,0:1>(kotlin.Array<out|kotlin.Pair<0:0,0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
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|plus@kotlinx.collections.immutable.PersistentMap<out|0:0,0:1>(kotlin.Pair<0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}[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§<kotlin.Any?>}[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§<kotlin.Any?>}[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§<kotlin.Any?>}[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§<kotlin.Any?>}[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§<kotlin.Any?>}[0]
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())
}
}