From b8328732031a47f79df43cfd4fce1acc70aee362 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 27 Oct 2024 10:35:20 +0700 Subject: [PATCH] Fix WriteOnce not properly handling nulls --- .../ru/dbotthepony/mc/otm/core/util/WriteOnce.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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) } }