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

import java.util.function.Supplier
import java.util.stream.Stream

class SupplierList<T> : AbstractList<T> {
	private val getters: Array<() -> T>

	constructor(vararg getters: () -> T) : super() {
		this.getters = Array(getters.size) { getters[it] }
	}

	constructor(getters: Collection<() -> T>) : super() {
		val iterator = getters.iterator()
		this.getters = Array(getters.size) { iterator.next() }
	}

	constructor(getters: Stream<() -> T>) : super() {
		this.getters = getters.toArray(::arrayOfNulls)
	}

	constructor(vararg getters: Supplier<T>) : super() {
		this.getters = Array(getters.size) { getters[it]::get }
	}

	constructor(size: Int, provider: (Int) -> () -> T) {
		this.getters = Array(size, provider)
	}

	override val size: Int
		get() = getters.size

	override fun get(index: Int): T {
		return getters[index].invoke()
	}
}
