Use NoSuchElementException instead of KeyMissingException. Deprecate KeyMissingException.

This commit is contained in:
Ilya Gorbunov
2015-06-22 21:17:51 +03:00
parent 9c7992abbb
commit d04e94c826
6 changed files with 25 additions and 16 deletions
+1 -1
View File
@@ -125,7 +125,7 @@ public open class LinkedHashMap<K, V>(
) : HashMap<K, V>(initialCapacity, loadFactor)
library
public class NoSuchElementException(message: String? = null) : Exception() {}
public open class NoSuchElementException(message: String? = null) : Exception() {}
library
public trait Enumeration<E> {
@@ -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 <K, V> Map<K, V>.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.") })
}
/**
@@ -1,5 +1,7 @@
package kotlin.properties
import java.util.NoSuchElementException
/**
* Standard property delegates.
@@ -177,6 +179,16 @@ private class BlockingLazyVal<T>(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.
@@ -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 <V> Map<in String, *>.get(thisRef: Any?, property: PropertyMetadata): V = getOrImplicitDefault(property.name) as V
@@ -21,7 +22,7 @@ public fun <V> Map<in String, *>.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 <V> MutableMap<in String, in V>.get(thisRef: Any?, property: PropertyMetadata): V = getOrImplicitDefault(property.name) as V
+2 -1
View File
@@ -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<String, Int> = 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 }
@@ -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)
}