package link;

import exeptions.IllegalValueObjectArgument;

import java.net.MalformedURLException;
import java.net.URL;

public class LinkUrl {

	private final URL url;

	public LinkUrl(String urlString) {
		try {
			this.url = new URL(urlString);
		}
		catch (MalformedURLException e) {
			throw new IllegalValueObjectArgument(e.getMessage());
		}
	}

	public URL getUrl() {
		return url;
	}

	public boolean hostEquals(LinkUrl other) {
		return this.url.getHost().equals(other.getUrl().getHost());
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		LinkUrl linkUrl = (LinkUrl) o;

		return url != null ? url.toString().equals(linkUrl.url.toString()) : linkUrl.url == null;
	}

	@Override
	public int hashCode() {
		return url != null ? url.toString().hashCode() : 0;
	}

	@Override
	public String toString() {
		return url.toString();
	}
}
