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

commonMain.io.ktor.util.collections.ConcurrentSet.kt Maven / Gradle / Ivy

Go to download

Ktor is a framework for quickly creating web applications in Kotlin with minimal effort.

There is a newer version: 3.0.0-rc-1
Show newest version
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.util.collections

/**
 * Concurrent set implemented on top of [ConcurrentMap]
 */
@Suppress("FunctionName")
public fun  ConcurrentSet(): MutableSet = object : MutableSet {
    private val delegate = ConcurrentMap()

    override fun add(element: Key): Boolean {
        if (delegate.containsKey(element)) return false
        delegate[element] = Unit
        return true
    }

    override fun addAll(elements: Collection): Boolean = elements.all { add(it) }

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

    override fun iterator(): MutableIterator = delegate.keys.iterator()

    override fun remove(element: Key): Boolean = delegate.remove(element) != null

    override fun removeAll(elements: Collection): Boolean = elements.all { remove(it) }

    override fun retainAll(elements: Collection): Boolean {
        val removeList = mutableSetOf()
        for (key in delegate.keys) {
            if (key !in elements) removeList.add(key)
        }

        return removeAll(removeList)
    }

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

    override fun contains(element: Key): Boolean = delegate.containsKey(element)

    override fun containsAll(elements: Collection): Boolean = elements.containsAll(delegate.keys)

    override fun isEmpty(): Boolean = delegate.isEmpty()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy