Skip to content

Commit 9d50ad3

Browse files
directly wrap in non-empty Collection type
1 parent d7e2d9f commit 9d50ad3

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/set/NonEmptySet.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ class NonEmptySet<T> internal constructor(
1515
override fun equals(other: Any?): Boolean = full == other
1616

1717
override fun hashCode(): Int = full.hashCode()
18+
19+
init {
20+
require(full.isNotEmpty()) {
21+
"Fatal Error! This is a bug. Please contact the library author."
22+
}
23+
}
1824
}

src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/unsafe/toNonEmptyCollection.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import com.quickbirdstudios.nonEmptyCollection.set.nonEmptySetOf
99

1010
@UnsafeNonEmptyCollectionApi
1111
fun <T> Iterable<T>.toNonEmptyList(): NonEmptyList<T> = isAlreadyNonEmptyOr {
12-
nonEmptyListOf(first(), drop(1))
12+
NonEmptyList(toList())
1313
}
1414

1515
@UnsafeNonEmptyCollectionApi
@@ -19,8 +19,7 @@ fun <T> Iterable<T>.toNonEmptySet(): NonEmptySet<T> = isAlreadyNonEmptyOr {
1919

2020
@UnsafeNonEmptyCollectionApi
2121
fun <T> Set<T>.toNonEmptySet(): NonEmptySet<T> = isAlreadyNonEmptyOr {
22-
val first = first()
23-
nonEmptySetOf(first, this - first)
22+
NonEmptySet(this)
2423
}
2524

2625
@UnsafeNonEmptyCollectionApi
@@ -30,9 +29,7 @@ fun <K, V> Iterable<Pair<K, V>>.toNonEmptyMap(): NonEmptyMap<K, V> = isAlreadyNo
3029

3130
@UnsafeNonEmptyCollectionApi
3231
fun <K, V> Map<K, V>.toNonEmptyMap(): NonEmptyMap<K, V> = isAlreadyNonEmptyOr {
33-
val first = entries.first()
34-
35-
nonEmptyMapOf(first.key to first.value, this - first.key)
32+
NonEmptyMap(this)
3633
}
3734

3835
private inline fun <Empty, reified NonEmpty> Empty.isAlreadyNonEmptyOr(

src/jvmTest/kotlin/com/quickbirdstudios/nonEmptyCollection/unsafe/ToNonEmptyCollectionTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ToNonEmptyCollectionTest {
1515
@Test
1616
fun toNonEmptyList() {
1717
assertEquals(nonEmptyListOf(15, 2), listOf(15, 2).toNonEmptyList())
18-
assertThrows<NoSuchElementException> {
18+
assertThrows<IllegalArgumentException> {
1919
emptyList<String>().toNonEmptyList()
2020
}
2121
}
@@ -25,10 +25,10 @@ class ToNonEmptyCollectionTest {
2525
assertEquals(nonEmptySetOf(15, 2), listOf(15, 2).toNonEmptySet())
2626
assertEquals(nonEmptySetOf(15, 2), setOf(15, 2).toNonEmptySet())
2727

28-
assertThrows<NoSuchElementException> {
28+
assertThrows<IllegalArgumentException> {
2929
emptyList<String>().toNonEmptySet()
3030
}
31-
assertThrows<NoSuchElementException> {
31+
assertThrows<IllegalArgumentException> {
3232
emptySet<String>().toNonEmptySet()
3333
}
3434
}
@@ -44,10 +44,10 @@ class ToNonEmptyCollectionTest {
4444
mapOf(15 to 't', 2 to 'P').toNonEmptyMap()
4545
)
4646

47-
assertThrows<NoSuchElementException> {
47+
assertThrows<IllegalArgumentException> {
4848
emptyList<Pair<String, Float>>().toNonEmptyMap()
4949
}
50-
assertThrows<NoSuchElementException> {
50+
assertThrows<IllegalArgumentException> {
5151
emptyMap<String, Double>().toNonEmptyMap()
5252
}
5353
}

0 commit comments

Comments
 (0)