Document map delegation accessors and withDefault functionality.

This commit is contained in:
Ilya Gorbunov
2015-06-19 17:44:38 +03:00
parent 2f8a431a79
commit d2feefbf3a
2 changed files with 43 additions and 9 deletions
@@ -9,15 +9,13 @@ import kotlin.platform.platformName
public class KeyMissingException(message: String): RuntimeException(message)
//private inline fun <K, V> Map<K, V>.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 <K, V> Map<K, V>.getOrImplicitDefault(key: K): V {
if (this is MapWithDefault)
return this.getOrImplicitDefault(key)
@@ -25,12 +23,26 @@ public fun <K, V> Map<K, V>.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 <K, V> Map<K, V>.withDefault(default: (key: K) -> V): Map<K, V> =
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 <K, V> MutableMap<K, V>.withDefault(default: (key: K) -> V): MutableMap<K, V> =
when (this) {
@@ -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 <V> Map<in String, *>.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 <V> MutableMap<in String, in V>.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 <V> MutableMap<in String, in V>.set(thisRef: Any?, property: PropertyMetadata, value: V) {
this.put(property.name, value)
}