From 70fdc379ef6d631d0f059077b1ccad90cc891258 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sat, 31 Oct 2015 04:45:34 +0300 Subject: [PATCH] Drop deprecated Delegates.lazy and blockingLazy and ObservableProperty constructor-like function --- .../src/kotlin/properties/Delegation.kt | 103 ++---------------- .../delegation/lazy/LazyValuesTest.kt | 72 +----------- 2 files changed, 12 insertions(+), 163 deletions(-) diff --git a/libraries/stdlib/src/kotlin/properties/Delegation.kt b/libraries/stdlib/src/kotlin/properties/Delegation.kt index 3c9710192ea..879aa8f7c0f 100644 --- a/libraries/stdlib/src/kotlin/properties/Delegation.kt +++ b/libraries/stdlib/src/kotlin/properties/Delegation.kt @@ -12,36 +12,7 @@ public object Delegates { * object construction time but at a later time. Trying to read the property before the initial value has been * assigned results in an exception. */ - public fun notNull(): ReadWriteProperty = NotNullVar() - - /** - * Returns a property delegate for a read-only property that is initialized on first access by calling the - * specified block of code. Supports lazy initialization semantics for properties. - * @param initializer the function that returns the value of the property. - */ - @Deprecated("Use lazy {} instead in kotlin package", ReplaceWith("kotlin.lazy(LazyThreadSafetyMode.NONE, initializer)")) - public fun lazy(initializer: () -> T): ReadOnlyProperty = LazyVal(initializer) - - /** - * Returns a property delegate for a read-only property that is initialized on first access by calling the - * specified block of code under a specified lock. Supports lazy initialization semantics for properties and - * concurrent access. - * @param lock the object the monitor of which is locked before calling the initializer block. If not specified, - * the property delegate object itself is used as a lock. - * @param initializer the function that returns the value of the property. - */ - @Deprecated("Use lazy(lock) {} instead in kotlin package", ReplaceWith("lazy(lock, initializer)")) - public fun blockingLazy(lock: Any?, initializer: () -> T): ReadOnlyProperty = BlockingLazyVal(lock, initializer) - - /** - * Returns a property delegate for a read-only property that is initialized on first access by calling the - * specified block of code under a specified lock. Supports lazy initialization semantics for properties and - * concurrent access. - * The property delegate object itself is used as a lock. - * @param initializer the function that returns the value of the property. - */ - @Deprecated("Use lazy {} instead in kotlin package", ReplaceWith("kotlin.lazy(initializer)")) - public fun blockingLazy(initializer: () -> T): ReadOnlyProperty = BlockingLazyVal(null, initializer) + public fun notNull(): ReadWriteProperty = NotNullVar() /** * Returns a property delegate for a read/write property that calls a specified callback function when changed. @@ -49,7 +20,7 @@ public object Delegates { * @param onChange the callback which is called after the change of the property is made. The value of the property * has already been changed when this callback is invoked. */ - public inline fun observable(initialValue: T, crossinline onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Unit): + public inline fun observable(initialValue: T, crossinline onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Unit): ReadWriteProperty = object : ObservableProperty(initialValue) { override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) = onChange(property, oldValue, newValue) } @@ -63,7 +34,7 @@ public object Delegates { * If the callback returns `true` the value of the property is being set to the new value, * and if the callback returns `false` the new value is discarded and the property remains its old value. */ - public inline fun vetoable(initialValue: T, crossinline onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Boolean): + public inline fun vetoable(initialValue: T, crossinline onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Boolean): ReadWriteProperty = object : ObservableProperty(initialValue) { override fun beforeChange(property: KProperty<*>, oldValue: T, newValue: T): Boolean = onChange(property, oldValue, newValue) } @@ -74,7 +45,7 @@ public object Delegates { * @param map the map where the property values are stored. */ @Deprecated("Delegate property to the map itself without creating a wrapper.", ReplaceWith("map", "kotlin.properties.get", "kotlin.properties.set")) - public fun mapVar(map: MutableMap): ReadWriteProperty { + public fun mapVar(map: MutableMap): ReadWriteProperty { return FixedMapVar(map, propertyNameSelector, throwKeyNotFound) } @@ -84,8 +55,8 @@ public object Delegates { * @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. */ - public fun mapVar(map: MutableMap, - default: (thisRef: Any?, desc: String) -> T): ReadWriteProperty { + public fun mapVar(map: MutableMap, + default: (thisRef: Any?, desc: String) -> T): ReadWriteProperty { return FixedMapVar(map, propertyNameSelector, default) } @@ -95,7 +66,7 @@ public object Delegates { * @param map the map where the property values are stored. */ @Deprecated("Delegate property to the map itself without creating a wrapper.", ReplaceWith("map", "kotlin.properties.get")) - public fun mapVal(map: Map): ReadOnlyProperty { + public fun mapVal(map: Map): ReadOnlyProperty { return FixedMapVal(map, propertyNameSelector, throwKeyNotFound) } @@ -105,8 +76,8 @@ public object Delegates { * @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. */ - public fun mapVal(map: Map, - default: (thisRef: Any?, desc: String) -> T): ReadOnlyProperty { + public fun mapVal(map: Map, + default: (thisRef: Any?, desc: String) -> T): ReadOnlyProperty { return FixedMapVal(map, propertyNameSelector, default) } } @@ -124,13 +95,6 @@ private class NotNullVar() : ReadWriteProperty { } } - -@Deprecated("Use Delegates.vetoable() instead or construct implementation of abstract ObservableProperty", ReplaceWith("Delegates.vetoable(initialValue, onChange)")) -public fun ObservableProperty(initialValue: T, onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Boolean): ObservableProperty = - object : ObservableProperty(initialValue) { - override fun beforeChange(property: KProperty<*>, oldValue: T, newValue: T): Boolean = onChange(property, oldValue, newValue) - } - /** * Implements the core logic of a property delegate for a read/write property that calls callback functions when changed. * @param initialValue the initial value of the property. @@ -166,51 +130,6 @@ public abstract class ObservableProperty(initialValue: T) : ReadWriteProperty } } -private object NULL_VALUE {} - -private fun escape(value: Any?): Any { - return value ?: NULL_VALUE -} - -private fun unescape(value: Any?): Any? { - return if (value === NULL_VALUE) null else value -} - -private class LazyVal(private val initializer: () -> T) : ReadOnlyProperty { - private var value: Any? = null - - public override fun getValue(thisRef: Any?, property: KProperty<*>): T { - if (value == null) { - value = escape(initializer()) - } - return unescape(value) as T - } -} - -private class BlockingLazyVal(lock: Any?, private val initializer: () -> T) : ReadOnlyProperty { - private val lock = lock ?: this - @Volatile private var value: Any? = null - - public override fun getValue(thisRef: Any?, property: KProperty<*>): T { - val _v1 = value - if (_v1 != null) { - return unescape(_v1) as T - } - - return synchronized(lock) { - val _v2 = value - if (_v2 != null) { - unescape(_v2) as T - } - else { - val typedValue = initializer() - value = escape(typedValue) - typedValue - } - } - } -} - /** * 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. @@ -309,8 +228,8 @@ public open class FixedMapVar( return map } - protected override fun key(desc: KProperty<*>): K { - return (key)(desc) + protected override fun key(property: KProperty<*>): K { + return (key)(property) } protected override fun default(ref: T, property: KProperty<*>): V { diff --git a/libraries/stdlib/test/properties/delegation/lazy/LazyValuesTest.kt b/libraries/stdlib/test/properties/delegation/lazy/LazyValuesTest.kt index e706fa4dd5d..3f28a21fd7a 100644 --- a/libraries/stdlib/test/properties/delegation/lazy/LazyValuesTest.kt +++ b/libraries/stdlib/test/properties/delegation/lazy/LazyValuesTest.kt @@ -91,79 +91,9 @@ class UnsafeNullableLazyValTest { } } -class UnsafeLazyValDeprecatedTest { - var result = 0 - val a by Delegates.lazy { - ++result - } - - @test fun doTest() { - a - assertTrue(a == 1, "fail: initializer should be invoked only once") - } -} - -class BlockingLazyValDeprecatedTest { - var result = 0 - val a by Delegates.blockingLazy { - ++result - } - - @test fun doTest() { - a - assertTrue(a == 1, "fail: initializer should be invoked only once") - } -} - -class UnsafeNullableLazyValDeprecatedTest { - var resultA = 0 - var resultB = 0 - - val a: Int? by Delegates.lazy { resultA++; null} - val b by Delegates.lazy { foo() } - - @test fun doTest() { - a - b - - assertTrue(a == null, "fail: a should be null") - assertTrue(b == null, "fail: b should be null") - assertTrue(resultA == 1, "fail: initializer for a should be invoked only once") - assertTrue(resultB == 1, "fail: initializer for b should be invoked only once") - } - - fun foo(): String? { - resultB++ - return null - } -} - -class BlockingNullableLazyValDeprecatedTest { - var resultA = 0 - var resultB = 0 - - val a: Int? by Delegates.blockingLazy { resultA++; null} - val b by Delegates.blockingLazy { foo() } - - @test fun doTest() { - a - b - - assertTrue(a == null, "fail: a should be null") - assertTrue(b == null, "fail: a should be null") - assertTrue(resultA == 1, "fail: initializer for a should be invoked only once") - assertTrue(resultB == 1, "fail: initializer for b should be invoked only once") - } - - fun foo(): String? { - resultB++ - return null - } -} - class IdentityEqualsIsUsedToUnescapeLazyValTest { var equalsCalled = 0 - val a by lazy { ClassWithCustomEquality { equalsCalled++ } } + private val a by lazy { ClassWithCustomEquality { equalsCalled++ } } @test fun doTest() { a