From d1a5988bfcf9b37d051ebeba898f79e5b87ad2e1 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 15 Jan 2016 22:23:47 +0300 Subject: [PATCH] Drop deprecations: complicated map delegates and kotlin.support.AbstractIterator. --- .../collections/AbstractIteratorDeprecated.kt | 8 - .../src/kotlin/properties/Delegation.kt | 154 -------------- .../delegation/MapDelegationTest.kt | 191 ------------------ 3 files changed, 353 deletions(-) delete mode 100644 libraries/stdlib/src/kotlin/collections/AbstractIteratorDeprecated.kt delete mode 100644 libraries/stdlib/test/properties/delegation/MapDelegationTest.kt diff --git a/libraries/stdlib/src/kotlin/collections/AbstractIteratorDeprecated.kt b/libraries/stdlib/src/kotlin/collections/AbstractIteratorDeprecated.kt deleted file mode 100644 index f5473628884..00000000000 --- a/libraries/stdlib/src/kotlin/collections/AbstractIteratorDeprecated.kt +++ /dev/null @@ -1,8 +0,0 @@ -package kotlin.support - -/** - * A base class to simplify implementing iterators so that implementations only have to implement [computeNext] - * to implement the iterator, calling [done] when the iteration is complete. - */ -@Deprecated("Use AbstractIterator from kotlin.collections instead.", ReplaceWith("kotlin.collections.AbstractIterator")) -public abstract class AbstractIterator : kotlin.collections.AbstractIterator() \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/properties/Delegation.kt b/libraries/stdlib/src/kotlin/properties/Delegation.kt index d133124e6bc..feff34bccc1 100644 --- a/libraries/stdlib/src/kotlin/properties/Delegation.kt +++ b/libraries/stdlib/src/kotlin/properties/Delegation.kt @@ -39,49 +39,6 @@ public object Delegates { override fun beforeChange(property: KProperty<*>, oldValue: T, newValue: T): Boolean = onChange(property, oldValue, newValue) } - /** - * Returns a property delegate for a read/write property that stores its value in a map, using the property name - * as a key. - * @param map the map where the property values are stored. - */ - @Deprecated("Delegate property to the map itself without creating a wrapper.", ReplaceWith("map")) - public fun mapVar(map: MutableMap): ReadWriteProperty { - return FixedMapVar(map, propertyNameSelector, throwKeyNotFound) - } - - /** - * Returns a property delegate for a read/write property that stores its value in a map, using the property name - * as a key. - * @param map the map where the property values are stored. - * @param default the function returning the value of the property for a given object if it's missing from the given map. - */ - @Deprecated("Complex scenarios of delegation to map are deprecated. Delegate to map itself.") - public fun mapVar(map: MutableMap, - default: (thisRef: R, propertyName: String) -> T): ReadWriteProperty { - return FixedMapVar(map, propertyNameSelector, default) - } - - /** - * Returns a property delegate for a read-only property that takes its value from a map, using the property name - * as a key. - * @param map the map where the property values are stored. - */ - @Deprecated("Delegate property to the map itself without creating a wrapper.", ReplaceWith("map")) - public fun mapVal(map: Map): ReadOnlyProperty { - return FixedMapVal(map, propertyNameSelector, throwKeyNotFound) - } - - /** - * Returns a property delegate for a read-only property that takes its value from a map, using the property name - * as a key. - * @param map the map where the property values are stored. - * @param default the function returning the value of the property for a given object if it's missing from the given map. - */ - @Deprecated("Complex scenarios of delegation to map are deprecated. Delegate to map itself.") - public fun mapVal(map: Map, - default: (thisRef: R, propertyName: String) -> T): ReadOnlyProperty { - return FixedMapVal(map, propertyNameSelector, default) - } } @@ -131,114 +88,3 @@ public abstract class ObservableProperty(initialValue: T) : ReadWriteProperty afterChange(property, oldValue, value) } } - -/** - * 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 K the type of key in the map. - * @param V the type of the property value. - */ -@Deprecated("Complex scenarios of delegation to map are deprecated. Delegate to map itself.") -public abstract class MapVal() : ReadOnlyProperty { - /** - * Returns the map used to store the values of the properties of the given object instance. - * @param ref the object instance for which the map is requested. - */ - protected abstract fun map(ref: T): Map - - /** - * Returns the map key used to store the values of the given property. - * @param property the property for which the key is requested. - */ - protected abstract fun key(property: KProperty<*>): K - - /** - * Returns the property value to be used when the map does not contain the corresponding key. - * @param ref the object instance for which the value was requested. - * @param property the property for which the value was requested. - */ - protected open fun default(ref: T, property: KProperty<*>): V { - throw NoSuchElementException("The value for property ${property.name} is missing in $ref.") - } - - public override fun getValue(thisRef: T, property: KProperty<*>) : V { - val map = map(thisRef) - val key = key(property) - return map.getOrElse(key, { default(thisRef, property) }) as V - } -} - -/** - * Implements the core logic for a read/write property delegate that stores property values in a map. - * @param T the type of the object that owns the delegated property. - * @param K the type of key in the map. - * @param V the type of the property value. - */ -@Deprecated("Complex scenarios of delegation to map are deprecated. Delegate to map itself.") -public abstract class MapVar() : MapVal(), ReadWriteProperty { - protected abstract override fun map(ref: T): MutableMap - - public override fun setValue(thisRef: T, property: KProperty<*>, value: V) { - val map = map(thisRef) - map.put(key(property), value) - } - - override fun getValue(thisRef: T, property: KProperty<*>): V { - return super.getValue(thisRef, property) - } -} - -private val propertyNameSelector: (KProperty<*>) -> String = {it.name} -private val throwKeyNotFound: (Any?, Any?) -> Nothing = {thisRef, key -> throw NoSuchElementException("The value for key $key is missing from $thisRef.") } - -/** - * Implements a read-only property delegate that stores the property values in a given map instance and uses the given - * callback functions for calculating the key and the default value for each property. - * @param map the map used to store the values. - * @param key the function to calculate the map key from a property metadata object. - * @param default the function returning the value of the property for a given object if it's missing from the given map. - */ -@Deprecated("Complex scenarios of delegation to map are deprecated. Delegate to map itself.") -public open class FixedMapVal( - private val map: Map, - private val key: (KProperty<*>) -> K, - private val default: (ref: T, key: K) -> V = throwKeyNotFound -) : MapVal() { - protected override fun map(ref: T): Map { - return map - } - - protected override fun key(property: KProperty<*>): K { - return (key)(property) - } - - protected override fun default(ref: T, property: KProperty<*>): V { - return (default)(ref, key(property)) - } -} - -/** - * Implements a read/write property delegate that stores the property values in a given map instance and uses the given - * callback functions for calculating the key and the default value for each property. - * @param map the map used to store the values. - * @param key the function to calculate the map key from a property metadata object. - * @param default the function returning the value of the property for a given object if it's missing from the given map. - */ -@Deprecated("Complex scenarios of delegation to map are deprecated. Delegate to map itself.") -public open class FixedMapVar( - private val map: MutableMap, - private val key: (KProperty<*>) -> K, - private val default: (ref: T, key: K) -> V = throwKeyNotFound -) : MapVar() { - protected override fun map(ref: T): MutableMap { - return map - } - - protected override fun key(property: KProperty<*>): K { - return (key)(property) - } - - protected override fun default(ref: T, property: KProperty<*>): V { - return (default)(ref, key(property)) - } -} diff --git a/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt b/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt deleted file mode 100644 index 0b1d7d60f00..00000000000 --- a/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt +++ /dev/null @@ -1,191 +0,0 @@ -package test.properties.delegation.map - -import org.junit.Test as test -import java.util.HashMap -import kotlin.properties.* -import kotlin.reflect.KProperty -import kotlin.test.* - -data class B(val a: Int) - - -class MapValWithDifferentTypesTest() { - val map = hashMapOf("a" to "a", "b" to 1, "c" to B(1), "d" to null) - val a by Delegates.mapVal(map) - val b by Delegates.mapVal(map) - val c by Delegates.mapVal(map) - val d by Delegates.mapVal(map) - - @test fun doTest() { - assertTrue(a == "a", "fail at 'a'") - assertTrue(b == 1, "fail at 'b'") - assertTrue(c == B(1), "fail at 'c'") - assertTrue(d == null, "fail at 'd'") - } -} - -class MapVarWithDifferentTypesTest() { - val map: HashMap = hashMapOf("a" to "a", "b" to 1, "c" to B(1), "d" to "d") - var a: String by Delegates.mapVar(map) - var b: Int by Delegates.mapVar(map) - var c by Delegates.mapVar(map) - var d by Delegates.mapVar(map) - - @test fun doTest() { - a = "aa" - b = 11 - c = B(11) - d = null - assertTrue(a == "aa", "fail at 'a'") - assertTrue(b == 11, "fail at 'b'") - assertTrue(c == B(11), "fail at 'c'") - assertTrue(d == null, "fail at 'd'") - - map["a"] = null - a // fails { a } // does not fail due to KT-8135 - } -} - -class MapNullableKeyTest { - val map = hashMapOf(null to "null") - var a by FixedMapVar(map, key = { desc -> null }, default = {ref, desc -> null}) - - @test fun doTest() { - assertTrue(a == "null", "fail at 'a'") - a = "foo" - assertTrue(a == "foo", "fail at 'a' after set") - } -} - -class MapPropertyStringTest() { - val map = hashMapOf("a" to "a", "b" to "b", "c" to "c") - val a: String by Delegates.mapVal(map) - var b by Delegates.mapVar(map) - val c by Delegates.mapVal(map) - - @test fun doTest() { - b = "newB" - assertTrue(a == "a", "fail at 'a'") - assertTrue(b == "newB", "fail at 'b'") - assertTrue(c == "c", "fail at 'c'") - } -} - -class MapValWithDefaultTest() { - val map = hashMapOf() - val a: String by Delegates.mapVal(map, default = { ref, desc -> "aDefault" }) - val b: String by FixedMapVal(map, default = { ref: MapValWithDefaultTest, desc: String -> "bDefault" }, key = {"b"}) - val c: String by FixedMapVal(map, default = { ref: MapValWithDefaultTest, desc: String -> "cDefault" }, key = { desc -> desc.name }) - val d: String by Delegates.mapVal(map, default = { ref, name -> ref.c }) - - @test fun doTest() { - assertEquals("aDefault", a, "fail at 'a'") - assertEquals("bDefault", b, "fail at 'b'") - assertEquals("cDefault", c, "fail at 'c'") - assertEquals(c, d, "'d' must have the default value equal to 'c'") - } -} - -class MapVarWithDefaultTest() { - val map = hashMapOf() - var a: String by Delegates.mapVar(map, default = {ref, desc -> "aDefault" }) - var b: String by FixedMapVar(map, default = {ref: Any?, desc: String -> "bDefault" }, key = {"b"}) - var c: String by FixedMapVar(map, default = {ref: Any?, desc: String -> "cDefault" }, key = { desc -> desc.name }) - var d: String by Delegates.mapVar(map, default = { ref, name -> ref.c }) - - @test fun doTest() { - assertEquals("aDefault", a, "fail at 'a'") - assertEquals("bDefault", b, "fail at 'b'") - assertEquals("cDefault", c, "fail at 'c'") - assertEquals(c, d, "'d' must have the default value equal to 'c'") - a = "a" - b = "b" - c = "c" - assertEquals("a", a, "fail at 'a' after set") - assertEquals("b", b, "fail at 'b' after set") - assertEquals("c", c, "fail at 'c' after set") - assertEquals(c, d, "'d' must still have the default value equal to 'c'") - } -} - -class MapPropertyKeyTest() { - val map = hashMapOf("a" to "a", "b" to "b") - val a by FixedMapVal(map, key = {"a"}) - var b by FixedMapVar(map, key = {"b"}) - - @test fun doTest() { - b = "c" - assertTrue(a == "a", "fail at 'a'") - assertTrue(b == "c", "fail at 'b'") - } -} - -class MapPropertyFunctionTest() { - val map = hashMapOf("aDesc" to "a", "bDesc" to "b") - val a by FixedMapVal(map, { desc -> "${desc.name}Desc" }) - var b by FixedMapVar(map, { desc -> "${desc.name}Desc" }) - - @test fun doTest() { - b = "c" - assertTrue(a == "a", "fail at 'a'") - assertTrue(b == "c", "fail at 'b' after set") - } -} - -@Suppress("ALWAYS_NULL") -private val topLevel: String by Delegates.mapVal(emptyMap(), default = { ref, name -> ref.toString() }) - -class MapTopLevelVarWithDefaultTest() { - @test fun doTest() { - assertEquals("null", topLevel) - } -} - -val mapVal = object : MapVal() { - override fun map(ref: MapPropertyCustomTest) = ref.map - override fun key(property: KProperty<*>) = "${property.name}Desc" -} - -val mapVar = object : MapVar() { - override fun map(ref: MapPropertyCustomTest) = ref.map - override fun key(property: KProperty<*>) = "${property.name}Desc" -} - -class MapPropertyCustomTest() { - val map = hashMapOf("aDesc" to "a", "bDesc" to "b") - val a by mapVal - var b by mapVar - - @test fun doTest() { - b = "newB" - assertTrue(a == "a", "fail at 'a'") - assertTrue(b == "newB", "fail at 'b' after set") - } -} - -val mapValWithDefault = object : MapVal() { - override fun map(ref: MapPropertyCustomWithDefaultTest) = ref.map - override fun key(property: KProperty<*>) = property.name - - override fun default(ref: MapPropertyCustomWithDefaultTest, property: KProperty<*>) = "default" -} - -val mapVarWithDefault = object : MapVar() { - override fun map(ref: MapPropertyCustomWithDefaultTest) = ref.map - override fun key(property: KProperty<*>) = property.name - - override fun default(ref: MapPropertyCustomWithDefaultTest, property: KProperty<*>) = "default" -} - -class MapPropertyCustomWithDefaultTest() { - val map = hashMapOf() - val a by mapValWithDefault - var b by mapVarWithDefault - - @test fun doTest() { - assertTrue(a == "default", "fail at 'a'") - assertTrue(b == "default", "fail at 'b'") - b = "c" - assertTrue(b == "c", "fail at 'b' after set") - } -}