package ru.dbotthepony.mc.otm.core.util

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

class WriteOnce<V>(private val customMessage: String? = null) : ReadWriteProperty<Any?, V> {
	private var value: V? = null

	override fun getValue(thisRef: Any?, property: KProperty<*>): V {
		return checkNotNull(value) { 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
	}
}
