commonMain.algorithms.FakeWeakMap.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weak Show documentation
Show all versions of weak Show documentation
Weak references and maps for Kotlin Multiplatform
The newest version!
package opensavvy.pedestal.weak.algorithms
import opensavvy.pedestal.weak.ExperimentalWeakApi
import opensavvy.pedestal.weak.WeakMap
private class FakeWeakMapImpl : WeakMap {
private val map = LinkedHashMap()
override fun get(key: K): V? =
map[key]
override fun set(key: K, value: V) {
map[key] = value
}
@ExperimentalWeakApi
override fun contains(key: K): Boolean =
map.containsKey(key)
@ExperimentalWeakApi
override fun remove(key: K): V? =
map.remove(key)
override fun toString() = "FakeWeakMap"
}
/**
* A [WeakMap] implementation that isn't weak.
*
* That is, all stored elements are strongly held and are never freed automatically.
* Elements are only removed when [WeakMap.remove] is called by the user.
*
* @see EmptyWeakMap Opposite behavior: values are immediately freed.
*/
@Suppress("FunctionName")
@ExperimentalWeakApi
fun FakeWeakMap(): WeakMap =
FakeWeakMapImpl()