package speiger.src.coreengine.utils.io.dataTag.parsing;

import java.util.Map;

import speiger.src.collections.objects.maps.impl.hash.Object2ObjectOpenHashMap;
import speiger.src.coreengine.utils.io.dataTag.parsing.DataTagIO.IOEntry;
import speiger.src.coreengine.utils.io.dataTag.parsing.deserialize.IDataDeserializer;
import speiger.src.coreengine.utils.io.dataTag.parsing.serialize.IDataSerializer;

public class DataTagIOBuilder
{
	Map<Class<?>, IOEntry<?>> map = new Object2ObjectOpenHashMap<>();
	
	public DataTagIO build()
	{
		return new DataTagIO(map);
	}
	
	public static DataTagIOBuilder copyOf(DataTagIO io)
	{
		DataTagIOBuilder builder = new DataTagIOBuilder();
		builder.map.putAll(io.entries);
		return builder;
	}
	
	@SuppressWarnings("unchecked")
	public <T> DataTagIOBuilder add(Class<T> type, Object obj)
	{
		IDataSerializer<T> serializer = obj instanceof IDataSerializer ? (IDataSerializer<T>)obj : null;
		IDataDeserializer<T> deserializer = obj instanceof IDataDeserializer ? (IDataDeserializer<T>)obj : null;
		if(serializer == null || deserializer == null)
		{
			return this;
		}
		map.put(type, new IOEntry<>(serializer, deserializer));
		return this;
	}
}
