From fe8ba4d3563ee9a0f81892f0345ef6aac2c73870 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sat, 16 Jan 2016 05:14:28 +0300 Subject: [PATCH] Provide mutableListOf. Deprecate linkedListOf. #KT-9663 --- .../stdlib/src/kotlin/collections/JUtil.kt | 9 ++++++ .../test/collections/CollectionJVMTest.kt | 18 +++++------ .../test/collections/IterableJVMTests.kt | 2 +- .../test/collections/ReversedViewsTest.kt | 32 +++++++++---------- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/libraries/stdlib/src/kotlin/collections/JUtil.kt b/libraries/stdlib/src/kotlin/collections/JUtil.kt index ee9c0599026..e0f4fb0eff9 100644 --- a/libraries/stdlib/src/kotlin/collections/JUtil.kt +++ b/libraries/stdlib/src/kotlin/collections/JUtil.kt @@ -74,9 +74,18 @@ public fun listOf(element: T): List = Collections.singletonList(element) /** Returns a new [LinkedList] with the given elements. */ @JvmVersion +@Deprecated("Use LinkedList constructor.", ReplaceWith("LinkedList(listOf(*elements))", "java.util.LinkedList")) public fun linkedListOf(vararg elements: T): LinkedList = if (elements.size == 0) LinkedList() else LinkedList(ArrayAsCollection(elements)) +@Deprecated("Use LinkedList constructor.", ReplaceWith("LinkedList()", "java.util.LinkedList")) +public fun linkedListOf() = LinkedList() + + +/** Returns a new [MutableList] with the given elements. */ +public fun mutableListOf(vararg elements: T): MutableList + = if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements)) + /** Returns a new [ArrayList] with the given elements. */ public fun arrayListOf(vararg elements: T): ArrayList = if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements)) diff --git a/libraries/stdlib/test/collections/CollectionJVMTest.kt b/libraries/stdlib/test/collections/CollectionJVMTest.kt index fa9e13e5a52..093c90c824b 100644 --- a/libraries/stdlib/test/collections/CollectionJVMTest.kt +++ b/libraries/stdlib/test/collections/CollectionJVMTest.kt @@ -47,13 +47,13 @@ class CollectionJVMTest { @test fun filterIntoLinkedList() { val data = listOf("foo", "bar") - val foo = data.filterTo(linkedListOf()) { it.startsWith("f") } + val foo = data.filterTo(LinkedList()) { it.startsWith("f") } assertTrue { foo.all { it.startsWith("f") } } assertEquals(1, foo.size) - assertEquals(linkedListOf("foo"), foo) + assertEquals(listOf("foo"), foo) assertTrue { foo is LinkedList @@ -62,13 +62,13 @@ class CollectionJVMTest { @test fun filterNotIntoLinkedListOf() { val data = listOf("foo", "bar") - val foo = data.filterNotTo(linkedListOf()) { it.startsWith("f") } + val foo = data.filterNotTo(LinkedList()) { it.startsWith("f") } assertTrue { foo.all { !it.startsWith("f") } } assertEquals(1, foo.size) - assertEquals(linkedListOf("bar"), foo) + assertEquals(listOf("bar"), foo) assertTrue { foo is LinkedList @@ -77,10 +77,10 @@ class CollectionJVMTest { @test fun filterNotNullIntoLinkedListOf() { val data = listOf(null, "foo", null, "bar") - val foo = data.filterNotNullTo(linkedListOf()) + val foo = data.filterNotNullTo(LinkedList()) assertEquals(2, foo.size) - assertEquals(linkedListOf("foo", "bar"), foo) + assertEquals(LinkedList(listOf("foo", "bar")), foo) assertTrue { foo is LinkedList @@ -105,15 +105,15 @@ class CollectionJVMTest { val data = listOf("foo", "bar") assertEquals("bar", data.last()) assertEquals(25, listOf(15, 19, 20, 25).last()) - assertEquals('a', linkedListOf('a').last()) + assertEquals('a', LinkedList(listOf('a')).last()) } @test fun lastException() { - assertFails { linkedListOf().last() } + assertFails { LinkedList().last() } } @test fun contains() { - assertTrue(linkedListOf(15, 19, 20).contains(15)) + assertTrue(LinkedList(listOf(15, 19, 20)).contains(15)) } @test fun toArray() { diff --git a/libraries/stdlib/test/collections/IterableJVMTests.kt b/libraries/stdlib/test/collections/IterableJVMTests.kt index eb826b466b8..1a48930275d 100644 --- a/libraries/stdlib/test/collections/IterableJVMTests.kt +++ b/libraries/stdlib/test/collections/IterableJVMTests.kt @@ -2,5 +2,5 @@ package test.collections import java.util.* -class LinkedListTest : OrderedIterableTests>(linkedListOf("foo", "bar"), linkedListOf()) +class LinkedListTest : OrderedIterableTests>(LinkedList(listOf("foo", "bar")), LinkedList()) diff --git a/libraries/stdlib/test/collections/ReversedViewsTest.kt b/libraries/stdlib/test/collections/ReversedViewsTest.kt index 4da1dbb00a6..41265807566 100644 --- a/libraries/stdlib/test/collections/ReversedViewsTest.kt +++ b/libraries/stdlib/test/collections/ReversedViewsTest.kt @@ -65,17 +65,17 @@ class ReversedViewsTest { } @test fun testMutableSimple() { - assertEquals(listOf(3, 2, 1), arrayListOf(1, 2, 3).asReversed()) - assertEquals(listOf(3, 2, 1), arrayListOf(1, 2, 3).asReversed().toList()) + assertEquals(listOf(3, 2, 1), mutableListOf(1, 2, 3).asReversed()) + assertEquals(listOf(3, 2, 1), mutableListOf(1, 2, 3).asReversed().toList()) } @test fun testMutableDoubleReverse() { - assertEquals(listOf(1, 2, 3), arrayListOf(1, 2, 3).asReversed().asReversed()) - assertEquals(listOf(2, 3), arrayListOf(1, 2, 3, 4).asReversed().subList(1, 3).asReversed()) + assertEquals(listOf(1, 2, 3), mutableListOf(1, 2, 3).asReversed().asReversed()) + assertEquals(listOf(2, 3), mutableListOf(1, 2, 3, 4).asReversed().subList(1, 3).asReversed()) } @test fun testMutableEmpty() { - assertEquals(emptyList(), arrayListOf().asReversed()) + assertEquals(emptyList(), mutableListOf().asReversed()) } @test fun testMutableReversedSubList() { @@ -84,7 +84,7 @@ class ReversedViewsTest { } @test fun testMutableAdd() { - val original = arrayListOf(1, 2, 3) + val original = mutableListOf(1, 2, 3) val reversed = original.asReversed() reversed.add(0) // add zero at end of reversed @@ -97,7 +97,7 @@ class ReversedViewsTest { } @test fun testMutableSet() { - val original = arrayListOf(1, 2, 3) + val original = mutableListOf(1, 2, 3) val reversed = original.asReversed() reversed.set(0, 300) @@ -109,7 +109,7 @@ class ReversedViewsTest { } @test fun testMutableRemove() { - val original = arrayListOf("a", "b", "c") + val original = mutableListOf("a", "b", "c") val reversed = original.asReversed() reversed.removeAt(0) // remove c @@ -124,7 +124,7 @@ class ReversedViewsTest { } @test fun testMutableRemoveByObj() { - val original = arrayListOf("a", "b", "c") + val original = mutableListOf("a", "b", "c") val reversed = original.asReversed() reversed.remove("c") @@ -133,7 +133,7 @@ class ReversedViewsTest { } @test fun testMutableClear() { - val original = arrayListOf(1, 2, 3) + val original = mutableListOf(1, 2, 3) val reversed = original.asReversed() reversed.clear() @@ -144,16 +144,16 @@ class ReversedViewsTest { @test fun testContains() { assertTrue { 1 in listOf(1, 2, 3).asReversed() } - assertTrue { 1 in arrayListOf(1, 2, 3).asReversed() } + assertTrue { 1 in mutableListOf(1, 2, 3).asReversed() } } @test fun testIndexOf() { assertEquals(2, listOf(1, 2, 3).asReversed().indexOf(1)) - assertEquals(2, arrayListOf(1, 2, 3).asReversed().indexOf(1)) + assertEquals(2, mutableListOf(1, 2, 3).asReversed().indexOf(1)) } @test fun testBidirectionalModifications() { - val original = arrayListOf(1, 2, 3, 4) + val original = mutableListOf(1, 2, 3, 4) val reversed = original.asReversed() original.removeAt(3) @@ -178,7 +178,7 @@ class ReversedViewsTest { @test fun testSetIOOB() { val success = try { - arrayListOf(1, 2, 3).asReversed().set(3, 0) + mutableListOf(1, 2, 3).asReversed().set(3, 0) true } catch(expected: IndexOutOfBoundsException) { false @@ -189,7 +189,7 @@ class ReversedViewsTest { @test fun testAddIOOB() { val success = try { - arrayListOf(1, 2, 3).asReversed().add(4, 0) + mutableListOf(1, 2, 3).asReversed().add(4, 0) true } catch(expected: IndexOutOfBoundsException) { false @@ -200,7 +200,7 @@ class ReversedViewsTest { @test fun testRemoveIOOB() { val success = try { - arrayListOf(1, 2, 3).asReversed().removeAt(3) + mutableListOf(1, 2, 3).asReversed().removeAt(3) true } catch(expected: IndexOutOfBoundsException) { false