Provide Lazy<T> in kotlin package — an interface that represents lazily computed value. Read-only properties can be delegated to lazy with the extension getter. Delegates.lazy and blockingLazy are deprecated.

This commit is contained in:
Ilya Gorbunov
2015-06-11 23:29:33 +03:00
parent f2788d53c0
commit 536e669023
5 changed files with 203 additions and 62 deletions
@@ -17,6 +17,7 @@ public object Delegates {
* 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(initializer)"))
public fun lazy<T>(initializer: () -> T): ReadOnlyProperty<Any?, T> = LazyVal(initializer)
/**
@@ -27,6 +28,7 @@ public object Delegates {
* 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<T>(lock: Any? = null, initializer: () -> T): ReadOnlyProperty<Any?, T> = BlockingLazyVal(lock, initializer)
/**
+79
View File
@@ -0,0 +1,79 @@
package kotlin
public interface Lazy<out T> {
public val value: T
public val valueCreated: Boolean
}
public fun <T> Lazy<T>.get(thisRef: Any?, property: PropertyMetadata): T = value
public enum class LazyThreadSafetyMode {
SYNCHRONIZED,
NONE,
}
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 LazyImpl<out T>(initializer: () -> T, private val lockObj: Any) : Lazy<T> {
private var initializer: (() -> T)? = initializer
private volatile var _value: Any? = null
public override val value: T
get() {
val _v1 = _value
if (_v1 != null) {
return unescape(_v1) as T
}
return synchronized(lockObj) {
val _v2 = _value
if (_v2 != null) {
unescape(_v2) as T
}
else {
val typedValue = initializer!!()
_value = escape(typedValue)
initializer = null
typedValue
}
}
}
override val valueCreated: Boolean = _value == null
override fun toString(): String = if (valueCreated) value.toString() else "Lazy value not created yet."
}
private class UnsafeLazyImpl<out T>(initializer: () -> T) : Lazy<T> {
private var initializer: (() -> T)? = initializer
private var _value: Any? = null
override val value: T
get() {
if (_value == null) {
_value = escape(initializer!!())
initializer == null
}
return unescape(_value) as T
}
override val valueCreated: Boolean = _value == null
override fun toString(): String = if (valueCreated) value.toString() else "Lazy value not created yet."
}
@@ -0,0 +1,9 @@
package kotlin
public fun lazy<T>(initializer: () -> T): Lazy<T> = LazyImpl(initializer, Any())
public fun lazy<T>(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T> =
when (mode) {
LazyThreadSafetyMode.SYNCHRONIZED -> LazyImpl(initializer, Any())
LazyThreadSafetyMode.NONE -> UnsafeLazyImpl(initializer)
}