Lazy: Rename valueCreated property to isInitialized() function.

Notes about behavior of Lazy when initializer throws an exception.
This commit is contained in:
Ilya Gorbunov
2015-06-17 22:44:08 +03:00
parent ecd131a7a1
commit b71fe4cd25
4 changed files with 17 additions and 11 deletions
+8 -6
View File
@@ -5,12 +5,14 @@ import java.io.Serializable
/** /**
* Represents a value with lazy initialization. * Represents a value with lazy initialization.
*
* To create an instance of [Lazy] use the [lazy] function.
*/ */
public interface Lazy<out T> { public interface Lazy<out T> {
/** Gets the lazily initialized value of the current Lazy instance. */ /** Gets the lazily initialized value of the current Lazy instance. */
public val value: T public val value: T
/** Returns `true` if a value for this Lazy instance has been already initialized. */ /** Returns `true` if a value for this Lazy instance has been already initialized. */
public val valueCreated: Boolean public fun isInitialized(): Boolean
} }
/** /**
@@ -67,9 +69,9 @@ private class LazyImpl<out T>(initializer: () -> T) : Lazy<T>, Serializable {
} }
} }
override val valueCreated: Boolean get() = _value !== UNINITIALIZED_VALUE override fun isInitialized(): Boolean = _value !== UNINITIALIZED_VALUE
override fun toString(): String = if (valueCreated) value.toString() else "Lazy value not initialized yet." override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet."
private fun writeReplace(): Any = InitializedLazyImpl(value) private fun writeReplace(): Any = InitializedLazyImpl(value)
} }
@@ -88,16 +90,16 @@ private class UnsafeLazyImpl<out T>(initializer: () -> T) : Lazy<T>, Serializabl
return _value as T return _value as T
} }
override val valueCreated: Boolean get() = _value !== UNINITIALIZED_VALUE override fun isInitialized(): Boolean = _value !== UNINITIALIZED_VALUE
override fun toString(): String = if (valueCreated) value.toString() else "Lazy value not initialized yet." override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet."
private fun writeReplace(): Any = InitializedLazyImpl(value) private fun writeReplace(): Any = InitializedLazyImpl(value)
} }
private class InitializedLazyImpl<out T>(override val value: T) : Lazy<T>, Serializable { private class InitializedLazyImpl<out T>(override val value: T) : Lazy<T>, Serializable {
override val valueCreated: Boolean get() = true override fun isInitialized(): Boolean = true
override fun toString(): String = value.toString() override fun toString(): String = value.toString()
@@ -5,6 +5,8 @@ package kotlin
* Initializes a new instance of the [Lazy] that uses the specified initialization function [initializer] * Initializes a new instance of the [Lazy] that uses the specified initialization function [initializer]
* and the default thread-safety mode [LazyThreadSafetyMode.SYNCHRONIZED]. * and the default thread-safety mode [LazyThreadSafetyMode.SYNCHRONIZED].
* *
* If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.
*
* Note that the returned instance uses itself to synchronize on. Do not synchronize from external code on * Note that the returned instance uses itself to synchronize on. Do not synchronize from external code on
* the returned instance as it may cause accidental deadlock. Also this behavior can be changed in the future. * the returned instance as it may cause accidental deadlock. Also this behavior can be changed in the future.
*/ */
@@ -14,6 +16,8 @@ public fun lazy<T>(initializer: () -> T): Lazy<T> = LazyImpl(initializer)
* Initializes a new instance of the [Lazy] that uses the specified initialization function [initializer] * Initializes a new instance of the [Lazy] that uses the specified initialization function [initializer]
* and thread-safety [mode]. * and thread-safety [mode].
* *
* If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.
*
* Note that when the [LazyThreadSafetyMode.SYNCHRONIZED] mode is specified the returned instance uses itself * Note that when the [LazyThreadSafetyMode.SYNCHRONIZED] mode is specified the returned instance uses itself
* to synchronize on. Do not synchronize from external code on the returned instance as it may cause accidental deadlock. * to synchronize on. Do not synchronize from external code on the returned instance as it may cause accidental deadlock.
* Also this behavior can be changed in the future. * Also this behavior can be changed in the future.
+3 -3
View File
@@ -11,10 +11,10 @@ class LazyJVMTest {
test fun lazyInitializationForcedOnSerialization() { test fun lazyInitializationForcedOnSerialization() {
for(mode in listOf(LazyThreadSafetyMode.SYNCHRONIZED, LazyThreadSafetyMode.NONE)) { for(mode in listOf(LazyThreadSafetyMode.SYNCHRONIZED, LazyThreadSafetyMode.NONE)) {
val lazy = lazy(mode) { "initialized" } val lazy = lazy(mode) { "initialized" }
assertFalse(lazy.valueCreated) assertFalse(lazy.isInitialized())
val lazy2 = serializeAndDeserialize(lazy) val lazy2 = serializeAndDeserialize(lazy)
assertTrue(lazy.valueCreated) assertTrue(lazy.isInitialized())
assertTrue(lazy2.valueCreated) assertTrue(lazy2.isInitialized())
assertEquals(lazy.value, lazy2.value) assertEquals(lazy.value, lazy2.value)
} }
} }
+2 -2
View File
@@ -22,12 +22,12 @@ class LazyTest {
var callCount = 0 var callCount = 0
val lazyInt = lazy { ++callCount } val lazyInt = lazy { ++callCount }
assertFalse(lazyInt.valueCreated) assertFalse(lazyInt.isInitialized())
assertEquals(0, callCount) assertEquals(0, callCount)
lazyInt.value lazyInt.value
assertTrue(lazyInt.valueCreated) assertTrue(lazyInt.isInitialized())
} }