diff --git a/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt b/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt index 9b408a1d361..46ef91aafac 100644 --- a/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt +++ b/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt @@ -21,8 +21,9 @@ internal fun Map.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 Map.withDefault(defaultValue: (key: K) -> V): Map /** * 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. */ diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index d5d58e83bcd..e52e65f4fd0 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -216,7 +216,17 @@ internal inline fun Map.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 Map.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, diff --git a/libraries/stdlib/test/collections/MapTest.kt b/libraries/stdlib/test/collections/MapTest.kt index a6126e1245b..eb1148f4910 100644 --- a/libraries/stdlib/test/collections/MapTest.kt +++ b/libraries/stdlib/test/collections/MapTest.kt @@ -28,14 +28,15 @@ class MapTest { assertEquals("x", d) } - @Suppress("INVISIBLE_MEMBER") - @Test fun getOrImplicitDefault() { + @Test fun getValue() { val data: MutableMap = hashMapOf("bar" to 1) - assertFailsWith { data.getOrImplicitDefault("foo") } - assertEquals(1, data.getOrImplicitDefault("bar")) + assertFailsWith { 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).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() { diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt index 6077db3b5fb..e0567b89c24 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt @@ -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;