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

import net.minecraft.nbt.ListTag
import net.minecraft.nbt.Tag
import java.util.function.BiConsumer
import java.util.function.BinaryOperator
import java.util.function.Function
import java.util.function.Supplier
import java.util.stream.Collector
import java.util.stream.Collector.Characteristics

object ListTagCollector : Collector<Tag, ListTag, ListTag> {
	override fun supplier(): Supplier<ListTag> {
		return Supplier { ListTag() }
	}

	override fun accumulator(): BiConsumer<ListTag, Tag> {
		return BiConsumer { t, u -> t.add(u) }
	}

	override fun combiner(): BinaryOperator<ListTag> {
		return BinaryOperator { t, u ->
			if (t.size >= u.size) {
				t.addAll(u)
				t
			} else {
				u.addAll(t)
				u
			}
		}
	}

	override fun finisher(): Function<ListTag, ListTag> {
		return Function.identity()
	}

	private val chars = setOf(Characteristics.IDENTITY_FINISH)

	override fun characteristics(): Set<Characteristics> {
		return chars
	}
}
