stdlib: Introduced filterIsInstance version
without class-literal as a parameter
This commit is contained in:
committed by
Andrey Breslav
parent
da5164acd8
commit
e768b4e285
@@ -36,6 +36,7 @@ class GenericFunction(val signature: String) : Comparable<GenericFunction> {
|
||||
var toNullableT: Boolean = false
|
||||
|
||||
var defaultInline = false
|
||||
var receiverAsterisk = false
|
||||
val inlineFamilies = HashMap<Family, Boolean>()
|
||||
|
||||
val buildFamilies = HashSet<Family>(defaultFamilies.toList())
|
||||
@@ -104,6 +105,10 @@ class GenericFunction(val signature: String) : Comparable<GenericFunction> {
|
||||
typeParams.add(t)
|
||||
}
|
||||
|
||||
fun receiverAsterisk(v: Boolean) {
|
||||
receiverAsterisk = true
|
||||
}
|
||||
|
||||
fun inline(value: Boolean, vararg families: Family) {
|
||||
if (families.isEmpty())
|
||||
defaultInline = value
|
||||
@@ -157,13 +162,14 @@ class GenericFunction(val signature: String) : Comparable<GenericFunction> {
|
||||
if (returnType.isEmpty())
|
||||
throw RuntimeException("No return type specified for $signature")
|
||||
|
||||
val isAsteriskOrT = if (receiverAsterisk) "*" else "T"
|
||||
val receiver = when (f) {
|
||||
Iterables -> "Iterable<T>"
|
||||
Collections -> "Collection<T>"
|
||||
Lists -> "List<T>"
|
||||
Iterables -> "Iterable<$isAsteriskOrT>"
|
||||
Collections -> "Collection<$isAsteriskOrT>"
|
||||
Lists -> "List<$isAsteriskOrT>"
|
||||
Maps -> "Map<K, V>"
|
||||
Streams -> "Stream<T>"
|
||||
ArraysOfObjects -> "Array<T>"
|
||||
Streams -> "Stream<$isAsteriskOrT>"
|
||||
ArraysOfObjects -> "Array<$isAsteriskOrT>"
|
||||
Strings -> "String"
|
||||
ArraysOfPrimitives -> primitive?.let { it.name() + "Array" } ?: throw IllegalArgumentException("Primitive array should specify primitive type")
|
||||
else -> throw IllegalStateException("Invalid family")
|
||||
@@ -215,7 +221,7 @@ class GenericFunction(val signature: String) : Comparable<GenericFunction> {
|
||||
if (primitive == null && f != Strings) {
|
||||
val implicitTypeParameters = receiver.dropWhile { it != '<' }.drop(1).filterNot { it == ' ' }.takeWhile { it != '>' }.split(",")
|
||||
for (implicit in implicitTypeParameters.reverse()) {
|
||||
if (!types.any { it.startsWith(implicit) }) {
|
||||
if (implicit != "*" && !types.any { it.startsWith(implicit) || it.startsWith("reified " + implicit) }) {
|
||||
types.add(0, implicit)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,5 +107,45 @@ fun specialJVM(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("filterIsInstanceTo(destination: C)") {
|
||||
doc { "Appends all elements that are instances of specified type parameter R to the given *destination*" }
|
||||
typeParam("reified R")
|
||||
typeParam("C : MutableCollection<in R>")
|
||||
inline(true)
|
||||
receiverAsterisk(true)
|
||||
returns("C")
|
||||
exclude(ArraysOfPrimitives, Strings)
|
||||
body {
|
||||
"""
|
||||
for (element in this) if (element is R) destination.add(element)
|
||||
return destination
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("filterIsInstance()") {
|
||||
doc { "Returns a list containing all elements that are instances of specified type parameter R" }
|
||||
typeParam("reified R")
|
||||
returns("List<R>")
|
||||
inline(true)
|
||||
receiverAsterisk(true)
|
||||
body {
|
||||
"""
|
||||
return filterIsInstanceTo(ArrayList<R>())
|
||||
"""
|
||||
}
|
||||
exclude(ArraysOfPrimitives, Strings)
|
||||
|
||||
doc(Streams) { "Returns a stream containing all elements that are instances of specified type parameter R" }
|
||||
returns(Streams) { "Stream<R>" }
|
||||
inline(true)
|
||||
receiverAsterisk(true)
|
||||
body(Streams) {
|
||||
"""
|
||||
return FilteringStream(this, true, { it is R }) as Stream<R>
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
return templates
|
||||
}
|
||||
Reference in New Issue
Block a user