Map.getOrImplicitDefault is exposed public as Map.getValue

#KT-11851
This commit is contained in:
Ilya Gorbunov
2016-12-29 07:49:27 +03:00
parent 0a840d0174
commit 822e58ad83
4 changed files with 26 additions and 12 deletions
@@ -21,8 +21,9 @@ internal fun <K, V> Map<K, V>.getOrImplicitDefault(key: K): V {
/**
* Returns a wrapper of this read-only map, having the implicit default value provided with the specified function [defaultValue].
* This implicit default value is used when properties are delegated to the returned map,
* and that map doesn't contain a value for the key specified.
*
* This implicit default value is used when the original map doesn't contain a value for the key specified
* and a value is obtained with [Map.getValue] function, for example when properties are delegated to the map.
*
* When this map already has an implicit default value provided with a former call to [withDefault], it is being replaced by this call.
*/
@@ -34,8 +35,9 @@ public fun <K, V> Map<K, V>.withDefault(defaultValue: (key: K) -> V): Map<K, V>
/**
* Returns a wrapper of this mutable map, having the implicit default value provided with the specified function [defaultValue].
* This implicit default value is used when properties are delegated to the returned map,
* and that map doesn't contain a value for the key specified.
*
* This implicit default value is used when the original map doesn't contain a value for the key specified
* and a value is obtained with [Map.getValue] function, for example when properties are delegated to the map.
*
* When this map already has an implicit default value provided with a former call to [withDefault], it is being replaced by this call.
*/
@@ -216,7 +216,17 @@ internal inline fun <K, V> Map<K, V>.getOrElseNullable(key: K, defaultValue: ()
}
}
/**
* Returns the value for the given [key] or throws an exception if there is no such key in the map.
*
* If the map was created by [withDefault], resorts to its `defaultValue` provider function
* instead of throwing an exception.
*
* @throws NoSuchElementException when the map doesn't contain a value for the specified key and
* no implicit default value was provided for that map.
*/
@SinceKotlin("1.1")
public fun <K, V> Map<K, V>.getValue(key: K): V = getOrImplicitDefault(key)
/**
* Returns the value for the given key. If the key is not found in the map, calls the [defaultValue] function,
+8 -7
View File
@@ -28,14 +28,15 @@ class MapTest {
assertEquals("x", d)
}
@Suppress("INVISIBLE_MEMBER")
@Test fun getOrImplicitDefault() {
@Test fun getValue() {
val data: MutableMap<String, Int> = hashMapOf("bar" to 1)
assertFailsWith<NoSuchElementException> { data.getOrImplicitDefault("foo") }
assertEquals(1, data.getOrImplicitDefault("bar"))
assertFailsWith<NoSuchElementException> { data.getValue("foo") }.let { e ->
assertTrue("foo" in e.message!!)
}
assertEquals(1, data.getValue("bar"))
val mutableWithDefault = data.withDefault { 42 }
assertEquals(42, mutableWithDefault.getOrImplicitDefault("foo"))
assertEquals(42, mutableWithDefault.getValue("foo"))
// verify that it is wrapper
mutableWithDefault["bar"] = 2
@@ -44,10 +45,10 @@ class MapTest {
assertEquals(3, mutableWithDefault["bar"])
val readonlyWithDefault = (data as Map<String, Int>).withDefault { it.length }
assertEquals(4, readonlyWithDefault.getOrImplicitDefault("loop"))
assertEquals(4, readonlyWithDefault.getValue("loop"))
val withReplacedDefault = readonlyWithDefault.withDefault { 42 }
assertEquals(42, withReplacedDefault.getOrImplicitDefault("loop"))
assertEquals(42, withReplacedDefault.getValue("loop"))
}
@Test fun getOrPut() {
@@ -1630,6 +1630,7 @@ public final class kotlin/collections/MapsKt {
public static final fun getOrImplicitDefaultNullable (Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
public static final fun getOrPut (Ljava/util/Map;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
public static final fun getOrPut (Ljava/util/concurrent/ConcurrentMap;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
public static final fun getValue (Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
public static final fun hashMapOf ([Lkotlin/Pair;)Ljava/util/HashMap;
public static final fun linkedMapOf ([Lkotlin/Pair;)Ljava/util/LinkedHashMap;
public static final fun map (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/List;