Provide toMutableList as a replacement for toArrayList.

This commit is contained in:
Ilya Gorbunov
2016-01-22 21:28:34 +03:00
parent a709ba4a6e
commit 90a239e74c
7 changed files with 184 additions and 38 deletions
@@ -32,7 +32,7 @@ fun ordering(): List<GenericFunction> {
body {
"""
if (this is Collection && isEmpty()) return emptyList()
val list = toArrayList()
val list = toMutableList()
Collections.reverse(list)
return list
"""
@@ -41,7 +41,7 @@ fun ordering(): List<GenericFunction> {
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
if (isEmpty()) return emptyList()
val list = toArrayList()
val list = toMutableList()
Collections.reverse(list)
return list
"""
@@ -97,10 +97,10 @@ fun ordering(): List<GenericFunction> {
body {
"""
if (this is Collection) {
if (size <= 1) return this.toArrayList()
if (size <= 1) return this.toMutableList()
return (toTypedArray<Comparable<T>>() as Array<T>).apply { sort() }.asList()
}
return toArrayList().apply { sort() }
return toMutableList().apply { sort() }
"""
}
body(ArraysOfPrimitives) {
@@ -122,7 +122,7 @@ fun ordering(): List<GenericFunction> {
"""
return object : Sequence<T> {
override fun iterator(): Iterator<T> {
val sortedList = this@sorted.toArrayList()
val sortedList = this@sorted.toMutableList()
sortedList.sort()
return sortedList.iterator()
}
@@ -226,10 +226,10 @@ fun ordering(): List<GenericFunction> {
body {
"""
if (this is Collection) {
if (size <= 1) return this.toArrayList()
if (size <= 1) return this.toMutableList()
return (toTypedArray<Any?>() as Array<T>).apply { sortWith(comparator) }.asList()
}
return toArrayList().apply { sortWith(comparator) }
return toMutableList().apply { sortWith(comparator) }
"""
}
body(ArraysOfPrimitives) {
@@ -251,7 +251,7 @@ fun ordering(): List<GenericFunction> {
"""
return object : Sequence<T> {
override fun iterator(): Iterator<T> {
val sortedList = this@sortedWith.toArrayList()
val sortedList = this@sortedWith.toMutableList()
sortedList.sortWith(comparator)
return sortedList.iterator()
}
@@ -81,6 +81,30 @@ fun snapshots(): List<GenericFunction> {
return list
"""
}
deprecate(Deprecation("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", "toCollection(arrayListOf())"))
}
templates add f("toMutableList()") {
doc { f -> "Returns a [MutableList] filled with all ${f.element.pluralize()} of this ${f.collection}." }
returns("MutableList<T>")
body { "return toCollection(ArrayList<T>())" }
body(Iterables) {
"""
if (this is Collection<T>)
return this.toMutableList()
return toCollection(ArrayList<T>())
"""
}
body(Collections) { "return ArrayList(this)" }
body(CharSequences) { "return toCollection(ArrayList<T>(length))" }
body(ArraysOfObjects) { "return ArrayList(this.asCollection())" }
body(ArraysOfPrimitives) {
"""
val list = ArrayList<T>(size)
for (item in this) list.add(item)
return list
"""
}
}
templates add f("toList()") {
@@ -101,7 +125,7 @@ fun snapshots(): List<GenericFunction> {
include(CharSequences)
doc { f -> "Returns a [List] containing all ${f.element.pluralize()}." }
returns("List<T>")
body { "return this.toArrayList()" }
body { "return this.toMutableList()" }
}
templates add f("toMap(transform: (T) -> Pair<K, V>)") {