#KT-1788 Fixed - added standard collection APIs to the various kinds of Array and removed the few old hand-crafted versions of these methods
This commit is contained in:
@@ -162,7 +162,7 @@ class CollectionTest {
|
||||
|
||||
test fun flatMap() {
|
||||
val data = arrayList("", "foo", "bar", "x", "")
|
||||
val characters = data.flatMap<String,Character>{ it.toCharList() }
|
||||
val characters = data.flatMap<String,Char>{ it.toCharList() }
|
||||
println("Got list of characters ${characters}")
|
||||
assertEquals(7, characters.size())
|
||||
val text = characters.makeString("")
|
||||
|
||||
@@ -48,6 +48,8 @@ fun main(args: Array<String>) {
|
||||
}
|
||||
val outDir = File(srcDir, "../generated")
|
||||
|
||||
val otherArrayNames = arrayList("Boolean", "Byte", "Char", "Short", "Int", "Long", "Float", "Double")
|
||||
|
||||
// JLangIterables - Generic iterable stuff
|
||||
generateFile(File(outDir, "ArraysFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt")) {
|
||||
it.replaceAll("java.lang.Iterable<T", "Array<T").replaceAll("java.lang.Iterable<T", "Array<T")
|
||||
@@ -55,6 +57,21 @@ fun main(args: Array<String>) {
|
||||
generateFile(File(outDir, "ArraysFromJLangIterablesLazy.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterablesLazy.kt")) {
|
||||
it.replaceAll("java.lang.Iterable<T", "Array<T").replaceAll("java.lang.Iterable<T", "Array<T")
|
||||
}
|
||||
for (arrayName in otherArrayNames) {
|
||||
fun replace(it: String): String {
|
||||
replaceGenerics(arrayName, it.replaceAll("<T> java.lang.Iterable<T>", "${arrayName}Array").
|
||||
replaceAll("<T> java.lang.Iterable<T\\?>", "${arrayName}Array").
|
||||
replaceAll("java.lang.Iterable<T\\?>", "${arrayName}Array").
|
||||
replaceAll("java.lang.Iterable<T>", "${arrayName}Array"))
|
||||
}
|
||||
|
||||
generateFile(File(outDir, "${arrayName}ArraysFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt")) {
|
||||
replace(it)
|
||||
}
|
||||
generateFile(File(outDir, "${arrayName}ArraysFromJLangIterablesLazy.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterablesLazy.kt")) {
|
||||
replace(it)
|
||||
}
|
||||
}
|
||||
|
||||
generateFile(File(outDir, "StandardFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt")) {
|
||||
it.replaceAll("java.lang.Iterable<T", "Iterable<T")
|
||||
@@ -69,10 +86,15 @@ fun main(args: Array<String>) {
|
||||
|
||||
|
||||
// JUtilCollections - methods returning a collection of the same input size (if its a collection)
|
||||
|
||||
generateFile(File(outDir, "ArraysFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt")) {
|
||||
it.replaceAll("java.util.Collection<T", "Array<T")
|
||||
}
|
||||
for (arrayName in otherArrayNames) {
|
||||
generateFile(File(outDir, "${arrayName}ArraysFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt")) {
|
||||
replaceGenerics(arrayName, it.replaceAll("<T> java.util.Collection<T>", "${arrayName}Array").
|
||||
replaceAll("java.util.Collection<T>", "${arrayName}Array"))
|
||||
}
|
||||
}
|
||||
|
||||
generateFile(File(outDir, "JUtilIterablesFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt")) {
|
||||
it.replaceAll("java.util.Collection<T", "java.lang.Iterable<T").replaceAll("(this.size)", "")
|
||||
@@ -82,3 +104,15 @@ fun main(args: Array<String>) {
|
||||
it.replaceAll("java.util.Collection<T", "Iterable<T").replaceAll("(this.size)", "")
|
||||
}
|
||||
}
|
||||
|
||||
// Pretty hacky way to code generate; ideally we'd be using the AST and just changing the function prototypes
|
||||
fun replaceGenerics(arrayName: String, it: String): String {
|
||||
return it.replaceAll(" <in T>", " ").replaceAll("<in T, ", "<").replaceAll("<T, ", "<").replaceAll("<T,", "<").
|
||||
replaceAll(" <T> ", " ").
|
||||
replaceAll("<T>", "<${arrayName}>").replaceAll("<in T>", "<${arrayName}>").
|
||||
replaceAll("\\(T\\)", "(${arrayName})").replaceAll("T\\?", "${arrayName}?").
|
||||
replaceAll("T,", "${arrayName},").
|
||||
replaceAll("T\\)", "${arrayName})").
|
||||
replaceAll(" T ", " ${arrayName} ")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user