All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.squirrelgrip.util.InitOnceProperty.kt Maven / Gradle / Ivy

There is a newer version: 1.5.5
Show newest version
package com.github.squirrelgrip.util

import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

@Suppress("UNCHECKED_CAST")
class InitOnceProperty : ReadWriteProperty {

    private object EMPTY

    private var value: Any? = EMPTY

    override fun getValue(thisRef: Any, property: KProperty<*>): T {
        if (value == EMPTY) {
            throw IllegalStateException("Value isn't initialized")
        }
        return value as T
    }

    override fun setValue(thisRef: Any, property: KProperty<*>, value: T) {
        if (this.value != EMPTY) {
            throw IllegalStateException("Value is initialized")
        }
        this.value = value
    }
}

inline fun  initOnce(): ReadWriteProperty = InitOnceProperty()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy