package ru.dbotthepony.mc.otm.core.collect

import com.google.common.collect.ImmutableSet
import java.util.stream.Stream

class SupplierMap<K, T> : AbstractMap<K, T> {
	override val entries: Set<Map.Entry<K, T>>

	constructor(vararg mValues: Pair<K, () -> T>) : super() {
		entries = ImmutableSet.copyOf(mValues.map { Entry(it.first, it.second) })
	}

	constructor(mValues: Collection<Pair<K, () -> T>>) : super() {
		entries = ImmutableSet.copyOf(mValues.map { Entry(it.first, it.second) })
	}

	constructor(mValues: Stream<Pair<K, () -> T>>) : super() {
		entries = mValues.map { Entry(it.first, it.second) }.collect(ImmutableSet.toImmutableSet())
	}

	constructor(mValues: Map<K, () -> T>) : super() {
		entries = ImmutableSet.copyOf(mValues.map { Entry(it.key, it.value) })
	}

	private inner class Entry(
		override val key: K,
		private val getter: () -> T
	) : Map.Entry<K, T> {
		override val value: T
			get() = getter.invoke()
	}
}
