KT-54739 Return single empty inst from collection builders(all src-sets)

Fixed remaining builders, added additional checks to a test.

^KT-54739 fixed
This commit is contained in:
Filipp Zhinkin
2023-04-20 15:10:11 +02:00
committed by Space Team
parent 0c1d957711
commit cadf4f63b0
7 changed files with 125 additions and 12 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -13,6 +13,10 @@ package kotlin.collections
* capacity and "growth increment" concepts.
*/
public actual open class ArrayList<E> internal constructor(private var array: Array<Any?>) : AbstractMutableList<E>(), MutableList<E>, RandomAccess {
private companion object {
private val Empty = ArrayList<Nothing>(0).also { it.isReadOnly = true }
}
private var isReadOnly: Boolean = false
/**
@@ -35,7 +39,7 @@ public actual open class ArrayList<E> internal constructor(private var array: Ar
internal fun build(): List<E> {
checkIsMutable()
isReadOnly = true
return this
return if (size > 0) this else Empty
}
/** Does nothing in this ArrayList implementation. */
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -18,6 +18,9 @@ import kotlin.collections.MutableMap.MutableEntry
* The insertion order is preserved by maintaining a doubly-linked list of all of its entries.
*/
public actual open class LinkedHashMap<K, V> : HashMap<K, V>, MutableMap<K, V> {
private companion object {
private val Empty = LinkedHashMap<Nothing, Nothing>(0).also { it.isReadOnly = true }
}
/**
* The entry we use includes next/prev pointers for a doubly-linked circular
@@ -204,7 +207,8 @@ public actual open class LinkedHashMap<K, V> : HashMap<K, V>, MutableMap<K, V> {
internal fun build(): Map<K, V> {
checkIsMutable()
isReadOnly = true
return this
@Suppress("UNCHECKED_CAST")
return if (size > 0) this else (Empty as Map<K, V>)
}
actual override fun clear() {
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
/*
@@ -15,6 +15,11 @@ package kotlin.collections
* This implementation preserves the insertion order of elements during the iteration.
*/
public actual open class LinkedHashSet<E> : HashSet<E>, MutableSet<E> {
private companion object {
private val Empty = LinkedHashSet<Nothing>(0).also {
(it.map as LinkedHashMap<Nothing, Any>).build()
}
}
internal constructor(map: LinkedHashMap<E, Any>) : super(map)
@@ -45,7 +50,7 @@ public actual open class LinkedHashSet<E> : HashSet<E>, MutableSet<E> {
@PublishedApi
internal fun build(): Set<E> {
(map as LinkedHashMap<E, Any>).build()
return this
return if (size > 0) this else Empty
}
internal override fun checkIsMutable(): Unit = map.checkIsMutable()