aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/plugability/LazyEvaluated.kt
blob: 17fad525f9a3b3eb8295234f250fb60fd2acf1a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package org.jetbrains.dokka.plugability

internal class LazyEvaluated<T : Any> private constructor(private val recipe: ((DokkaContext) -> T)? = null, private var value: T? = null) {

    @Synchronized
    internal fun get(context: DokkaContext): T {
        if(value == null) {
            value = recipe?.invoke(context)
        }
        return value ?: throw AssertionError("Incorrect initialized LazyEvaluated instance")
    }

    companion object {
        fun <T : Any> fromInstance(value: T) = LazyEvaluated(value = value)
        fun <T : Any> fromRecipe(recipe: (DokkaContext) -> T) = LazyEvaluated(recipe = recipe)
    }
}