Skip to content

Commit c01f6b6

Browse files
committed
Rename toImmutable extensions.
1 parent 07bccd5 commit c01f6b6

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/extensions.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,29 @@ fun <E> immutableHashSetOf(vararg elements: E): ImmutableSet<E> = ImmutableHashS
6767

6868
fun <K, V> immutableHashMapOf(vararg pairs: Pair<K, V>): ImmutableMap<K, V> = ImmutableHashMap.emptyOf<K,V>().mutate { it += pairs }
6969

70-
fun <T> List<T>.toImmutable(): ImmutableList<T>
71-
= this as? ImmutableList
70+
fun <T> Iterable<T>.toImmutableList(): ImmutableList<T> =
71+
this as? ImmutableList
7272
?: (this as? ImmutableList.Builder)?.build()
73-
?: ImmutableVectorList.emptyOf<T>().addAll(this)
73+
?: immutableListOf<T>() + this
7474

75-
fun <T> Set<T>.toImmutable(): ImmutableSet<T>
76-
= this as? ImmutableSet<T>
75+
76+
// fun <T> Array<T>.toImmutableList(): ImmutableList<T> = immutableListOf<T>() + this.asList()
77+
78+
fun CharSequence.toImmutableList(): ImmutableList<Char> =
79+
immutableListOf<Char>().mutate { this.toCollection(it) }
80+
81+
fun <T> Iterable<T>.toImmutableSet(): ImmutableSet<T> =
82+
this as? ImmutableSet<T>
7783
?: (this as? ImmutableSet.Builder)?.build()
78-
?: ImmutableOrderedSet.emptyOf<T>().addAll(this)
84+
?: immutableSetOf<T>() + this
7985

8086
fun <T> Set<T>.toImmutableHashSet(): ImmutableSet<T>
8187
= this as? ImmutableHashSet
8288
?: (this as? ImmutableHashSet.Builder)?.build()
83-
?: ImmutableHashSet.emptyOf<T>().addAll(this)
89+
?: ImmutableHashSet.emptyOf<T>() + this
8490

8591

86-
fun <K, V> Map<K, V>.toImmutable(): ImmutableMap<K, V>
92+
fun <K, V> Map<K, V>.toImmutableMap(): ImmutableMap<K, V>
8793
= this as? ImmutableMap
8894
?: (this as? ImmutableMap.Builder)?.build()
8995
?: ImmutableHashMap.emptyOf<K, V>().putAll(this) // TODO: ImmutableOrderedMap.emptyOf

kotlinx-collections-immutable/tests/src/test/kotlin/kotlinx.collections.immutable/ImmutableListTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class ImmutableListTest {
2626
val original = listOf("a", "bar", "cat", null)
2727

2828
val list = original.toMutableList() // copy
29-
var immList = list.toImmutable()
30-
val immList2 = immList.toImmutable()
29+
var immList = list.toImmutableList()
30+
val immList2 = immList.toImmutableList()
3131
assertTrue(immList2 === immList)
3232

3333
assertEquals<List<*>>(list, immList) // problem
@@ -54,7 +54,7 @@ class ImmutableListTest {
5454
}
5555

5656
@Test fun replaceElements() {
57-
var list = "abcxaxab12".toList().toImmutable()
57+
var list = "abcxaxab12".toImmutableList()
5858

5959
for (i in list.indices) {
6060
list = list.set(i, list[i] as Char + i)
@@ -66,7 +66,7 @@ class ImmutableListTest {
6666
}
6767

6868
@Test fun removeElements() {
69-
val list = "abcxaxyz12".toList().toImmutable()
69+
val list = "abcxaxyz12".toImmutableList()
7070
fun expectList(content: String, list: ImmutableList<Char>) {
7171
assertEquals(content, list.joinToString(""))
7272
}
@@ -83,7 +83,7 @@ class ImmutableListTest {
8383
}
8484

8585
@Test fun subList() {
86-
val list = "abcxaxyz12".toList().toImmutable()
86+
val list = "abcxaxyz12".toImmutableList()
8787
val subList = list.subList(2, 5) // 2, 3, 4
8888
assertTrue(subList is ImmutableList)
8989
assertEquals(listOf('c', 'x', 'a'), subList)
@@ -99,7 +99,7 @@ class ImmutableListTest {
9999
assertEquals<List<*>>(list, builder)
100100
assertTrue(list === builder.build(), "Building the same list without modifications")
101101

102-
val list2 = builder.toImmutable()
102+
val list2 = builder.toImmutableList()
103103
assertTrue(list2 === list, "toImmutable calls build()")
104104

105105
with(list) {
@@ -132,7 +132,7 @@ class ImmutableListTest {
132132
@Test fun noOperation() {
133133
immutableListOf<Int>().testNoOperation({ clear() }, { clear() })
134134

135-
val list = "abcxaxyz12".toList().toImmutable()
135+
val list = "abcxaxyz12".toImmutableList()
136136
with(list) {
137137
testNoOperation({ remove('d') }, { remove('d') })
138138
testNoOperation({ removeAll(listOf('d', 'e')) }, { removeAll(listOf('d', 'e')) })

kotlinx-collections-immutable/tests/src/test/kotlin/kotlinx.collections.immutable/ImmutableMapTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class ImmutableMapTest {
3232
val original = mapOf("x" to 1, "y" to null, null to 2)
3333

3434
val map = HashMap(original) // copy
35-
var immMap = map.toImmutable()
36-
val immMap2 = immMap.toImmutable()
35+
var immMap = map.toImmutableMap()
36+
val immMap2 = immMap.toImmutableMap()
3737
assertTrue(immMap2 === immMap)
3838

3939
assertEquals<Map<*, *>>(map, immMap) // problem
@@ -85,7 +85,7 @@ class ImmutableMapTest {
8585
assertEquals<Map<*, *>>(map, builder)
8686
assertTrue(map === builder.build(), "Building the same list without modifications")
8787

88-
val map2 = builder.toImmutable()
88+
val map2 = builder.toImmutableMap()
8989
assertTrue(map2 === map, "toImmutable calls build()")
9090

9191
with(map) {

kotlinx-collections-immutable/tests/src/test/kotlin/kotlinx.collections.immutable/ImmutableSetTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ImmutableHashSetTest : ImmutableSetTestBase() {
2121
override fun noOperation() {
2222
// immutableSetOf<Int>().testNoOperation({ clear() }, { clear() }) // fails to implement this property
2323

24-
val list = "abcxyz12".toSet().toImmutable()
24+
val list = "abcxyz12".toList().toImmutableSet()
2525
with(list) {
2626
testNoOperation({ add('a') }, { add('a') })
2727
testNoOperation({ addAll(listOf('a', 'b')) }, { addAll(listOf('a', 'b')) })
@@ -57,8 +57,8 @@ abstract class ImmutableSetTestBase {
5757
val original = setOf("a", "bar", "cat", null)
5858

5959
val set = original.toMutableSet() // copy
60-
var immSet = set.toImmutable()
61-
val immSet2 = immSet.toImmutable()
60+
var immSet = set.toImmutableSet()
61+
val immSet2 = immSet.toImmutableSet()
6262
assertTrue(immSet2 === immSet)
6363

6464
assertEquals<Set<*>>(set, immSet) // problem
@@ -84,7 +84,7 @@ abstract class ImmutableSetTestBase {
8484

8585

8686
@Test fun removeElements() {
87-
val set = "abcxyz12".toSet().toImmutable()
87+
val set = "abcxyz12".toList().toImmutableSet()
8888
fun expectSet(content: String, set: ImmutableSet<Char>) {
8989
assertEquals(content.toSet(), set)
9090
}
@@ -106,7 +106,7 @@ abstract class ImmutableSetTestBase {
106106
assertEquals<Set<*>>(set, builder)
107107
assertTrue(set === builder.build(), "Building the same set without modifications")
108108

109-
val set2 = builder.toImmutable()
109+
val set2 = builder.toImmutableSet()
110110
assertTrue(set2 === set, "toImmutable calls build()")
111111

112112
with(set) {
@@ -137,7 +137,7 @@ abstract class ImmutableSetTestBase {
137137
@Test open fun noOperation() {
138138
immutableSetOf<Int>().testNoOperation({ clear() }, { clear() })
139139

140-
val set = "abcxyz12".toSet().toImmutable()
140+
val set = "abcxyz12".asIterable().toImmutableSet()
141141
with(set) {
142142
testNoOperation({ add('a') }, { add('a') })
143143
testNoOperation({ addAll(listOf('a', 'b')) }, { addAll(listOf('a', 'b')) })

0 commit comments

Comments
 (0)