commonMain.io.ktor.util.collections.ConcurrentSet.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ktor-utils-watchosarm64 Show documentation
Show all versions of ktor-utils-watchosarm64 Show documentation
Ktor is a framework for quickly creating web applications in Kotlin with minimal effort.
/*
* 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()
}