createOrNull

@ExperimentalSince(version = KotoolsTypesVersion.V4_5_0)
fun <K, V> createOrNull(map: Map<K, V>): NotEmptyMap<K, V>?

Creates a NotEmptyMap containing all the entries of the specified map, or returns null if the map is empty.

Here's an example for calling this function from Kotlin code:

val map: Map<Char, Int> = mapOf('a' to 1, 'b' to 2)
val result: NotEmptyMap<Char, Int>? = NotEmptyMap.createOrNull(map)
println(result) // Success({a=1, b=2})

The NotEmptyMap type being an inline value class, this function is not available yet for Java users.

Please note that changes made to the original map will not be reflected on the resulting NotEmptyMap.

val original: MutableMap<Char, Int> =
mutableMapOf('a' to 1, 'b' to 2)
val notEmptyMap: NotEmptyMap<Char, Int>? =
NotEmptyMap.createOrNull(original)
println(original) // {a=1, b=2}
println(notEmptyMap) // {a=1, b=2}

original.clear()
println(original) // {}
println(notEmptyMap) // {a=1, b=2}

You can use the NotEmptyMap.Companion.create function for throwing an exception instead of returning null in case of invalid map.