Provide protected toArray implementation in AbstractCollection.
Relates to #KT-13898
This commit is contained in:
@@ -158,4 +158,8 @@ header operator fun <K, V> MutableMap<K, V>.set(key: K, value: V): Unit
|
||||
|
||||
// from Grouping.kt
|
||||
public header fun <T, K> Grouping<T, K>.eachCount(): Map<K, Int>
|
||||
// public header inline fun <T, K> Grouping<T, K>.eachSumOf(valueSelector: (T) -> Int): Map<K, Int>
|
||||
// public header inline fun <T, K> Grouping<T, K>.eachSumOf(valueSelector: (T) -> Int): Map<K, Int>
|
||||
|
||||
internal header fun copyToArrayImpl(collection: Collection<*>): Array<Any?>
|
||||
|
||||
internal header fun <T> copyToArrayImpl(collection: Collection<*>, array: Array<T>): Array<T>
|
||||
|
||||
@@ -31,7 +31,14 @@ public abstract class AbstractCollection<out E> protected constructor() : Collec
|
||||
if (it === this) "(this Collection)" else it.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns new array of type `Array<Any?>` with the elements of this collection.
|
||||
*/
|
||||
protected open fun toArray(): Array<Any?> = copyToArrayImpl(this)
|
||||
|
||||
/**
|
||||
* Fills the provided [array] or creates new array of the same type
|
||||
* and fills it with the elements of this collection.
|
||||
*/
|
||||
protected open fun <T> toArray(array: Array<T>): Array<T> = copyToArrayImpl(this, array)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -182,6 +182,16 @@ internal fun <T> List<T>.optimizeReadOnlyList() = when (size) {
|
||||
else -> this
|
||||
}
|
||||
|
||||
@JvmVersion
|
||||
@kotlin.internal.InlineOnly
|
||||
internal inline fun copyToArrayImpl(collection: Collection<*>): Array<Any?> =
|
||||
kotlin.jvm.internal.CollectionToArray.toArray(collection)
|
||||
|
||||
@JvmVersion
|
||||
@kotlin.internal.InlineOnly
|
||||
internal inline fun <T> copyToArrayImpl(collection: Collection<*>, array: Array<T>): Array<T> =
|
||||
kotlin.jvm.internal.CollectionToArray.toArray(collection, array)
|
||||
|
||||
// copies typed varargs array to array of objects
|
||||
@JvmVersion
|
||||
private fun <T> Array<out T>.copyToArrayOfAny(isVarargs: Boolean): Array<Any?> =
|
||||
|
||||
@@ -905,4 +905,30 @@ class CollectionTest {
|
||||
assertTrue(listOf(1) is RandomAccess, "Default singleton list is RandomAccess")
|
||||
assertTrue(emptyList<Int>() is RandomAccess, "Empty list is RandomAccess")
|
||||
}
|
||||
|
||||
@Test fun abstractCollectionToArray() {
|
||||
class TestCollection<out E>(val data: Collection<E>) : AbstractCollection<E>() {
|
||||
val invocations = mutableListOf<String>()
|
||||
override val size get() = data.size
|
||||
override fun iterator() = data.iterator()
|
||||
|
||||
override fun toArray(): Array<Any?> {
|
||||
invocations += "toArray1"
|
||||
return data.toTypedArray()
|
||||
}
|
||||
public override fun <T> toArray(array: Array<T>): Array<T> {
|
||||
invocations += "toArray2"
|
||||
return super.toArray(array)
|
||||
}
|
||||
}
|
||||
val data = listOf("abc", "def")
|
||||
val coll = TestCollection(data)
|
||||
|
||||
val arr1 = coll.toTypedArray()
|
||||
assertEquals(data, arr1.asList())
|
||||
assertTrue("toArray1" in coll.invocations || "toArray2" in coll.invocations)
|
||||
|
||||
val arr2: Array<String> = coll.toArray(Array(coll.size + 1) { "" })
|
||||
assertEquals(data + listOf(null), arr2.asList())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user