diff --git a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/NonEmptyCollection.kt b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/NonEmptyCollection.kt index 04cdff5..772a3e6 100644 --- a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/NonEmptyCollection.kt +++ b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/NonEmptyCollection.kt @@ -15,4 +15,5 @@ interface NonEmptyCollection : Collection { message = "Alternative is never used!", replaceWith = ReplaceWith("first()") ) + fun NonEmptyCollection.firstOr(alternative: () -> T): T = first() diff --git a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/list/NonEmptyList+plus.kt b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/list/NonEmptyList+plus.kt index 591a91f..0a2988b 100644 --- a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/list/NonEmptyList+plus.kt +++ b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/list/NonEmptyList+plus.kt @@ -6,7 +6,9 @@ operator fun NonEmptyList.plus(value: T): NonEmptyList = full + value operator fun List.plus(value: T): NonEmptyList = NonEmptyList(this.stdPlus(value)) -operator fun NonEmptyList.plus(other: Iterable): NonEmptyList = NonEmptyList(full.stdPlus(other)) +operator fun NonEmptyList.plus( + other: Iterable +): NonEmptyList = NonEmptyList(full.stdPlus(other)) operator fun List.plus( other: NonEmptyList diff --git a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/list/NonEmptyList.kt b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/list/NonEmptyList.kt index 769d9e8..d5166c7 100644 --- a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/list/NonEmptyList.kt +++ b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/list/NonEmptyList.kt @@ -2,14 +2,24 @@ package com.quickbirdstudios.nonEmptyCollection.list import com.quickbirdstudios.nonEmptyCollection.NonEmptyCollection -class NonEmptyList internal constructor(internal val full: List) : List by full, NonEmptyCollection { - constructor( +class NonEmptyList internal constructor( + internal val full: List +) : List by full, NonEmptyCollection { + + internal constructor( head: T, tail: List - ) : this(ArrayList(tail.size + 1).apply { add(head); addAll(tail) }) + ) : this( + full = ArrayList(tail.size + 1).apply { + add(head) + addAll(tail) + } + ) init { - require(full.isNotEmpty()) { "Fatal Error! This is a bug. Please contact the library author." } + require(full.isNotEmpty()) { + "Fatal Error! This is a bug. Please contact the library author." + } } override fun toString(): String = full.toString() diff --git a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/map/NonEmptyMap+plus.kt b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/map/NonEmptyMap+plus.kt index 0440be6..fc4da2a 100644 --- a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/map/NonEmptyMap+plus.kt +++ b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/map/NonEmptyMap+plus.kt @@ -4,9 +4,13 @@ import kotlin.collections.plus as stdPlus operator fun NonEmptyMap.plus(entry: Pair): NonEmptyMap = full + entry -operator fun Map.plus(entry: Pair): NonEmptyMap = NonEmptyMap(this.stdPlus(entry)) +operator fun Map.plus( + entry: Pair +): NonEmptyMap = NonEmptyMap(this.stdPlus(entry)) -operator fun NonEmptyMap.plus(other: Map): NonEmptyMap = NonEmptyMap(full.stdPlus(other)) +operator fun NonEmptyMap.plus( + other: Map +): NonEmptyMap = NonEmptyMap(full.stdPlus(other)) operator fun Map.plus( other: NonEmptyMap diff --git a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/map/NonEmptyMap.kt b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/map/NonEmptyMap.kt index 2654aaa..1921675 100644 --- a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/map/NonEmptyMap.kt +++ b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/map/NonEmptyMap.kt @@ -1,13 +1,23 @@ package com.quickbirdstudios.nonEmptyCollection.map -class NonEmptyMap internal constructor(internal val full: Map) : Map by full { - constructor( +class NonEmptyMap internal constructor( + internal val full: Map +) : Map by full { + + internal constructor( first: Pair, rest: Map - ) : this(LinkedHashMap(rest.size + 1).apply { put(first.first, first.second); putAll(rest) }) + ) : this( + full = LinkedHashMap(rest.size + 1).apply { + put(first.first, first.second) + putAll(rest) + } + ) init { - require(full.isNotEmpty()) { "Fatal Error! This is a bug. Please contact the library author." } + require(full.isNotEmpty()) { + "Fatal Error! This is a bug. Please contact the library author." + } } override fun toString(): String = full.toString() diff --git a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/set/NonEmptySet.kt b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/set/NonEmptySet.kt index 458479a..4bdedd8 100644 --- a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/set/NonEmptySet.kt +++ b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/set/NonEmptySet.kt @@ -2,13 +2,19 @@ package com.quickbirdstudios.nonEmptyCollection.set import com.quickbirdstudios.nonEmptyCollection.NonEmptyCollection -class NonEmptySet internal constructor( +class NonEmptySet internal constructor( internal val full: Set ) : Set by full, NonEmptyCollection { - constructor( + + internal constructor( first: T, rest: Set - ) : this(HashSet(rest.size + 1).apply { add(first); addAll(rest) }) + ) : this( + full = HashSet(rest.size + 1).apply { + add(first) + addAll(rest) + } + ) override fun toString(): String = full.toString() diff --git a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/unsafe/wrapListOperator.kt b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/unsafe/wrapListOperator.kt index cc66a27..3429bda 100644 --- a/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/unsafe/wrapListOperator.kt +++ b/src/commonMain/kotlin/com/quickbirdstudios/nonEmptyCollection/unsafe/wrapListOperator.kt @@ -2,7 +2,6 @@ package com.quickbirdstudios.nonEmptyCollection.unsafe - import com.quickbirdstudios.nonEmptyCollection.NonEmptyCollection import com.quickbirdstudios.nonEmptyCollection.list.NonEmptyList import kotlin.experimental.ExperimentalTypeInference