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

import ru.dbotthepony.kommons.util.KOptional

interface InvalidableLazy<V> : Lazy<V> {
	fun invalidate()

	class Impl<V>(private val supplier: () -> V) : InvalidableLazy<V> {
		private var _value: KOptional<V> = KOptional.empty()

		override val value: V get() {
			_value.ifPresent {
				return it
			}.ifNotPresent {
				val v = supplier.invoke()
				_value = KOptional(v)
				return v
			}

			throw RuntimeException()
		}

		override fun invalidate() {
			_value = KOptional()
		}

		override fun isInitialized(): Boolean {
			return _value.isPresent
		}
	}
}
