package common.tags;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

class TagString extends Tag {
	private String data;

	TagString() {
		this.data = "";
	}

	public TagString(String data) {
		if(data == null)
			throw new IllegalArgumentException("Eine Zeichenkette aus null ist nicht erlaubt");
		this.data = data;
	}

	void write(DataOutput output) throws IOException {
		output.writeUTF(this.data);
	}

	void read(DataInput input, int depth, SizeTracker tracker) throws IOException {
		tracker.read(36);
		this.data = input.readUTF();
		tracker.read(2 * this.data.length());
	}

	TagType getType() {
		return TagType.STRING;
	}

	public String toString() {
		return "\"" + this.data.replace("\"", "\\\"") + "\"";
	}

	public Tag copy() {
		return new TagString(this.data);
	}

	public boolean equals(Object other) {
		if(!super.equals(other))
			return false;
		TagString tag = (TagString)other;
		return this.data == null && tag.data == null || this.data != null && this.data.equals(tag.data);
	}

	public int hashCode() {
		return super.hashCode() ^ this.data.hashCode();
	}

	public String getString() {
		return this.data;
	}
}
