Skip to content

Commit a3909f5

Browse files
committed
Do not require keys, values and entries collections of immutable map being immutable. Just read-only is fine.
1 parent 483e6db commit a3909f5

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ internal abstract class AbstractImmutableMap<K, out V> protected constructor(pro
1818

1919

2020
// should it be immutable set/collection or just read-only?
21-
private var keysWrapped: ImmutableSet<K>? = null
22-
override val keys: ImmutableSet<K> get() = keysWrapped ?: ImmutableSetWrapper(impl.keys).apply { keysWrapped = this }
21+
private var _keys: Set<K>? = null
22+
final override val keys: Set<K> get() = _keys ?: createKeys().apply { _keys = this }
23+
protected open fun createKeys(): Set<K> = impl.keys
2324

24-
private var valuesWrapped: ImmutableCollection<V>? = null
25-
override val values: ImmutableCollection<V> get() = valuesWrapped ?: ImmutableCollectionWrapper(impl.values).apply { valuesWrapped = this }
25+
private var _values: Collection<V>? = null
26+
final override val values: Collection<V> get() = _values ?: createValues().apply { _values = this }
27+
protected open fun createValues(): Collection<V> = impl.values
2628

27-
private var entriesWrapped: ImmutableSet<Map.Entry<K, V>>? = null
28-
override val entries: ImmutableSet<Map.Entry<K, V>> get() = entriesWrapped ?: ImmutableSetWrapper(impl.entries).apply { entriesWrapped = this }
29+
private var _entries: Set<Map.Entry<K, V>>? = null
30+
final override val entries: Set<Map.Entry<K, V>> get() = _entries ?: createEntries().apply { _entries = this }
31+
protected open fun createEntries(): Set<Map.Entry<K, V>> = impl.entries
2932

3033
override fun put(key: K, value: @UnsafeVariance V): ImmutableMap<K, V> = wrap(impl.plus(key, value))
3134
override fun putAll(m: Map<out K, @UnsafeVariance V>): ImmutableMap<K, V> = wrap(impl.plusAll(m))
@@ -139,8 +142,8 @@ internal abstract class AbstractImmutableMap<K, out V> protected constructor(pro
139142
}
140143
}
141144

142-
private fun <K, V> PMap<K, V>.contains(key: K, value: @UnsafeVariance V): Boolean
143-
= this[key]?.let { candidate -> candidate == value } ?: (containsKey(key) && value == null)
145+
internal fun <K, V> PMap<K, V>.contains(key: K, value: @UnsafeVariance V): Boolean
146+
= this[key]?.let { candidate -> candidate == value } ?: (value == null && containsKey(key))
144147

145148

146149

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package kotlinx.collections.immutable
33

44
public interface ImmutableMap<K, out V>: Map<K, V> {
55

6-
override val keys: ImmutableSet<K>
6+
override val keys: Set<K>
77

8-
override val values: ImmutableCollection<V>
8+
override val values: Collection<V>
99

10-
override val entries: ImmutableSet<Map.Entry<K, V>>
10+
override val entries: Set<Map.Entry<K, V>>
1111

1212
fun put(key: K, value: @UnsafeVariance V): ImmutableMap<K, V>
1313

0 commit comments

Comments
 (0)