kotlin.collections.MapWithDefault.kt Maven / Gradle / Ivy
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("MapsKt")
package kotlin
import java.util.*
import kotlin.platform.platformName
/**
* Returns the value for the given key, or the implicit default value for this map.
* By default no implicit value is provided for maps and a [NoSuchElementException] is thrown.
* To create a map with implicit default value use [withDefault] method.
*
* @throws NoSuchElementException when the map doesn't contain value for the specified key and no implicit default was provided for that map.
*/
public fun Map.getOrImplicitDefault(key: K): V {
if (this is MapWithDefault)
return this.getOrImplicitDefault(key)
return getOrElse(key, { throw NoSuchElementException("Key $key is missing in the map.") })
}
/**
* Returns a wrapper of this read-only map, having the implicit default value provided with the specified function [default].
* This implicit default value is used when [getOrImplicitDefault] is called on the returned map,
* and that map doesn't contain value for the key specified.
*
* When this map already have an implicit default value provided with a former call to [withDefault], it is being replaced by this call.
*/
public fun Map.withDefault(default: (key: K) -> V): Map =
when (this) {
is MapWithDefault -> this.map.withDefault(default)
else -> MapWithDefaultImpl(this, default)
}
/**
* Returns a wrapper of this mutable map, having the implicit default value provided with the specified function [default].
* This implicit default value is used when [getOrImplicitDefault] is called on the returned map,
* and that map doesn't contain value for the key specified.
*
* When this map already have an implicit default value provided with a former call to [withDefault], it is being replaced by this call.
*/
@platformName("withDefaultMutable")
public fun MutableMap.withDefault(default: (key: K) -> V): MutableMap =
when (this) {
is MutableMapWithDefault -> this.map.withDefault(default)
else -> MutableMapWithDefaultImpl(this, default)
}
private interface MapWithDefault: Map {
public val map: Map
public fun getOrImplicitDefault(key: K): V
}
private interface MutableMapWithDefault: MutableMap, MapWithDefault {
public override val map: MutableMap
}
private class MapWithDefaultImpl(public override val map: Map, private val default: (key: K) -> V) : MapWithDefault {
override fun equals(other: Any?): Boolean = map.equals(other)
override fun hashCode(): Int = map.hashCode()
override fun toString(): String = map.toString()
override fun size(): Int = map.size()
override fun isEmpty(): Boolean = map.isEmpty()
override fun containsKey(key: Any?): Boolean = map.containsKey(key)
override fun containsValue(value: Any?): Boolean = map.containsValue(value)
override fun get(key: Any?): V? = map.get(key)
override fun keySet(): Set = map.keySet()
override fun values(): Collection = map.values()
override fun entrySet(): Set> = map.entrySet()
override fun getOrImplicitDefault(key: K): V = map.getOrElse(key, { default(key) })
}
private class MutableMapWithDefaultImpl(public override val map: MutableMap, private val default: (key: K) -> V): MutableMapWithDefault {
override fun equals(other: Any?): Boolean = map.equals(other)
override fun hashCode(): Int = map.hashCode()
override fun toString(): String = map.toString()
override fun size(): Int = map.size()
override fun isEmpty(): Boolean = map.isEmpty()
override fun containsKey(key: Any?): Boolean = map.containsKey(key)
override fun containsValue(value: Any?): Boolean = map.containsValue(value)
override fun get(key: Any?): V? = map.get(key)
override fun keySet(): MutableSet = map.keySet()
override fun values(): MutableCollection = map.values()
override fun entrySet(): MutableSet> = map.entrySet()
override fun put(key: K, value: V): V? = map.put(key, value)
override fun remove(key: Any?): V? = map.remove(key)
override fun putAll(m: Map) = map.putAll(m)
override fun clear() = map.clear()
override fun getOrImplicitDefault(key: K): V = map.getOrElse(key, { default(key) })
}