Use NoSuchElementException instead of KeyMissingException. Deprecate KeyMissingException.
This commit is contained in:
@@ -125,7 +125,7 @@ public open class LinkedHashMap<K, V>(
|
|||||||
) : HashMap<K, V>(initialCapacity, loadFactor)
|
) : HashMap<K, V>(initialCapacity, loadFactor)
|
||||||
|
|
||||||
library
|
library
|
||||||
public class NoSuchElementException(message: String? = null) : Exception() {}
|
public open class NoSuchElementException(message: String? = null) : Exception() {}
|
||||||
|
|
||||||
library
|
library
|
||||||
public trait Enumeration<E> {
|
public trait Enumeration<E> {
|
||||||
|
|||||||
@@ -1,26 +1,20 @@
|
|||||||
package kotlin
|
package kotlin
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
import kotlin.platform.platformName
|
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.
|
* 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.
|
* 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 {
|
public fun <K, V> Map<K, V>.getOrImplicitDefault(key: K): V {
|
||||||
if (this is MapWithDefault)
|
if (this is MapWithDefault)
|
||||||
return this.getOrImplicitDefault(key)
|
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
|
package kotlin.properties
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Standard property delegates.
|
* 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.
|
* 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.
|
* @param T the type of the object that owns the delegated property.
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package kotlin.properties
|
package kotlin.properties
|
||||||
|
|
||||||
import java.io.Serializable
|
import java.io.Serializable
|
||||||
|
import java.util.NoSuchElementException
|
||||||
import kotlin.platform.platformName
|
import kotlin.platform.platformName
|
||||||
|
|
||||||
// extensions for Map and MutableMap
|
// 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.
|
* @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.
|
* @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
|
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.
|
* @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.
|
* @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")
|
platformName("getVar")
|
||||||
public fun <V> MutableMap<in String, in V>.get(thisRef: Any?, property: PropertyMetadata): V = getOrImplicitDefault(property.name) as V
|
public fun <V> MutableMap<in String, in V>.get(thisRef: Any?, property: PropertyMetadata): V = getOrImplicitDefault(property.name) as V
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package test.collections
|
package test.collections
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
import kotlin.test.assertFalse
|
import kotlin.test.assertFalse
|
||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
@@ -24,7 +25,7 @@ class MapTest {
|
|||||||
|
|
||||||
test fun getOrImplicitDefault() {
|
test fun getOrImplicitDefault() {
|
||||||
val data: MutableMap<String, Int> = hashMapOf("bar" to 1)
|
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"))
|
assertEquals(1, data.getOrImplicitDefault("bar"))
|
||||||
|
|
||||||
val mutableWithDefault = data.withDefault { 42 }
|
val mutableWithDefault = data.withDefault { 42 }
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package test.properties.delegation
|
package test.properties.delegation
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
import kotlin.properties.*
|
import kotlin.properties.*
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
import org.junit.Test as test
|
import org.junit.Test as test
|
||||||
@@ -24,7 +25,7 @@ class ValByMapExtensionsTest {
|
|||||||
assertEquals("default", e)
|
assertEquals("default", e)
|
||||||
assertEquals(1, i)
|
assertEquals(1, i)
|
||||||
assertEquals(1.0, x)
|
assertEquals(1.0, x)
|
||||||
fails { d }
|
assertTrue(fails { d } is NoSuchElementException)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +60,7 @@ class VarByMapExtensionsTest {
|
|||||||
map["a"] = null
|
map["a"] = null
|
||||||
a // fails { a } // does not fail due to KT-8135
|
a // fails { a } // does not fail due to KT-8135
|
||||||
|
|
||||||
fails { d }
|
assertTrue(fails { d } is NoSuchElementException)
|
||||||
map["d"] = null
|
map["d"] = null
|
||||||
assertEquals(null, d)
|
assertEquals(null, d)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user