Provide protected toArray implementation in AbstractCollection.

Relates to #KT-13898
This commit is contained in:
Ilya Gorbunov
2016-12-24 04:28:32 +03:00
parent bbf61664ea
commit cdfb72ab76
8 changed files with 71 additions and 11 deletions
@@ -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?> =