main.com.sceyt.chatuikit.presentation.common.ConcurrentHashSet.kt Maven / Gradle / Ivy
package com.sceyt.chatuikit.presentation.common
import java.util.concurrent.ConcurrentHashMap
class ConcurrentHashSet : MutableSet {
private val map = ConcurrentHashMap()
private val present = Any()
override val size: Int
get() = map.size
override fun isEmpty(): Boolean = map.isEmpty()
override fun contains(element: E): Boolean = map.containsKey(element)
override fun iterator(): MutableIterator = map.keys.iterator()
override fun add(element: E): Boolean = map.put(element, present) == null
override fun remove(element: E): Boolean = map.remove(element) == present
override fun containsAll(elements: Collection): Boolean = elements.all { contains(it) }
override fun addAll(elements: Collection): Boolean = elements.fold(false) { acc, e -> acc or add(e) }
override fun removeAll(elements: Collection): Boolean = elements.fold(false) { acc, e -> acc or remove(e) }
override fun retainAll(elements: Collection): Boolean {
val toRetain = elements.toSet()
var modified = false
val iterator = map.keys.iterator()
while (iterator.hasNext()) {
val key = iterator.next()
if (!toRetain.contains(key)) {
iterator.remove()
modified = true
}
}
return modified
}
override fun clear() = map.clear()
}