aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt
blob: 979f7a7646eded36a2cf572ed2bca4dec8e672ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package ca.allanwang.kau.kotlin

import java.io.Serializable
import kotlin.reflect.KProperty

/**
 * Created by Allan Wang on 2017-05-30.
 *
 * Lazy delegate that can be invalidated if needed
 * https://stackoverflow.com/a/37294840/4407321
 */
internal object UNINITIALIZED

fun <T : Any> lazyResettable(initializer: () -> T): LazyResettable<T> = LazyResettable<T>(initializer)

class LazyResettable<T : Any>(private val initializer: () -> T, lock: Any? = null) : ILazyResettable<T>, Serializable {
    @Volatile
    private var _value: Any = UNINITIALIZED
    private val lock = lock ?: this

    override fun invalidate() {
        _value = UNINITIALIZED
    }

    override val value: T
        get() {
            val _v1 = _value
            if (_v1 !== UNINITIALIZED)
                @Suppress("UNCHECKED_CAST")
                return _v1 as T

            return synchronized(lock) {
                val _v2 = _value
                if (_v2 !== UNINITIALIZED) {
                    @Suppress("UNCHECKED_CAST")
                    _v2 as T
                } else {
                    val typedValue = initializer()
                    _value = typedValue
                    typedValue
                }
            }
        }

    override fun isInitialized(): Boolean = _value !== UNINITIALIZED

    override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet."

    operator fun setValue(any: Any, property: KProperty<*>, t: T) {
        _value = t
    }
}

interface ILazyResettable<out T> : Lazy<T> {
    fun invalidate()
}

interface ILazyResettableRegistry {
    fun <T : Any> lazy(initializer: () -> T): LazyResettable<T>
    fun <T : Any> add(resettable: LazyResettable<T>): LazyResettable<T>
    fun invalidateAll()
    fun clear()
}

/**
 * The following below is a helper class that registers all resettables into a weakly held list
 * All resettables can therefore be invalidated at once
 */
class LazyResettableRegistry : ILazyResettableRegistry {

    var lazyRegistry: MutableList<LazyResettable<*>> = mutableListOf()

    override fun <T : Any> lazy(initializer: () -> T): LazyResettable<T> = add(lazyResettable(initializer))

    override fun <T : Any> add(resettable: LazyResettable<T>): LazyResettable<T> {
        lazyRegistry.add(resettable)
        return resettable
    }

    override fun invalidateAll() = lazyRegistry.forEach { it.invalidate() }

    override fun clear() = lazyRegistry.clear()
}