From 63e8865123573ae0d366d589133827eee55b4468 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 20 Jan 2016 06:22:35 +0300 Subject: [PATCH] Make Lazy an interface open to implementation. #KT-9287 Fixed --- libraries/stdlib/src/kotlin/util/Lazy.kt | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/libraries/stdlib/src/kotlin/util/Lazy.kt b/libraries/stdlib/src/kotlin/util/Lazy.kt index c7d6979130b..317c98a81a1 100644 --- a/libraries/stdlib/src/kotlin/util/Lazy.kt +++ b/libraries/stdlib/src/kotlin/util/Lazy.kt @@ -10,11 +10,15 @@ import kotlin.reflect.KProperty * * To create an instance of [Lazy] use the [lazy] function. */ -public abstract class Lazy internal constructor() { - /** Gets the lazily initialized value of the current Lazy instance. */ - public abstract val value: T - /** Returns `true` if a value for this Lazy instance has been already initialized, and `false` otherwise. - * Once this function has returned `true` it stays `true` for the rest of lifetime of this Lazy instance. +public interface Lazy { + /** + * Gets the lazily initialized value of the current Lazy instance. + * Once the value was initialized it must not change during the rest of lifetime of this Lazy instance. + */ + public abstract val value: T + /** + * Returns `true` if a value for this Lazy instance has been already initialized, and `false` otherwise. + * Once this function has returned `true` it stays `true` for the rest of lifetime of this Lazy instance. */ public abstract fun isInitialized(): Boolean } @@ -103,7 +107,7 @@ public enum class LazyThreadSafetyMode { private object UNINITIALIZED_VALUE -private class SynchronizedLazyImpl(initializer: () -> T, lock: Any? = null) : Lazy(), Serializable { +private class SynchronizedLazyImpl(initializer: () -> T, lock: Any? = null) : Lazy, Serializable { private var initializer: (() -> T)? = initializer @Volatile private var _value: Any? = UNINITIALIZED_VALUE // final field is required to enable safe publication of constructed instance @@ -138,7 +142,7 @@ private class SynchronizedLazyImpl(initializer: () -> T, lock: Any? = nul } // internal to be called from lazy in JS -internal class UnsafeLazyImpl(initializer: () -> T) : Lazy(), Serializable { +internal class UnsafeLazyImpl(initializer: () -> T) : Lazy, Serializable { private var initializer: (() -> T)? = initializer private var _value: Any? = UNINITIALIZED_VALUE @@ -158,7 +162,7 @@ internal class UnsafeLazyImpl(initializer: () -> T) : Lazy(), Serializ private fun writeReplace(): Any = InitializedLazyImpl(value) } -private class InitializedLazyImpl(override val value: T) : Lazy(), Serializable { +private class InitializedLazyImpl(override val value: T) : Lazy, Serializable { override fun isInitialized(): Boolean = true @@ -167,7 +171,7 @@ private class InitializedLazyImpl(override val value: T) : Lazy(), Ser } @kotlin.jvm.JvmVersion -private class SafePublicationLazyImpl(initializer: () -> T) : Lazy(), Serializable { +private class SafePublicationLazyImpl(initializer: () -> T) : Lazy, Serializable { private var initializer: (() -> T)? = initializer @Volatile private var _value: Any? = UNINITIALIZED_VALUE // this final field is required to enable safe publication of constructed instance