package ru.dbotthepony.kstarbound.util

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

class WriteOnce<V> : ReadWriteProperty<Any, V> {
	private var value: V? = null

	override fun getValue(thisRef: Any, property: KProperty<*>): V {
		return value ?: throw IllegalStateException("Property ${property.name} is not initialized")
	}

	override fun setValue(thisRef: Any, property: KProperty<*>, value: V) {
		if (this.value != null) {
			throw IllegalStateException("Already initialized")
		}

		this.value = value
	}
}
