package org.kne.cloud.network.scanner;

import java.io.Serializable;
import java.net.UnknownHostException;
import java.util.Objects;

import org.kne.cloud.network.MultiProtocolSocketAddress;

public class ScanRange implements Serializable {
	public ScanRange(String range) throws UnknownHostException {
		setMsaString(range);
	}
	protected void setMsaString(String range) throws UnknownHostException {
		String[]sp=range.split("~");
		MultiProtocolSocketAddress begin=new MultiProtocolSocketAddress(sp[0]);
		MultiProtocolSocketAddress end;
		if(sp.length>1) {
		end=new MultiProtocolSocketAddress(sp[1]);
		}else {
			end=new MultiProtocolSocketAddress(sp[0]);
		}
		setMsa(begin, end);
	}
	public ScanRange(MultiProtocolSocketAddress begin, MultiProtocolSocketAddress end) throws UnknownHostException {
		setMsa(begin,end);
		
	}
	
	protected void setMsa(MultiProtocolSocketAddress begin, MultiProtocolSocketAddress end) throws UnknownHostException {
		this.addressRange=new InetAddressRange(begin.getHost(), end.getHost());
		this.portRange=new PortRange(begin.getPort(), end.getPort());
	}
	public ScanRange(InetAddressRange addressRange, PortRange portRange) {
		super();
		this.addressRange = addressRange;
		this.portRange = portRange;
	}
	private static final long serialVersionUID = 1L;

	protected InetAddressRange addressRange;
	protected PortRange portRange;
	public InetAddressRange getAddressRange() {
		return addressRange;
	}
	public PortRange getPortRange() {
		return portRange;
	}
	
	public String toString() {
		return new MultiProtocolSocketAddress(addressRange.getBeginHost(), portRange.getBegin())+"~"+new MultiProtocolSocketAddress(addressRange.getEndHost(), portRange.getEnd());
	}
	@Override
	public int hashCode() {
		return Objects.hash(addressRange, portRange);
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ScanRange other = (ScanRange) obj;
		return Objects.equals(addressRange, other.addressRange) && Objects.equals(portRange, other.portRange);
	}
}
