package be.ac.ulb.code.wit.rangeparser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Parameter {
	private final static Pattern escape = Pattern.compile("\\\\(.)");
	private String key, value;

	public Parameter(String key, String value) {
		super();
		this.key = key;
		
		//Fix the value of quoted strings
		if (value.startsWith("\"") && value.endsWith("\"")) {
			value = value.substring(1,value.length()-1);
			Matcher m = escape.matcher(value);
			value = m.replaceAll("$1");
		}
		this.value = value;
	}

	public String getKey() {
		return key;
	}

	public String getValue() {
		return value;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj == null) return false;
		if (obj == this) return true;
		if (this.getClass() != obj.getClass()) return false;
		
		Parameter other = (Parameter)obj;
		return (other.value.equals(value)) && (other.key.equals(key));
	}
	
	@Override
	public int hashCode() {
		String id = this.key + "=" + this.value;
		return id.hashCode();
	}
	
	@Override
	public String toString() {
		return this.key + "=" + this.value;
	}
}
