From d04e94c826940c967fe1abfe156707e5a3b7d486 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 22 Jun 2015 21:17:51 +0300 Subject: [PATCH] Use NoSuchElementException instead of KeyMissingException. Deprecate KeyMissingException. --- js/js.libraries/src/core/javautil.kt | 2 +- .../src/kotlin/collections/MapWithDefault.kt | 14 ++++---------- .../stdlib/src/kotlin/properties/Delegation.kt | 12 ++++++++++++ .../stdlib/src/kotlin/properties/MapAccessors.kt | 5 +++-- libraries/stdlib/test/collections/MapTest.kt | 3 ++- .../test/properties/delegation/MapAccessorsTest.kt | 5 +++-- 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/js/js.libraries/src/core/javautil.kt b/js/js.libraries/src/core/javautil.kt index 2d5afba68fd..70250de8ed9 100644 --- a/js/js.libraries/src/core/javautil.kt +++ b/js/js.libraries/src/core/javautil.kt @@ -125,7 +125,7 @@ public open class LinkedHashMap( ) : HashMap(initialCapacity, loadFactor) library -public class NoSuchElementException(message: String? = null) : Exception() {} +public open class NoSuchElementException(message: String? = null) : Exception() {} library public trait Enumeration { diff --git a/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt b/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt index 88e60a784cb..fc65667a3d5 100644 --- a/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt +++ b/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt @@ -1,26 +1,20 @@ package kotlin +import java.util.* import kotlin.platform.platformName - -/** - * Exception thrown when the map does not contain the corresponding key. - */ -public class KeyMissingException(message: String): RuntimeException(message) - - /** * 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. + * By default no implicit value is provided for maps and a [NoSuchElementException] 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. + * @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 KeyMissingException("Key $key is missing in the map.") }) + return getOrElse(key, { throw NoSuchElementException("Key $key is missing in the map.") }) } /** diff --git a/libraries/stdlib/src/kotlin/properties/Delegation.kt b/libraries/stdlib/src/kotlin/properties/Delegation.kt index 1d6831379e2..a90fed1fac7 100644 --- a/libraries/stdlib/src/kotlin/properties/Delegation.kt +++ b/libraries/stdlib/src/kotlin/properties/Delegation.kt @@ -1,5 +1,7 @@ package kotlin.properties +import java.util.NoSuchElementException + /** * Standard property delegates. @@ -177,6 +179,16 @@ private class BlockingLazyVal(lock: Any?, private val initializer: () -> T) : } } +/** + * Exception thrown by the default implementation of property delegates which store values in a map + * when the map does not contain the corresponding key. + */ +deprecated("Do not throw or catch this exception, use NoSuchElementException instead.") +public class KeyMissingException + deprecated("Throw NoSuchElementException instead.", ReplaceWith("NoSuchElementException(message)")) + constructor(message: String): NoSuchElementException(message) + + /** * Implements the core logic for a property delegate that stores property values in a map. * @param T the type of the object that owns the delegated property. diff --git a/libraries/stdlib/src/kotlin/properties/MapAccessors.kt b/libraries/stdlib/src/kotlin/properties/MapAccessors.kt index 2de12707e05..6b7bf833775 100644 --- a/libraries/stdlib/src/kotlin/properties/MapAccessors.kt +++ b/libraries/stdlib/src/kotlin/properties/MapAccessors.kt @@ -1,6 +1,7 @@ package kotlin.properties import java.io.Serializable +import java.util.NoSuchElementException import kotlin.platform.platformName // extensions for Map and MutableMap @@ -11,7 +12,7 @@ import kotlin.platform.platformName * @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]). + * @throws NoSuchElementException 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 @@ -21,7 +22,7 @@ public fun Map.get(thisRef: Any?, property: PropertyMetadata): * @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]). + * @throws NoSuchElementException 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 diff --git a/libraries/stdlib/test/collections/MapTest.kt b/libraries/stdlib/test/collections/MapTest.kt index 2f5a6243825..2a2e1c72f90 100644 --- a/libraries/stdlib/test/collections/MapTest.kt +++ b/libraries/stdlib/test/collections/MapTest.kt @@ -1,5 +1,6 @@ package test.collections +import java.util.* import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -24,7 +25,7 @@ class MapTest { test fun getOrImplicitDefault() { val data: MutableMap = hashMapOf("bar" to 1) - assertTrue(fails { data.getOrImplicitDefault("foo") } is KeyMissingException) + assertTrue(fails { data.getOrImplicitDefault("foo") } is NoSuchElementException) assertEquals(1, data.getOrImplicitDefault("bar")) val mutableWithDefault = data.withDefault { 42 } diff --git a/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt b/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt index b96bfadb5cf..0254fd984da 100644 --- a/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt +++ b/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt @@ -1,5 +1,6 @@ package test.properties.delegation +import java.util.* import kotlin.properties.* import kotlin.test.* import org.junit.Test as test @@ -24,7 +25,7 @@ class ValByMapExtensionsTest { assertEquals("default", e) assertEquals(1, i) assertEquals(1.0, x) - fails { d } + assertTrue(fails { d } is NoSuchElementException) } } @@ -59,7 +60,7 @@ class VarByMapExtensionsTest { map["a"] = null a // fails { a } // does not fail due to KT-8135 - fails { d } + assertTrue(fails { d } is NoSuchElementException) map["d"] = null assertEquals(null, d) }