KT-42589 Add listOf, setOf, mapOf overloads for a single value to common src-set

`listOf`, `setOf` and `mapOf` overloads were defined in some source-sets
but common source-set was missing it.
This change adds common definition of these methods and also add
declarations for source-sets that were previously
missing it.

^KT-42589 fixed
This commit is contained in:
Filipp Zhinkin
2023-04-19 08:48:29 +02:00
committed by Space Team
parent aa85e6d95c
commit 50841e7cb0
12 changed files with 112 additions and 36 deletions
+17 -14
View File
@@ -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.
*/
@@ -60,11 +60,12 @@ internal actual fun <T> copyToArrayImpl(collection: Collection<*>, array: Array<
return array
}
/**
* Returns an immutable list containing only the specified object [element].
* Returns a new read-only list containing only the specified object [element].
*
* @sample samples.collections.Collections.Lists.singletonReadOnlyList
*/
public fun <T> listOf(element: T): List<T> = arrayListOf(element)
public actual fun <T> listOf(element: T): List<T> = arrayListOf(element)
@PublishedApi
@SinceKotlin("1.3")
@@ -81,11 +82,12 @@ internal actual inline fun <E> buildListInternal(capacity: Int, builderAction: M
return ArrayList<E>(capacity).apply(builderAction).build()
}
/**
* Returns an immutable set containing only the specified object [element].
* Returns a new read-only set containing only the specified object [element].
*
* @sample samples.collections.Collections.Sets.singletonReadOnlySet
*/
public fun <T> setOf(element: T): Set<T> = hashSetOf(element)
public actual fun <T> setOf(element: T): Set<T> = hashSetOf(element)
@PublishedApi
@SinceKotlin("1.3")
@@ -101,13 +103,6 @@ internal actual inline fun <E> buildSetInternal(capacity: Int, builderAction: Mu
return LinkedHashSet<E>(capacity).apply(builderAction).build()
}
/**
* Returns an immutable map, mapping only the specified key to the
* specified value.
*/
public fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> = hashMapOf(pair)
@PublishedApi
@SinceKotlin("1.3")
@kotlin.internal.InlineOnly
@@ -260,3 +255,11 @@ internal actual fun mapCapacity(expectedSize: Int) = expectedSize
internal fun checkBuilderCapacity(capacity: Int) {
require(capacity >= 0) { "capacity must be non-negative." }
}
/**
* Returns a new read-only map, mapping only the specified key to the
* specified value.
*
* @sample samples.collections.Maps.Instantiation.mapFromPairs
*/
public actual fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> = hashMapOf(pair)
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 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,11 +13,13 @@ import kotlin.internal.InlineOnly
import kotlin.internal.apiVersionIsAtLeast
/**
* Returns an immutable list containing only the specified object [element].
* Returns a new read-only list containing only the specified object [element].
*
* The returned list is serializable.
*
* @sample samples.collections.Collections.Lists.singletonReadOnlyList
*/
public fun <T> listOf(element: T): List<T> = java.util.Collections.singletonList(element)
public actual fun <T> listOf(element: T): List<T> = java.util.Collections.singletonList(element)
@PublishedApi
@SinceKotlin("1.3")
@@ -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,16 +15,15 @@ import java.util.TreeMap
import java.util.concurrent.ConcurrentMap
import kotlin.collections.builders.MapBuilder
/**
* Returns an immutable map, mapping only the specified key to the
* Returns a new read-only map, mapping only the specified key to the
* specified value.
*
* The returned map is serializable.
*
* @sample samples.collections.Maps.Instantiation.mapFromPairs
*/
public fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> = java.util.Collections.singletonMap(pair.first, pair.second)
public actual fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> = java.util.Collections.singletonMap(pair.first, pair.second)
@PublishedApi
@SinceKotlin("1.3")
@@ -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.
*/
@@ -10,12 +10,14 @@ package kotlin.collections
import kotlin.collections.builders.SetBuilder
/**
* Returns an immutable set containing only the specified object [element].
* Returns a new read-only set containing only the specified object [element].
*
* The returned set is serializable.
*
* @sample samples.collections.Collections.Sets.singletonReadOnlySet
*/
public fun <T> setOf(element: T): Set<T> = java.util.Collections.singleton(element)
public actual fun <T> setOf(element: T): Set<T> = java.util.Collections.singleton(element)
@PublishedApi
@SinceKotlin("1.3")
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 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.
*/
@@ -184,10 +184,14 @@ class CollectionJVMTest {
@Test fun emptyMapIsSerializable() = testSingletonSerialization(emptyMap<Any, Any>())
private fun testSingletonSerialization(value: Any) {
private fun checkSerializeAndDeserialize(value: Any): Any {
val result = serializeAndDeserialize(value)
assertEquals(value, result)
return result
}
private fun testSingletonSerialization(value: Any) {
val result = checkSerializeAndDeserialize(value)
assertSame(value, result)
}
@@ -245,7 +249,6 @@ class CollectionJVMTest {
testCollectionBuilderSerialization(source)
}
private fun testCollectionBuilderSerialization(value: Any) {
val result = serializeAndDeserialize(value)
assertEquals(value, result)
@@ -260,4 +263,14 @@ class CollectionJVMTest {
}
}
@Test fun singletonListIsSerializable() = testSingletonCollectionSerialization(listOf(42))
@Test fun singletonSetIsSerializable() = testSingletonCollectionSerialization(setOf(42))
@Test fun singletonMapIsSerializable() = testSingletonCollectionSerialization(mapOf("hello" to "world"))
private fun testSingletonCollectionSerialization(value: Any) {
val deserialized = checkSerializeAndDeserialize(value)
assertReadOnly(deserialized)
}
}
@@ -122,3 +122,11 @@ internal actual inline fun checkCountOverflow(count: Int): Int {
}
return count
}
/**
* Returns a new read-only list containing only the specified object [element].
*
* @sample samples.collections.Collections.Lists.singletonReadOnlyList
*/
@SinceKotlin("1.9")
public actual fun <T> listOf(element: T): List<T> = arrayListOf(element)
@@ -34,3 +34,12 @@ internal actual fun <K, V> Map<out K, V>.toSingletonMap(): Map<K, V>
*/
@PublishedApi
internal actual fun mapCapacity(expectedSize: Int) = expectedSize
/**
* Returns a new read-only map, mapping only the specified key to the
* specified value.
*
* @sample samples.collections.Maps.Instantiation.mapFromPairs
*/
@SinceKotlin("1.9")
public actual fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> = hashMapOf(pair)
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@@ -7,9 +7,11 @@ package kotlin.collections
// TODO: Add SingletonSet class
/**
* Returns an immutable set containing only the specified object [element].
* Returns a new read-only set containing only the specified object [element].
*
* @sample samples.collections.Collections.Sets.singletonReadOnlySet
*/
public fun <T> setOf(element: T): Set<T> = hashSetOf(element)
public actual fun <T> setOf(element: T): Set<T> = hashSetOf(element)
@PublishedApi
@SinceKotlin("1.3")
@@ -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.
*/
@@ -301,6 +301,13 @@ class Collections {
assertTrue(set1 == set2)
}
@Sample
fun singletonReadOnlySet() {
val set = setOf('a')
assertPrints(set, "[a]")
assertPrints(set.size, "1")
}
@Sample
fun emptyMutableSet() {
val set = mutableSetOf<Int>()
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 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.
*/
@@ -76,6 +76,16 @@ public fun <T> emptyList(): List<T> = EmptyList
*/
public fun <T> listOf(vararg elements: T): List<T> = if (elements.size > 0) elements.asList() else emptyList()
/**
* Returns a new read-only list containing only the specified object [element].
*
* The returned list is serializable (JVM).
*
* @sample samples.collections.Collections.Lists.singletonReadOnlyList
*/
@SinceKotlin("1.9")
public expect fun <T> listOf(element: T): List<T>
/**
* Returns an empty read-only list. The returned list is serializable (JVM).
* @sample samples.collections.Collections.Lists.emptyReadOnlyList
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 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.
*/
@@ -54,6 +54,17 @@ public fun <K, V> emptyMap(): Map<K, V> = @Suppress("UNCHECKED_CAST") (EmptyMap
public fun <K, V> mapOf(vararg pairs: Pair<K, V>): Map<K, V> =
if (pairs.size > 0) pairs.toMap(LinkedHashMap(mapCapacity(pairs.size))) else emptyMap()
/**
* Returns a new read-only map, mapping only the specified key to the
* specified value.
*
* The returned map is serializable (JVM).
*
* @sample samples.collections.Maps.Instantiation.mapFromPairs
*/
@SinceKotlin("1.9")
public expect fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V>
/**
* Returns an empty read-only map.
*
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 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.
*/
@@ -43,6 +43,16 @@ public fun <T> emptySet(): Set<T> = EmptySet
*/
public fun <T> setOf(vararg elements: T): Set<T> = if (elements.size > 0) elements.toSet() else emptySet()
/**
* Returns a new read-only set containing only the specified object [element].
*
* The returned set is serializable (JVM).
*
* @sample samples.collections.Collections.Sets.singletonReadOnlySet
*/
@SinceKotlin("1.9")
public expect fun <T> setOf(element: T): Set<T>
/**
* Returns an empty read-only set. The returned set is serializable (JVM).
* @sample samples.collections.Collections.Sets.emptyReadOnlySet