macosX64Main.common.workarounds.TreeMap.objc.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluid-stdlib-macosx64 Show documentation
Show all versions of fluid-stdlib-macosx64 Show documentation
Potentially useful Kotlin standard library additions
package io.fluidsonic.stdlib
// TODO remove
internal actual class _TreeMap actual constructor() : MutableMap {
private val container = hashMapOf()
private val _entries = EntrySet()
private val _keys = KeySet()
override fun clear() {
container.clear()
}
override fun containsKey(key: K) =
container.containsKey(key)
override fun containsValue(value: V) =
container.containsValue(value)
override fun get(key: K) =
container[key]
override fun isEmpty() =
container.isEmpty()
override val entries: MutableSet>
get() = _entries
override val keys: MutableSet
get() = _keys
override val values: MutableCollection
get() = throw UnsupportedOperationException()
override fun put(key: K, value: V) =
container.put(key, value)
override fun putAll(from: Map) =
container.putAll(from)
override fun remove(key: K) =
container.remove(key)
override val size
get() = container.size
private inner class EntrySet : MutableSet> {
override fun add(element: MutableMap.MutableEntry) =
throw UnsupportedOperationException()
override fun addAll(elements: Collection>) =
throw UnsupportedOperationException()
override fun clear() {
container.entries.clear()
}
override fun contains(element: MutableMap.MutableEntry) =
container.entries.contains(element)
override fun containsAll(elements: Collection>) =
container.entries.containsAll(elements)
override fun isEmpty() =
container.entries.isEmpty()
override fun iterator(): MutableIterator> {
val sortedEntries = (container.entries as Collection, V>>).sortedBy { it.key }
as List>
val iterator = sortedEntries.iterator()
return object : MutableIterator> {
private var current: MutableMap.MutableEntry? = null
override fun hasNext() =
iterator.hasNext()
override fun next(): MutableMap.MutableEntry {
val next = iterator.next()
current = next
return next
}
override fun remove() {
val current = current ?: return
this.current = null
container.remove(current.key)
}
}
}
override fun remove(element: MutableMap.MutableEntry) =
container.entries.remove(element)
override fun removeAll(elements: Collection>) =
container.entries.removeAll(elements)
override fun retainAll(elements: Collection>) =
container.entries.retainAll(elements)
override val size
get() = container.entries.size
}
private inner class KeySet : MutableSet {
override fun add(element: K) =
throw UnsupportedOperationException()
override fun addAll(elements: Collection) =
throw UnsupportedOperationException()
override fun clear() {
container.clear()
}
override fun contains(element: K) =
container.containsKey(element)
override fun containsAll(elements: Collection) =
elements.all { container.containsKey(it) }
override fun isEmpty() =
container.isEmpty()
override fun iterator(): MutableIterator {
val sortedKeys = (container.keys as MutableSet>).sorted() as List
val iterator = sortedKeys.iterator()
return object : MutableIterator {
private var current: K? = null
private var hasCurrent = false
override fun hasNext() =
iterator.hasNext()
override fun next(): K {
val next = iterator.next()
current = next
hasCurrent = true
return next
}
override fun remove() {
if (!hasCurrent) return
val current = current ?: return
this.current = null
this.hasCurrent = false
container.remove(current)
}
}
}
override fun remove(element: K) =
container.remove(element) != null
override fun removeAll(elements: Collection): Boolean {
var removedAny = false
for (element in elements)
if (remove(element))
removedAny = true
return removedAny
}
override fun retainAll(elements: Collection): Boolean {
val elementToRemove = mutableListOf()
for (element in this)
if (!elements.contains(element))
elementToRemove += element
if (elementToRemove.isEmpty()) return false
for (element in elementToRemove)
remove(element)
return true
}
override val size
get() = container.size
}
}