1616
1717package kotlinx.collections.immutable
1818
19+ import kotlinx.collections.immutable.adapters.ImmutableCollectionAdapter
20+ import kotlinx.collections.immutable.adapters.ImmutableSetAdapter
1921import org.pcollections.PMap
2022import java.util.ConcurrentModificationException
2123
22- internal abstract class AbstractImmutableMap <K , out V > protected constructor(protected val impl : PMap <K , @UnsafeVariance V >) : ImmutableMap <K, V> {
24+ internal abstract class AbstractImmutableMap <K , out V > protected constructor(protected val impl : PMap <K , @UnsafeVariance V >) : PersistentMap <K, V> {
2325
2426 abstract class AbstractImmutableEntry <out K , out V > : Map .Entry <K , V > {
2527 override fun equals (other : Any? ): Boolean = other is Map .Entry <* ,* > && other.key == key && other.value == value
@@ -40,31 +42,31 @@ internal abstract class AbstractImmutableMap<K, out V> protected constructor(pro
4042
4143
4244 // should it be immutable set/collection or just read-only?
43- private var _keys : Set <K >? = null
44- final override val keys: Set <K > get() = _keys ? : createKeys().apply { _keys = this }
45- protected open fun createKeys (): Set <K > = impl.keys
46-
47- private var _values : Collection <V >? = null
48- final override val values: Collection <V > get() = _values ? : createValues().apply { _values = this }
49- protected open fun createValues (): Collection <V > = impl.values
50-
51- private var _entries : Set <Map .Entry <K , V >>? = null
52- final override val entries: Set <Map .Entry <K , V >> get() = _entries ? : createEntries().apply { _entries = this }
53- protected open fun createEntries (): Set <Map .Entry <K , V >> = impl.entries
54-
55- override fun put (key : K , value : @UnsafeVariance V ): ImmutableMap <K , V > = wrap(impl.plus(key, value))
56- override fun putAll (m : Map <out K , @UnsafeVariance V >): ImmutableMap <K , V > = wrap(impl.plusAll(m))
57- override fun remove (key : K ): ImmutableMap <K , V > = wrap(impl.minus(key))
58- override fun remove (key : K , value : @UnsafeVariance V ): ImmutableMap <K , V >
45+ private var _keys : ImmutableSet <K >? = null
46+ final override val keys: ImmutableSet <K > get() = _keys ? : createKeys().apply { _keys = this }
47+ protected open fun createKeys (): ImmutableSet <K > = ImmutableSetAdapter ( impl.keys)
48+
49+ private var _values : ImmutableCollection <V >? = null
50+ final override val values: ImmutableCollection <V > get() = _values ? : createValues().apply { _values = this }
51+ protected open fun createValues (): ImmutableCollection <V > = ImmutableCollectionAdapter ( impl.values)
52+
53+ private var _entries : ImmutableSet <Map .Entry <K , V >>? = null
54+ final override val entries: ImmutableSet <Map .Entry <K , V >> get() = _entries ? : createEntries().apply { _entries = this }
55+ protected open fun createEntries (): ImmutableSet <Map .Entry <K , V >> = ImmutableSetAdapter ( impl.entries)
56+
57+ override fun put (key : K , value : @UnsafeVariance V ): PersistentMap <K , V > = wrap(impl.plus(key, value))
58+ override fun putAll (m : Map <out K , @UnsafeVariance V >): PersistentMap <K , V > = wrap(impl.plusAll(m))
59+ override fun remove (key : K ): PersistentMap <K , V > = wrap(impl.minus(key))
60+ override fun remove (key : K , value : @UnsafeVariance V ): PersistentMap <K , V >
5961 = if (! impl.contains(key, value)) this else wrap(impl.minus(key))
6062
6163 override abstract fun builder (): Builder <K , @UnsafeVariance V >
6264
6365 protected abstract fun wrap (impl : PMap <K , @UnsafeVariance V >): AbstractImmutableMap <K , V >
6466
6567
66- abstract class Builder <K , V > protected constructor(protected var value : AbstractImmutableMap <K , V >, protected var impl : PMap <K , V >) : ImmutableMap .Builder<K, V>, AbstractMutableMap<K, V>() {
67- override fun build (): ImmutableMap <K , V > = value.wrap(impl).apply { value = this }
68+ abstract class Builder <K , V > protected constructor(protected var value : AbstractImmutableMap <K , V >, protected var impl : PMap <K , V >) : PersistentMap .Builder<K, V>, AbstractMutableMap<K, V>() {
69+ override fun build (): AbstractImmutableMap <K , V > = value.wrap(impl).apply { value = this }
6870
6971 override val size: Int get() = impl.size
7072 override fun isEmpty (): Boolean = impl.isEmpty()
@@ -164,38 +166,3 @@ internal abstract class AbstractImmutableMap<K, out V> protected constructor(pro
164166internal fun <K , V > PMap <K , V >.contains (key : K , value : @UnsafeVariance V ): Boolean
165167 = this [key]?.let { candidate -> candidate == value } ? : (value == null && containsKey(key))
166168
167-
168- /*
169- private open class ImmutableCollectionWrapper<E>(protected val impl: Collection<E>) : ImmutableCollection<E> {
170- override val size: Int get() = impl.size
171- override fun isEmpty(): Boolean = impl.isEmpty()
172- override fun contains(element: @UnsafeVariance E): Boolean = impl.contains(element)
173- override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean = impl.containsAll(elements)
174- override fun iterator(): Iterator<E> = impl.iterator()
175-
176- override fun equals(other: Any?): Boolean = impl.equals(other)
177- override fun hashCode(): Int = impl.hashCode()
178- override fun toString(): String = impl.toString()
179-
180- override fun builder(): ImmutableCollection.Builder<E> = ImmutableVectorList.emptyOf<E>().builder().apply { addAll(impl) }
181-
182- override fun add(element: E): ImmutableCollection<E> = builder().apply { add(element) }.build()
183- override fun addAll(elements: Collection<E>): ImmutableCollection<E> = builder().apply { addAll(elements) }.build()
184- override fun remove(element: E): ImmutableCollection<E> = builder().apply { remove(element) }.build()
185- override fun removeAll(elements: Collection<E>): ImmutableCollection<E> = builder().apply { removeAll(elements) }.build()
186- override fun removeAll(predicate: (E) -> Boolean): ImmutableCollection<E> = builder().apply { removeAll(predicate) }.build()
187- override fun clear(): ImmutableCollection<E> = immutableListOf()
188- }
189-
190- private class ImmutableSetWrapper<E>(impl: Set<E>) : ImmutableSet<E>, ImmutableCollectionWrapper<E>(impl) {
191-
192- override fun builder(): ImmutableSet.Builder<E> = ImmutableOrderedSet.emptyOf<E>().builder().apply { addAll(impl) }
193-
194- override fun add(element: E): ImmutableSet<E> = super.add(element) as ImmutableSet
195- override fun addAll(elements: Collection<E>): ImmutableSet<E> = super.addAll(elements) as ImmutableSet
196- override fun remove(element: E): ImmutableSet<E> = super.remove(element) as ImmutableSet
197- override fun removeAll(elements: Collection<E>): ImmutableSet<E> = super.removeAll(elements) as ImmutableSet
198- override fun removeAll(predicate: (E) -> Boolean): ImmutableSet<E> = super.removeAll(predicate) as ImmutableSet
199- override fun clear(): ImmutableSet<E> = immutableSetOf()
200- }
201- */
0 commit comments