diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index dc0c923cc12..8d0cd371eaa 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -4378,7 +4378,7 @@ public fun ShortArray.reverse(): Unit { */ public fun Array.reversed(): List { if (isEmpty()) return emptyList() - val list = toArrayList() + val list = toMutableList() Collections.reverse(list) return list } @@ -4388,7 +4388,7 @@ public fun Array.reversed(): List { */ public fun BooleanArray.reversed(): List { if (isEmpty()) return emptyList() - val list = toArrayList() + val list = toMutableList() Collections.reverse(list) return list } @@ -4398,7 +4398,7 @@ public fun BooleanArray.reversed(): List { */ public fun ByteArray.reversed(): List { if (isEmpty()) return emptyList() - val list = toArrayList() + val list = toMutableList() Collections.reverse(list) return list } @@ -4408,7 +4408,7 @@ public fun ByteArray.reversed(): List { */ public fun CharArray.reversed(): List { if (isEmpty()) return emptyList() - val list = toArrayList() + val list = toMutableList() Collections.reverse(list) return list } @@ -4418,7 +4418,7 @@ public fun CharArray.reversed(): List { */ public fun DoubleArray.reversed(): List { if (isEmpty()) return emptyList() - val list = toArrayList() + val list = toMutableList() Collections.reverse(list) return list } @@ -4428,7 +4428,7 @@ public fun DoubleArray.reversed(): List { */ public fun FloatArray.reversed(): List { if (isEmpty()) return emptyList() - val list = toArrayList() + val list = toMutableList() Collections.reverse(list) return list } @@ -4438,7 +4438,7 @@ public fun FloatArray.reversed(): List { */ public fun IntArray.reversed(): List { if (isEmpty()) return emptyList() - val list = toArrayList() + val list = toMutableList() Collections.reverse(list) return list } @@ -4448,7 +4448,7 @@ public fun IntArray.reversed(): List { */ public fun LongArray.reversed(): List { if (isEmpty()) return emptyList() - val list = toArrayList() + val list = toMutableList() Collections.reverse(list) return list } @@ -4458,7 +4458,7 @@ public fun LongArray.reversed(): List { */ public fun ShortArray.reversed(): List { if (isEmpty()) return emptyList() - val list = toArrayList() + val list = toMutableList() Collections.reverse(list) return list } @@ -6019,6 +6019,7 @@ public inline fun > ShortArray.associateTo(dest /** * Returns an [ArrayList] of all elements. */ +@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())")) public fun Array.toArrayList(): ArrayList { return ArrayList(this.asCollection()) } @@ -6026,6 +6027,7 @@ public fun Array.toArrayList(): ArrayList { /** * Returns an [ArrayList] of all elements. */ +@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())")) public fun BooleanArray.toArrayList(): ArrayList { val list = ArrayList(size) for (item in this) list.add(item) @@ -6035,6 +6037,7 @@ public fun BooleanArray.toArrayList(): ArrayList { /** * Returns an [ArrayList] of all elements. */ +@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())")) public fun ByteArray.toArrayList(): ArrayList { val list = ArrayList(size) for (item in this) list.add(item) @@ -6044,6 +6047,7 @@ public fun ByteArray.toArrayList(): ArrayList { /** * Returns an [ArrayList] of all elements. */ +@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())")) public fun CharArray.toArrayList(): ArrayList { val list = ArrayList(size) for (item in this) list.add(item) @@ -6053,6 +6057,7 @@ public fun CharArray.toArrayList(): ArrayList { /** * Returns an [ArrayList] of all elements. */ +@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())")) public fun DoubleArray.toArrayList(): ArrayList { val list = ArrayList(size) for (item in this) list.add(item) @@ -6062,6 +6067,7 @@ public fun DoubleArray.toArrayList(): ArrayList { /** * Returns an [ArrayList] of all elements. */ +@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())")) public fun FloatArray.toArrayList(): ArrayList { val list = ArrayList(size) for (item in this) list.add(item) @@ -6071,6 +6077,7 @@ public fun FloatArray.toArrayList(): ArrayList { /** * Returns an [ArrayList] of all elements. */ +@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())")) public fun IntArray.toArrayList(): ArrayList { val list = ArrayList(size) for (item in this) list.add(item) @@ -6080,6 +6087,7 @@ public fun IntArray.toArrayList(): ArrayList { /** * Returns an [ArrayList] of all elements. */ +@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())")) public fun LongArray.toArrayList(): ArrayList { val list = ArrayList(size) for (item in this) list.add(item) @@ -6089,6 +6097,7 @@ public fun LongArray.toArrayList(): ArrayList { /** * Returns an [ArrayList] of all elements. */ +@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())")) public fun ShortArray.toArrayList(): ArrayList { val list = ArrayList(size) for (item in this) list.add(item) @@ -6252,63 +6261,63 @@ public fun ShortArray.toHashSet(): HashSet { * Returns a [List] containing all elements. */ public fun Array.toList(): List { - return this.toArrayList() + return this.toMutableList() } /** * Returns a [List] containing all elements. */ public fun BooleanArray.toList(): List { - return this.toArrayList() + return this.toMutableList() } /** * Returns a [List] containing all elements. */ public fun ByteArray.toList(): List { - return this.toArrayList() + return this.toMutableList() } /** * Returns a [List] containing all elements. */ public fun CharArray.toList(): List { - return this.toArrayList() + return this.toMutableList() } /** * Returns a [List] containing all elements. */ public fun DoubleArray.toList(): List { - return this.toArrayList() + return this.toMutableList() } /** * Returns a [List] containing all elements. */ public fun FloatArray.toList(): List { - return this.toArrayList() + return this.toMutableList() } /** * Returns a [List] containing all elements. */ public fun IntArray.toList(): List { - return this.toArrayList() + return this.toMutableList() } /** * Returns a [List] containing all elements. */ public fun LongArray.toList(): List { - return this.toArrayList() + return this.toMutableList() } /** * Returns a [List] containing all elements. */ public fun ShortArray.toList(): List { - return this.toArrayList() + return this.toMutableList() } /** @@ -6536,6 +6545,85 @@ public inline fun ShortArray.toMapBy(selector: (Short) -> K, transform: ( return associateBy(selector, transform) } +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun Array.toMutableList(): MutableList { + return ArrayList(this.asCollection()) +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun BooleanArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun ByteArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun CharArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun DoubleArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun FloatArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun IntArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun LongArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun ShortArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + /** * Returns a [Set] of all elements. */ diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index d41a3b52080..338bbb05cfc 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -756,7 +756,7 @@ public fun MutableList.reverse(): Unit { */ public fun Iterable.reversed(): List { if (this is Collection && isEmpty()) return emptyList() - val list = toArrayList() + val list = toMutableList() Collections.reverse(list) return list } @@ -787,10 +787,10 @@ public fun > MutableList.sortDescending(): Unit { */ public fun > Iterable.sorted(): List { if (this is Collection) { - if (size <= 1) return this.toArrayList() + if (size <= 1) return this.toMutableList() return (toTypedArray>() as Array).apply { sort() }.asList() } - return toArrayList().apply { sort() } + return toMutableList().apply { sort() } } /** @@ -819,10 +819,10 @@ public fun > Iterable.sortedDescending(): List { */ public fun Iterable.sortedWith(comparator: Comparator): List { if (this is Collection) { - if (size <= 1) return this.toArrayList() + if (size <= 1) return this.toMutableList() return (toTypedArray() as Array).apply { sortWith(comparator) }.asList() } - return toArrayList().apply { sortWith(comparator) } + return toMutableList().apply { sortWith(comparator) } } /** @@ -983,6 +983,7 @@ public inline fun > Iterable.associateTo( /** * Returns an [ArrayList] of all elements. */ +@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())")) public fun Collection.toArrayList(): ArrayList { return ArrayList(this) } @@ -990,6 +991,7 @@ public fun Collection.toArrayList(): ArrayList { /** * Returns an [ArrayList] of all elements. */ +@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())")) public fun Iterable.toArrayList(): ArrayList { if (this is Collection) return this.toArrayList() @@ -1017,7 +1019,7 @@ public fun Iterable.toHashSet(): HashSet { * Returns a [List] containing all elements. */ public fun Iterable.toList(): List { - return this.toArrayList() + return this.toMutableList() } /** @@ -1045,6 +1047,22 @@ public inline fun Iterable.toMapBy(selector: (T) -> K, transform: ( return associateBy(selector, transform) } +/** + * Returns a [MutableList] filled with all elements of this collection. + */ +public fun Collection.toMutableList(): MutableList { + return ArrayList(this) +} + +/** + * Returns a [MutableList] filled with all elements of this collection. + */ +public fun Iterable.toMutableList(): MutableList { + if (this is Collection) + return this.toMutableList() + return toCollection(ArrayList()) +} + /** * Returns a [Set] of all elements. */ diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 376afded6b2..01f6bc7cfcc 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -381,7 +381,7 @@ public fun Sequence.takeWhile(predicate: (T) -> Boolean): Sequence { public fun > Sequence.sorted(): Sequence { return object : Sequence { override fun iterator(): Iterator { - val sortedList = this@sorted.toArrayList() + val sortedList = this@sorted.toMutableList() sortedList.sort() return sortedList.iterator() } @@ -415,7 +415,7 @@ public fun > Sequence.sortedDescending(): Sequence { public fun Sequence.sortedWith(comparator: Comparator): Sequence { return object : Sequence { override fun iterator(): Iterator { - val sortedList = this@sortedWith.toArrayList() + val sortedList = this@sortedWith.toMutableList() sortedList.sortWith(comparator) return sortedList.iterator() } @@ -489,6 +489,7 @@ public inline fun > Sequence.associateTo( /** * Returns an [ArrayList] of all elements. */ +@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())")) public fun Sequence.toArrayList(): ArrayList { return toCollection(ArrayList()) } @@ -514,7 +515,7 @@ public fun Sequence.toHashSet(): HashSet { * Returns a [List] containing all elements. */ public fun Sequence.toList(): List { - return this.toArrayList() + return this.toMutableList() } /** @@ -542,6 +543,13 @@ public inline fun Sequence.toMapBy(selector: (T) -> K, transform: ( return associateBy(selector, transform) } +/** + * Returns a [MutableList] filled with all elements of this sequence. + */ +public fun Sequence.toMutableList(): MutableList { + return toCollection(ArrayList()) +} + /** * Returns a [Set] of all elements. */ diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index 42b680d008f..2ea27e8f455 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -561,6 +561,7 @@ public inline fun > CharSequence.associateTo(de /** * Returns an [ArrayList] of all characters. */ +@Deprecated("Use toMutableList instead or toCollection(ArrayList()) if you need ArrayList's ensureCapacity and trimToSize.", ReplaceWith("toCollection(arrayListOf())")) public fun CharSequence.toArrayList(): ArrayList { return toCollection(ArrayList(length)) } @@ -586,7 +587,7 @@ public fun CharSequence.toHashSet(): HashSet { * Returns a [List] containing all characters. */ public fun CharSequence.toList(): List { - return this.toArrayList() + return this.toMutableList() } /** @@ -614,6 +615,13 @@ public inline fun CharSequence.toMapBy(selector: (Char) -> K, transform: return associateBy(selector, transform) } +/** + * Returns a [MutableList] filled with all characters of this char sequence. + */ +public fun CharSequence.toMutableList(): MutableList { + return toCollection(ArrayList(length)) +} + /** * Returns a [Set] of all characters. */ diff --git a/libraries/stdlib/test/collections/ReversedViewsTest.kt b/libraries/stdlib/test/collections/ReversedViewsTest.kt index 41265807566..84b80479a17 100644 --- a/libraries/stdlib/test/collections/ReversedViewsTest.kt +++ b/libraries/stdlib/test/collections/ReversedViewsTest.kt @@ -79,7 +79,7 @@ class ReversedViewsTest { } @test fun testMutableReversedSubList() { - val reversed = (1..10).toArrayList().asReversed() + val reversed = (1..10).toMutableList().asReversed() assertEquals(listOf(9, 8, 7), reversed.subList(1, 4)) } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt index b4f7119f3b9..0dc990201c4 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt @@ -32,7 +32,7 @@ fun ordering(): List { 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 { 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 { body { """ if (this is Collection) { - if (size <= 1) return this.toArrayList() + if (size <= 1) return this.toMutableList() return (toTypedArray>() as Array).apply { sort() }.asList() } - return toArrayList().apply { sort() } + return toMutableList().apply { sort() } """ } body(ArraysOfPrimitives) { @@ -122,7 +122,7 @@ fun ordering(): List { """ return object : Sequence { override fun iterator(): Iterator { - val sortedList = this@sorted.toArrayList() + val sortedList = this@sorted.toMutableList() sortedList.sort() return sortedList.iterator() } @@ -226,10 +226,10 @@ fun ordering(): List { body { """ if (this is Collection) { - if (size <= 1) return this.toArrayList() + if (size <= 1) return this.toMutableList() return (toTypedArray() as Array).apply { sortWith(comparator) }.asList() } - return toArrayList().apply { sortWith(comparator) } + return toMutableList().apply { sortWith(comparator) } """ } body(ArraysOfPrimitives) { @@ -251,7 +251,7 @@ fun ordering(): List { """ return object : Sequence { override fun iterator(): Iterator { - val sortedList = this@sortedWith.toArrayList() + val sortedList = this@sortedWith.toMutableList() sortedList.sortWith(comparator) return sortedList.iterator() } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index 0b5cf676c8b..295f211aaa9 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -81,6 +81,30 @@ fun snapshots(): List { 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") + body { "return toCollection(ArrayList())" } + body(Iterables) { + """ + if (this is Collection) + return this.toMutableList() + return toCollection(ArrayList()) + """ + } + body(Collections) { "return ArrayList(this)" } + body(CharSequences) { "return toCollection(ArrayList(length))" } + body(ArraysOfObjects) { "return ArrayList(this.asCollection())" } + body(ArraysOfPrimitives) { + """ + val list = ArrayList(size) + for (item in this) list.add(item) + return list + """ + } } templates add f("toList()") { @@ -101,7 +125,7 @@ fun snapshots(): List { include(CharSequences) doc { f -> "Returns a [List] containing all ${f.element.pluralize()}." } returns("List") - body { "return this.toArrayList()" } + body { "return this.toMutableList()" } } templates add f("toMap(transform: (T) -> Pair)") {