toNotEmptySet

@Since(version = KotoolsTypesVersion.V4_0_0)
fun <E> Collection<E>.toNotEmptySet(): Result<NotEmptySet<E>>

Returns an encapsulated NotEmptySet containing all the elements of this collection, or returns an encapsulated IllegalArgumentException if this collection is empty.


Calling from Kotlin

Here's an example of calling this method from Kotlin code:

var collection: Collection<Int> = setOf(1, 2, 3, 1)
var notEmptySet: NotEmptySet<Int>? = collection.toNotEmptySet()
.getOrNull()
assertNotNull(notEmptySet)
collection = emptySet()
notEmptySet = collection.toNotEmptySet()
.getOrNull()
assertNull(notEmptySet)

Please note that changes made to the original collection will not be reflected on the resulting NotEmptySet.

val original: MutableCollection<Int> = mutableSetOf(1, 2, 3)
val notEmptySet: NotEmptySet<Int> = original.toNotEmptySet()
.getOrThrow()
assertEquals(expected = "$original", actual = "$notEmptySet")
original.clear()
assertNotEquals(illegal = "$original", actual = "$notEmptySet")