Skip to content

Commit 6f5be1e

Browse files
mark also list and set generic with out
(and a bit of linting)
1 parent 90f4c5e commit 6f5be1e

7 files changed

Lines changed: 47 additions & 15 deletions

File tree

src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/NonEmptyCollection.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ interface NonEmptyCollection<out T> : Collection<T> {
1515
message = "Alternative is never used!",
1616
replaceWith = ReplaceWith("first()")
1717
)
18+
1819
fun <T> NonEmptyCollection<T>.firstOr(alternative: () -> T): T = first()

src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/list/NonEmptyList+plus.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ operator fun <T> NonEmptyList<T>.plus(value: T): NonEmptyList<T> = full + value
66

77
operator fun <T> List<T>.plus(value: T): NonEmptyList<T> = NonEmptyList(this.stdPlus(value))
88

9-
operator fun <T> NonEmptyList<T>.plus(other: Iterable<T>): NonEmptyList<T> = NonEmptyList(full.stdPlus(other))
9+
operator fun <T> NonEmptyList<T>.plus(
10+
other: Iterable<T>
11+
): NonEmptyList<T> = NonEmptyList(full.stdPlus(other))
1012

1113
operator fun <T> List<T>.plus(
1214
other: NonEmptyList<T>

src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/list/NonEmptyList.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@ package com.quickbirdstudios.nonEmptyCollection.list
22

33
import com.quickbirdstudios.nonEmptyCollection.NonEmptyCollection
44

5-
class NonEmptyList<T> internal constructor(internal val full: List<T>) : List<T> by full, NonEmptyCollection<T> {
6-
constructor(
5+
class NonEmptyList<out T> internal constructor(
6+
internal val full: List<T>
7+
) : List<T> by full, NonEmptyCollection<T> {
8+
9+
internal constructor(
710
head: T,
811
tail: List<T>
9-
) : this(ArrayList<T>(tail.size + 1).apply { add(head); addAll(tail) })
12+
) : this(
13+
full = ArrayList<T>(tail.size + 1).apply {
14+
add(head)
15+
addAll(tail)
16+
}
17+
)
1018

1119
init {
12-
require(full.isNotEmpty()) { "Fatal Error! This is a bug. Please contact the library author." }
20+
require(full.isNotEmpty()) {
21+
"Fatal Error! This is a bug. Please contact the library author."
22+
}
1323
}
1424

1525
override fun toString(): String = full.toString()

src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/map/NonEmptyMap+plus.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import kotlin.collections.plus as stdPlus
44

55
operator fun <K, V> NonEmptyMap<K, V>.plus(entry: Pair<K, V>): NonEmptyMap<K, V> = full + entry
66

7-
operator fun <K, V> Map<K, V>.plus(entry: Pair<K, V>): NonEmptyMap<K, V> = NonEmptyMap(this.stdPlus(entry))
7+
operator fun <K, V> Map<K, V>.plus(
8+
entry: Pair<K, V>
9+
): NonEmptyMap<K, V> = NonEmptyMap(this.stdPlus(entry))
810

9-
operator fun <K, V> NonEmptyMap<K, V>.plus(other: Map<K, V>): NonEmptyMap<K, V> = NonEmptyMap(full.stdPlus(other))
11+
operator fun <K, V> NonEmptyMap<K, V>.plus(
12+
other: Map<K, V>
13+
): NonEmptyMap<K, V> = NonEmptyMap(full.stdPlus(other))
1014

1115
operator fun <K, V> Map<K, V>.plus(
1216
other: NonEmptyMap<K, V>

src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/map/NonEmptyMap.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package com.quickbirdstudios.nonEmptyCollection.map
22

3-
class NonEmptyMap<K, out V> internal constructor(internal val full: Map<K, V>) : Map<K, V> by full {
4-
constructor(
3+
class NonEmptyMap<K, out V> internal constructor(
4+
internal val full: Map<K, V>
5+
) : Map<K, V> by full {
6+
7+
internal constructor(
58
first: Pair<K, V>,
69
rest: Map<K, V>
7-
) : this(LinkedHashMap<K, V>(rest.size + 1).apply { put(first.first, first.second); putAll(rest) })
10+
) : this(
11+
full = LinkedHashMap<K, V>(rest.size + 1).apply {
12+
put(first.first, first.second)
13+
putAll(rest)
14+
}
15+
)
816

917
init {
10-
require(full.isNotEmpty()) { "Fatal Error! This is a bug. Please contact the library author." }
18+
require(full.isNotEmpty()) {
19+
"Fatal Error! This is a bug. Please contact the library author."
20+
}
1121
}
1222

1323
override fun toString(): String = full.toString()

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ package com.quickbirdstudios.nonEmptyCollection.set
22

33
import com.quickbirdstudios.nonEmptyCollection.NonEmptyCollection
44

5-
class NonEmptySet<T> internal constructor(
5+
class NonEmptySet<out T> internal constructor(
66
internal val full: Set<T>
77
) : Set<T> by full, NonEmptyCollection<T> {
8-
constructor(
8+
9+
internal constructor(
910
first: T,
1011
rest: Set<T>
11-
) : this(HashSet<T>(rest.size + 1).apply { add(first); addAll(rest) })
12+
) : this(
13+
full = HashSet<T>(rest.size + 1).apply {
14+
add(first)
15+
addAll(rest)
16+
}
17+
)
1218

1319
override fun toString(): String = full.toString()
1420

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
package com.quickbirdstudios.nonEmptyCollection.unsafe
44

5-
65
import com.quickbirdstudios.nonEmptyCollection.NonEmptyCollection
76
import com.quickbirdstudios.nonEmptyCollection.list.NonEmptyList
87
import kotlin.experimental.ExperimentalTypeInference

0 commit comments

Comments
 (0)