Skip to content

Commit

Permalink
mark also list and set generic with out
Browse files Browse the repository at this point in the history
(and a bit of linting)
  • Loading branch information
balazstothofficial committed Apr 8, 2021
1 parent 90f4c5e commit 6f5be1e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ interface NonEmptyCollection<out T> : Collection<T> {
message = "Alternative is never used!",
replaceWith = ReplaceWith("first()")
)

fun <T> NonEmptyCollection<T>.firstOr(alternative: () -> T): T = first()
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ operator fun <T> NonEmptyList<T>.plus(value: T): NonEmptyList<T> = full + value

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

operator fun <T> NonEmptyList<T>.plus(other: Iterable<T>): NonEmptyList<T> = NonEmptyList(full.stdPlus(other))
operator fun <T> NonEmptyList<T>.plus(
other: Iterable<T>
): NonEmptyList<T> = NonEmptyList(full.stdPlus(other))

operator fun <T> List<T>.plus(
other: NonEmptyList<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@ package com.quickbirdstudios.nonEmptyCollection.list

import com.quickbirdstudios.nonEmptyCollection.NonEmptyCollection

class NonEmptyList<T> internal constructor(internal val full: List<T>) : List<T> by full, NonEmptyCollection<T> {
constructor(
class NonEmptyList<out T> internal constructor(
internal val full: List<T>
) : List<T> by full, NonEmptyCollection<T> {

internal constructor(
head: T,
tail: List<T>
) : this(ArrayList<T>(tail.size + 1).apply { add(head); addAll(tail) })
) : this(
full = ArrayList<T>(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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import kotlin.collections.plus as stdPlus

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

operator fun <K, V> Map<K, V>.plus(entry: Pair<K, V>): NonEmptyMap<K, V> = NonEmptyMap(this.stdPlus(entry))
operator fun <K, V> Map<K, V>.plus(
entry: Pair<K, V>
): NonEmptyMap<K, V> = NonEmptyMap(this.stdPlus(entry))

operator fun <K, V> NonEmptyMap<K, V>.plus(other: Map<K, V>): NonEmptyMap<K, V> = NonEmptyMap(full.stdPlus(other))
operator fun <K, V> NonEmptyMap<K, V>.plus(
other: Map<K, V>
): NonEmptyMap<K, V> = NonEmptyMap(full.stdPlus(other))

operator fun <K, V> Map<K, V>.plus(
other: NonEmptyMap<K, V>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package com.quickbirdstudios.nonEmptyCollection.map

class NonEmptyMap<K, out V> internal constructor(internal val full: Map<K, V>) : Map<K, V> by full {
constructor(
class NonEmptyMap<K, out V> internal constructor(
internal val full: Map<K, V>
) : Map<K, V> by full {

internal constructor(
first: Pair<K, V>,
rest: Map<K, V>
) : this(LinkedHashMap<K, V>(rest.size + 1).apply { put(first.first, first.second); putAll(rest) })
) : this(
full = LinkedHashMap<K, V>(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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package com.quickbirdstudios.nonEmptyCollection.set

import com.quickbirdstudios.nonEmptyCollection.NonEmptyCollection

class NonEmptySet<T> internal constructor(
class NonEmptySet<out T> internal constructor(
internal val full: Set<T>
) : Set<T> by full, NonEmptyCollection<T> {
constructor(

internal constructor(
first: T,
rest: Set<T>
) : this(HashSet<T>(rest.size + 1).apply { add(first); addAll(rest) })
) : this(
full = HashSet<T>(rest.size + 1).apply {
add(first)
addAll(rest)
}
)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package com.quickbirdstudios.nonEmptyCollection.unsafe


import com.quickbirdstudios.nonEmptyCollection.NonEmptyCollection
import com.quickbirdstudios.nonEmptyCollection.list.NonEmptyList
import kotlin.experimental.ExperimentalTypeInference
Expand Down

0 comments on commit 6f5be1e

Please sign in to comment.