diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/WriteOnce.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/WriteOnce.kt index bda267313..7f2148bdf 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/WriteOnce.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/WriteOnce.kt @@ -1,17 +1,19 @@ package ru.dbotthepony.mc.otm.core.util +import ru.dbotthepony.kommons.util.KOptional import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty class WriteOnce(private val customMessage: String? = null) : ReadWriteProperty { - private var value: V? = null + private var value: KOptional = KOptional() override fun getValue(thisRef: Any?, property: KProperty<*>): V { - return checkNotNull(value) { customMessage ?: "Property ${property.name} is not initialized" } + value.ifPresent { return it } + throw IllegalStateException(customMessage ?: "Property ${property.name} is not initialized") } override fun setValue(thisRef: Any?, property: KProperty<*>, value: V) { - check(this.value == null) { "Property ${property.name} is already initialized" } - this.value = value + this.value.ifPresent { throw IllegalStateException("Property ${property.name} is already initialized") } + this.value = KOptional(value) } }