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,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