diff --git a/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt b/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt index 1e3a9cda589..88e60a784cb 100644 --- a/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt +++ b/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt @@ -9,15 +9,13 @@ import kotlin.platform.platformName public class KeyMissingException(message: String): RuntimeException(message) -//private inline fun Map.getOrDefault(key: K, defaultValue: () -> V): V { -// val value = get(key) -// if (value == null && !containsKey(key)) { -// return defaultValue() -// } else { -// return value as V -// } -//} - +/** + * 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 [KeyMissingException] is thrown. + * To create a map with implicit default value use [Map.withDefault] method. + * + * @throws KeyMissingException 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) @@ -25,12 +23,26 @@ public fun Map.getOrImplicitDefault(key: K): V { return getOrElse(key, { throw KeyMissingException("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) { diff --git a/libraries/stdlib/src/kotlin/properties/MapAccessors.kt b/libraries/stdlib/src/kotlin/properties/MapAccessors.kt index 9d7084f51d8..6269cbc762c 100644 --- a/libraries/stdlib/src/kotlin/properties/MapAccessors.kt +++ b/libraries/stdlib/src/kotlin/properties/MapAccessors.kt @@ -5,11 +5,33 @@ import kotlin.platform.platformName // extensions for Map and MutableMap +/** + * Returns the value of the property for the given object from this read-only map. + * @param thisRef the object for which the value is requested (not used). + * @param property the metadata for the property, used to get the name of property and lookup the value corresponding to this name in the map. + * @return the property value. + * + * @throws KeyMissingException when the map doesn't contain value for the property name and doesn't provide an implicit default (see [Map.withDefault]). + */ public fun Map.get(thisRef: Any?, property: PropertyMetadata): V = getOrImplicitDefault(property.name) as V +/** + * Returns the value of the property for the given object from this mutable map. + * @param thisRef the object for which the value is requested (not used). + * @param property the metadata for the property, used to get the name of property and lookup the value corresponding to this name in the map. + * @return the property value. + * + * @throws KeyMissingException when the map doesn't contain value for the property name and doesn't provide an implicit default (see [Map.withDefault]). + */ platformName("getVar") public fun MutableMap.get(thisRef: Any?, property: PropertyMetadata): V = getOrImplicitDefault(property.name) as V +/** + * Stores the value of the property for the given object in this mutable map. + * @param thisRef the object for which the value is requested (not used). + * @param property the metadata for the property, used to get the name of property and store the value associated with that name in the map. + * @param value the value to set. + */ public fun MutableMap.set(thisRef: Any?, property: PropertyMetadata, value: V) { this.put(property.name, value) }