All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.implementations.immutableMap.PersistentHashMapBuilderContentViews.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016-2019 JetBrains s.r.o.
 * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
 */

package kotlinx.collections.immutable.implementations.immutableMap

import kotlinx.collections.immutable.internal.MapImplementation

// intermediate abstract class to workaround KT-43321
internal abstract class AbstractMapBuilderEntries, K, V> : AbstractMutableSet() {
    final override fun contains(element: E): Boolean {
        // TODO: Eliminate this check after KT-30016 gets fixed.
        @Suppress("USELESS_CAST")
        if ((element as? Any?) !is Map.Entry<*, *>) return false
        return containsEntry(element)
    }
    abstract fun containsEntry(element: Map.Entry): Boolean

    final override fun remove(element: E): Boolean {
        // TODO: Eliminate this check after KT-30016 gets fixed.
        @Suppress("USELESS_CAST")
        if ((element as? Any?) !is Map.Entry<*, *>) return false
        return removeEntry(element)
    }
    abstract fun removeEntry(element: Map.Entry): Boolean
}

internal class PersistentHashMapBuilderEntries(private val builder: PersistentHashMapBuilder)
    : AbstractMapBuilderEntries, K, V>() {
    override fun add(element: MutableMap.MutableEntry): Boolean {
        throw UnsupportedOperationException()
    }

    override fun clear() {
        builder.clear()
    }

    override fun iterator(): MutableIterator> {
        return PersistentHashMapBuilderEntriesIterator(builder)
    }

    override fun removeEntry(element: Map.Entry): Boolean {
        return builder.remove(element.key, element.value)
    }

    override val size: Int
        get() = builder.size

    override fun containsEntry(element: Map.Entry): Boolean {
        return MapImplementation.containsEntry(builder, element)
    }
}

internal class PersistentHashMapBuilderKeys(private val builder: PersistentHashMapBuilder) : MutableSet, AbstractMutableSet() {
    override fun add(element: K): Boolean {
        throw UnsupportedOperationException()
    }

    override fun clear() {
        builder.clear()
    }

    override fun iterator(): MutableIterator {
        return PersistentHashMapBuilderKeysIterator(builder)
    }

    override fun remove(element: K): Boolean {
        if (builder.containsKey(element)) {
            builder.remove(element)
            return true
        }
        return false
    }

    override val size: Int
        get() = builder.size

    override fun contains(element: K): Boolean {
        return builder.containsKey(element)
    }
}

internal class PersistentHashMapBuilderValues(private val builder: PersistentHashMapBuilder) : MutableCollection, AbstractMutableCollection() {
    override val size: Int
        get() = builder.size

    override fun contains(element: V): Boolean {
        return builder.containsValue(element)
    }

    override fun add(element: V): Boolean {
        throw UnsupportedOperationException()
    }

    override fun clear() {
        builder.clear()
    }

    override fun iterator(): MutableIterator {
        return PersistentHashMapBuilderValuesIterator(builder)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy