Provide mutableListOf.

Deprecate linkedListOf.
#KT-9663
This commit is contained in:
Ilya Gorbunov
2016-01-16 05:14:28 +03:00
parent 60ca1cbcf3
commit fe8ba4d356
4 changed files with 35 additions and 26 deletions
@@ -74,9 +74,18 @@ public fun <T> listOf(element: T): List<T> = 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 <T> linkedListOf(vararg elements: T): LinkedList<T>
= if (elements.size == 0) LinkedList() else LinkedList(ArrayAsCollection(elements))
@Deprecated("Use LinkedList constructor.", ReplaceWith("LinkedList<T>()", "java.util.LinkedList"))
public fun <T> linkedListOf() = LinkedList<T>()
/** Returns a new [MutableList] with the given elements. */
public fun <T> mutableListOf(vararg elements: T): MutableList<T>
= if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements))
/** Returns a new [ArrayList] with the given elements. */
public fun <T> arrayListOf(vararg elements: T): ArrayList<T>
= if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements))
@@ -47,13 +47,13 @@ class CollectionJVMTest {
@test fun filterIntoLinkedList() {
val data = listOf("foo", "bar")
val foo = data.filterTo(linkedListOf<String>()) { it.startsWith("f") }
val foo = data.filterTo(LinkedList<String>()) { 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<String>
@@ -62,13 +62,13 @@ class CollectionJVMTest {
@test fun filterNotIntoLinkedListOf() {
val data = listOf("foo", "bar")
val foo = data.filterNotTo(linkedListOf<String>()) { it.startsWith("f") }
val foo = data.filterNotTo(LinkedList<String>()) { 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<String>
@@ -77,10 +77,10 @@ class CollectionJVMTest {
@test fun filterNotNullIntoLinkedListOf() {
val data = listOf(null, "foo", null, "bar")
val foo = data.filterNotNullTo(linkedListOf<String>())
val foo = data.filterNotNullTo(LinkedList<String>())
assertEquals(2, foo.size)
assertEquals(linkedListOf("foo", "bar"), foo)
assertEquals(LinkedList(listOf("foo", "bar")), foo)
assertTrue {
foo is LinkedList<String>
@@ -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<String>().last() }
assertFails { LinkedList<String>().last() }
}
@test fun contains() {
assertTrue(linkedListOf(15, 19, 20).contains(15))
assertTrue(LinkedList(listOf(15, 19, 20)).contains(15))
}
@test fun toArray() {
@@ -2,5 +2,5 @@ package test.collections
import java.util.*
class LinkedListTest : OrderedIterableTests<LinkedList<String>>(linkedListOf("foo", "bar"), linkedListOf<String>())
class LinkedListTest : OrderedIterableTests<LinkedList<String>>(LinkedList(listOf("foo", "bar")), LinkedList<String>())
@@ -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<Int>(), arrayListOf<Int>().asReversed())
assertEquals(emptyList<Int>(), mutableListOf<Int>().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