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

import java.io.IOException;

import speiger.src.coreengine.utils.io.dataTag.DataTag;
import speiger.src.coreengine.utils.io.dataTag.compression.DataTagLimiter;
import speiger.src.coreengine.utils.io.dataTag.compression.TagCompressor;
import speiger.src.coreengine.utils.io.dataTag.compression.TagDecompressor;
import speiger.src.coreengine.utils.io.streams.ExtendedDataInput;
import speiger.src.coreengine.utils.io.streams.ExtendedDataOutput;

public class DoubleTag implements PrimitiveTag
{
	double value;
	
	public DoubleTag()
	{
	}
	
	public DoubleTag(double value)
	{
		this.value = value;
	}

	@Override
	public int getId()
	{
		return 7;
	}
	
	@Override
	public String getTypeName()
	{
		return "Double";
	}
	
	@Override
	public long countBytes(long input)
	{
		return input + 8L;
	}
	
	@Override
	public void write(ExtendedDataOutput output, TagCompressor compressor) throws IOException
	{
		output.writeDouble(value);
	}

	@Override
	public void read(ExtendedDataInput input, TagDecompressor decompressor, int depthLimit, DataTagLimiter limiter) throws IOException
	{
		value = input.readDouble();	
		limiter.countBytes(8);
	}
	
	@Override
	public DataTag copy()
	{
		return new DoubleTag(value);
	}
	
	@Override
	public int hashCode()
	{
		return Double.hashCode(value);
	}

	@Override
	public boolean equals(Object obj)
	{
		return obj instanceof DoubleTag && ((DoubleTag)obj).value == value;
	}

	@Override
	public String toString()
	{
		return value+"d";
	}
	
	@Override
	public String prettyPrint(String indent, int depth)
	{	
		return new StringBuilder().append(NUMBER_HIGHLIGHT).append(value).append(TYPE_HIGHLIGHT).append("d").append(RESET_HIGHLIGHT).toString();
	}
	
	@Override
	public byte asByte()
	{
		return (byte)value;
	}
	
	@Override
	public short asShort()
	{
		return (short)value;
	}
	
	@Override
	public int asInt()
	{
		return (int)value;
	}
	
	@Override
	public long asLong()
	{
		return (long)value;
	}
	
	@Override
	public float asFloat()
	{
		return (float)value;
	}
	
	@Override
	public double asDouble()
	{
		return value;
	}
}
