Provide mutableListOf.
Deprecate linkedListOf. #KT-9663
This commit is contained in:
@@ -74,9 +74,18 @@ public fun <T> listOf(element: T): List<T> = Collections.singletonList(element)
|
|||||||
|
|
||||||
/** Returns a new [LinkedList] with the given elements. */
|
/** Returns a new [LinkedList] with the given elements. */
|
||||||
@JvmVersion
|
@JvmVersion
|
||||||
|
@Deprecated("Use LinkedList constructor.", ReplaceWith("LinkedList(listOf(*elements))", "java.util.LinkedList"))
|
||||||
public fun <T> linkedListOf(vararg elements: T): LinkedList<T>
|
public fun <T> linkedListOf(vararg elements: T): LinkedList<T>
|
||||||
= if (elements.size == 0) LinkedList() else LinkedList(ArrayAsCollection(elements))
|
= 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. */
|
/** Returns a new [ArrayList] with the given elements. */
|
||||||
public fun <T> arrayListOf(vararg elements: T): ArrayList<T>
|
public fun <T> arrayListOf(vararg elements: T): ArrayList<T>
|
||||||
= if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements))
|
= if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements))
|
||||||
|
|||||||
@@ -47,13 +47,13 @@ class CollectionJVMTest {
|
|||||||
|
|
||||||
@test fun filterIntoLinkedList() {
|
@test fun filterIntoLinkedList() {
|
||||||
val data = listOf("foo", "bar")
|
val data = listOf("foo", "bar")
|
||||||
val foo = data.filterTo(linkedListOf<String>()) { it.startsWith("f") }
|
val foo = data.filterTo(LinkedList<String>()) { it.startsWith("f") }
|
||||||
|
|
||||||
assertTrue {
|
assertTrue {
|
||||||
foo.all { it.startsWith("f") }
|
foo.all { it.startsWith("f") }
|
||||||
}
|
}
|
||||||
assertEquals(1, foo.size)
|
assertEquals(1, foo.size)
|
||||||
assertEquals(linkedListOf("foo"), foo)
|
assertEquals(listOf("foo"), foo)
|
||||||
|
|
||||||
assertTrue {
|
assertTrue {
|
||||||
foo is LinkedList<String>
|
foo is LinkedList<String>
|
||||||
@@ -62,13 +62,13 @@ class CollectionJVMTest {
|
|||||||
|
|
||||||
@test fun filterNotIntoLinkedListOf() {
|
@test fun filterNotIntoLinkedListOf() {
|
||||||
val data = listOf("foo", "bar")
|
val data = listOf("foo", "bar")
|
||||||
val foo = data.filterNotTo(linkedListOf<String>()) { it.startsWith("f") }
|
val foo = data.filterNotTo(LinkedList<String>()) { it.startsWith("f") }
|
||||||
|
|
||||||
assertTrue {
|
assertTrue {
|
||||||
foo.all { !it.startsWith("f") }
|
foo.all { !it.startsWith("f") }
|
||||||
}
|
}
|
||||||
assertEquals(1, foo.size)
|
assertEquals(1, foo.size)
|
||||||
assertEquals(linkedListOf("bar"), foo)
|
assertEquals(listOf("bar"), foo)
|
||||||
|
|
||||||
assertTrue {
|
assertTrue {
|
||||||
foo is LinkedList<String>
|
foo is LinkedList<String>
|
||||||
@@ -77,10 +77,10 @@ class CollectionJVMTest {
|
|||||||
|
|
||||||
@test fun filterNotNullIntoLinkedListOf() {
|
@test fun filterNotNullIntoLinkedListOf() {
|
||||||
val data = listOf(null, "foo", null, "bar")
|
val data = listOf(null, "foo", null, "bar")
|
||||||
val foo = data.filterNotNullTo(linkedListOf<String>())
|
val foo = data.filterNotNullTo(LinkedList<String>())
|
||||||
|
|
||||||
assertEquals(2, foo.size)
|
assertEquals(2, foo.size)
|
||||||
assertEquals(linkedListOf("foo", "bar"), foo)
|
assertEquals(LinkedList(listOf("foo", "bar")), foo)
|
||||||
|
|
||||||
assertTrue {
|
assertTrue {
|
||||||
foo is LinkedList<String>
|
foo is LinkedList<String>
|
||||||
@@ -105,15 +105,15 @@ class CollectionJVMTest {
|
|||||||
val data = listOf("foo", "bar")
|
val data = listOf("foo", "bar")
|
||||||
assertEquals("bar", data.last())
|
assertEquals("bar", data.last())
|
||||||
assertEquals(25, listOf(15, 19, 20, 25).last())
|
assertEquals(25, listOf(15, 19, 20, 25).last())
|
||||||
assertEquals('a', linkedListOf('a').last())
|
assertEquals('a', LinkedList(listOf('a')).last())
|
||||||
}
|
}
|
||||||
|
|
||||||
@test fun lastException() {
|
@test fun lastException() {
|
||||||
assertFails { linkedListOf<String>().last() }
|
assertFails { LinkedList<String>().last() }
|
||||||
}
|
}
|
||||||
|
|
||||||
@test fun contains() {
|
@test fun contains() {
|
||||||
assertTrue(linkedListOf(15, 19, 20).contains(15))
|
assertTrue(LinkedList(listOf(15, 19, 20)).contains(15))
|
||||||
}
|
}
|
||||||
|
|
||||||
@test fun toArray() {
|
@test fun toArray() {
|
||||||
|
|||||||
@@ -2,5 +2,5 @@ package test.collections
|
|||||||
|
|
||||||
import java.util.*
|
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() {
|
@test fun testMutableSimple() {
|
||||||
assertEquals(listOf(3, 2, 1), arrayListOf(1, 2, 3).asReversed())
|
assertEquals(listOf(3, 2, 1), mutableListOf(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().toList())
|
||||||
}
|
}
|
||||||
|
|
||||||
@test fun testMutableDoubleReverse() {
|
@test fun testMutableDoubleReverse() {
|
||||||
assertEquals(listOf(1, 2, 3), arrayListOf(1, 2, 3).asReversed().asReversed())
|
assertEquals(listOf(1, 2, 3), mutableListOf(1, 2, 3).asReversed().asReversed())
|
||||||
assertEquals(listOf(2, 3), arrayListOf(1, 2, 3, 4).asReversed().subList(1, 3).asReversed())
|
assertEquals(listOf(2, 3), mutableListOf(1, 2, 3, 4).asReversed().subList(1, 3).asReversed())
|
||||||
}
|
}
|
||||||
|
|
||||||
@test fun testMutableEmpty() {
|
@test fun testMutableEmpty() {
|
||||||
assertEquals(emptyList<Int>(), arrayListOf<Int>().asReversed())
|
assertEquals(emptyList<Int>(), mutableListOf<Int>().asReversed())
|
||||||
}
|
}
|
||||||
|
|
||||||
@test fun testMutableReversedSubList() {
|
@test fun testMutableReversedSubList() {
|
||||||
@@ -84,7 +84,7 @@ class ReversedViewsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@test fun testMutableAdd() {
|
@test fun testMutableAdd() {
|
||||||
val original = arrayListOf(1, 2, 3)
|
val original = mutableListOf(1, 2, 3)
|
||||||
val reversed = original.asReversed()
|
val reversed = original.asReversed()
|
||||||
|
|
||||||
reversed.add(0) // add zero at end of reversed
|
reversed.add(0) // add zero at end of reversed
|
||||||
@@ -97,7 +97,7 @@ class ReversedViewsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@test fun testMutableSet() {
|
@test fun testMutableSet() {
|
||||||
val original = arrayListOf(1, 2, 3)
|
val original = mutableListOf(1, 2, 3)
|
||||||
val reversed = original.asReversed()
|
val reversed = original.asReversed()
|
||||||
|
|
||||||
reversed.set(0, 300)
|
reversed.set(0, 300)
|
||||||
@@ -109,7 +109,7 @@ class ReversedViewsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@test fun testMutableRemove() {
|
@test fun testMutableRemove() {
|
||||||
val original = arrayListOf("a", "b", "c")
|
val original = mutableListOf("a", "b", "c")
|
||||||
val reversed = original.asReversed()
|
val reversed = original.asReversed()
|
||||||
|
|
||||||
reversed.removeAt(0) // remove c
|
reversed.removeAt(0) // remove c
|
||||||
@@ -124,7 +124,7 @@ class ReversedViewsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@test fun testMutableRemoveByObj() {
|
@test fun testMutableRemoveByObj() {
|
||||||
val original = arrayListOf("a", "b", "c")
|
val original = mutableListOf("a", "b", "c")
|
||||||
val reversed = original.asReversed()
|
val reversed = original.asReversed()
|
||||||
|
|
||||||
reversed.remove("c")
|
reversed.remove("c")
|
||||||
@@ -133,7 +133,7 @@ class ReversedViewsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@test fun testMutableClear() {
|
@test fun testMutableClear() {
|
||||||
val original = arrayListOf(1, 2, 3)
|
val original = mutableListOf(1, 2, 3)
|
||||||
val reversed = original.asReversed()
|
val reversed = original.asReversed()
|
||||||
|
|
||||||
reversed.clear()
|
reversed.clear()
|
||||||
@@ -144,16 +144,16 @@ class ReversedViewsTest {
|
|||||||
|
|
||||||
@test fun testContains() {
|
@test fun testContains() {
|
||||||
assertTrue { 1 in listOf(1, 2, 3).asReversed() }
|
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() {
|
@test fun testIndexOf() {
|
||||||
assertEquals(2, listOf(1, 2, 3).asReversed().indexOf(1))
|
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() {
|
@test fun testBidirectionalModifications() {
|
||||||
val original = arrayListOf(1, 2, 3, 4)
|
val original = mutableListOf(1, 2, 3, 4)
|
||||||
val reversed = original.asReversed()
|
val reversed = original.asReversed()
|
||||||
|
|
||||||
original.removeAt(3)
|
original.removeAt(3)
|
||||||
@@ -178,7 +178,7 @@ class ReversedViewsTest {
|
|||||||
|
|
||||||
@test fun testSetIOOB() {
|
@test fun testSetIOOB() {
|
||||||
val success = try {
|
val success = try {
|
||||||
arrayListOf(1, 2, 3).asReversed().set(3, 0)
|
mutableListOf(1, 2, 3).asReversed().set(3, 0)
|
||||||
true
|
true
|
||||||
} catch(expected: IndexOutOfBoundsException) {
|
} catch(expected: IndexOutOfBoundsException) {
|
||||||
false
|
false
|
||||||
@@ -189,7 +189,7 @@ class ReversedViewsTest {
|
|||||||
|
|
||||||
@test fun testAddIOOB() {
|
@test fun testAddIOOB() {
|
||||||
val success = try {
|
val success = try {
|
||||||
arrayListOf(1, 2, 3).asReversed().add(4, 0)
|
mutableListOf(1, 2, 3).asReversed().add(4, 0)
|
||||||
true
|
true
|
||||||
} catch(expected: IndexOutOfBoundsException) {
|
} catch(expected: IndexOutOfBoundsException) {
|
||||||
false
|
false
|
||||||
@@ -200,7 +200,7 @@ class ReversedViewsTest {
|
|||||||
|
|
||||||
@test fun testRemoveIOOB() {
|
@test fun testRemoveIOOB() {
|
||||||
val success = try {
|
val success = try {
|
||||||
arrayListOf(1, 2, 3).asReversed().removeAt(3)
|
mutableListOf(1, 2, 3).asReversed().removeAt(3)
|
||||||
true
|
true
|
||||||
} catch(expected: IndexOutOfBoundsException) {
|
} catch(expected: IndexOutOfBoundsException) {
|
||||||
false
|
false
|
||||||
|
|||||||
Reference in New Issue
Block a user