// Path: monica-cluster/src/main/java/monica/cluster/router/parse/object/Host.java
// public class Host {
// 	private List<String> ips;
// 	private Operator ops;
// 
// 	public List<String> getIps() {
// 		return ips;
// 	}
// 
// 	public void setIps(List<String> ips) {
// 		this.ips = ips;
// 	}
// 
// 	public Operator getOps() {
// 		return ops;
// 	}
// 
// 	public void setOps(Operator ops) {
// 		this.ops = ops;
// 	}
// 
// 	public String toString() {
// 		StringBuilder stringBuilder = new StringBuilder();
// 		stringBuilder.append("host");
// 		if (ops.equals(Operator.INCLUDE)) {
// 			stringBuilder.append("=");
// 		} else {
// 			stringBuilder.append("!=");
// 		}
// 		for (String s : ips) {
// 			if (ips.indexOf(s) != (ips.size() - 1)) {
// 				stringBuilder.append(s + ",");
// 			} else {
// 				stringBuilder.append(s);
// 			}
// 		}
// 		return stringBuilder.toString();
// 	}
// }
// 
// Path: monica-cluster/src/main/java/monica/cluster/router/parse/object/Operator.java
// public enum Operator {
// 	INCLUDE, EXCLUDE
// }
// 
// Path: monica-cluster/src/main/java/monica/cluster/router/parse/object/Rule.java
// public class Rule implements Comparable<Rule> {
// 	private Host source;
// 	private Host destination;
// 	private int priority;
// 
// 	public Host getSource() {
// 		return source;
// 	}
// 
// 	public void setSource(Host source) {
// 		this.source = source;
// 	}
// 
// 	public Host getDestination() {
// 		return destination;
// 	}
// 
// 	public void setDestination(Host destination) {
// 		this.destination = destination;
// 	}
// 
// 	public int getPriority() {
// 		return priority;
// 	}
// 
// 	public void setPriority(int priority) {
// 
// 		this.priority = priority;
// 	}
// 
// 	public String toString() {
// 		if (null != source && null != destination) {
// 			return source + "=>" + destination;
// 		} else if (null != source && null == destination) {
// 			return source + "=>";
// 		} else if (null == source && null != destination) {
// 			return "=>" + destination;
// 		} else {
// 			return "";
// 		}
// 
// 	}
// 
// 	public int compareTo(Rule o) {
// 		if (this.priority < o.priority)
// 			return -1;
// 		return 1;
// 	}
// 
// }


import java.util.Arrays;
import monica.cluster.router.parse.object.Host;
import monica.cluster.router.parse.object.Operator;
import monica.cluster.router.parse.object.Rule;

package monica.cluster.router.parse;



/**
 * 
 * @author lucy@polarcoral.com
 *
 *         2017-08-29
 */
public class StringToObjectParser {

	public Rule parseRuleStringToRule(String rule) {
		int index = rule.lastIndexOf(",");
		int priorityIndex = rule.lastIndexOf("=");
		String hostString = rule.trim().substring(rule.indexOf("['") + 2, index - 1).trim();
		String[] hosts = hostString.split("=>");
		Rule r = new Rule();
		if (hostString.trim().indexOf("=>") == 0 && hosts.length > 0) {
			r.setDestination(this.parseHostStringToHost(hosts[0]));
		} else if (hostString.trim().indexOf("=>") == (hostString.trim().length() - 2) && hosts.length > 0) {
			r.setSource(this.parseHostStringToHost(hosts[0]));
		} else {
			r.setSource(this.parseHostStringToHost(hosts[0]));
			r.setSource(this.parseHostStringToHost(hosts[1]));
		}

	
		/*
		 * List<Rule> rList = new ArrayList<Rule>(); rList.sort(new
		 * Comparator<Rule>(){ public int compare(Rule o1, Rule o2) { return
		 * o1.compareTo(o2); }
		 * 
		 * });
		 */
		r.setPriority(Integer.valueOf(rule.substring(priorityIndex + 1, rule.length() - 1)));
		return r;
	}

	private Host parseHostStringToHost(String host) {
		Host h;
		if (null != host && !host.trim().equals("")) {
			h = new Host();
			if (host.indexOf("=") != -1) {
				if (host.indexOf("!") != -1) {
					h.setIps(Arrays.asList(host.substring(host.indexOf("!=")).split(",")));

// Path: monica-cluster/src/main/java/monica/cluster/router/parse/object/Host.java
// public class Host {
// 	private List<String> ips;
// 	private Operator ops;
// 
// 	public List<String> getIps() {
// 		return ips;
// 	}
// 
// 	public void setIps(List<String> ips) {
// 		this.ips = ips;
// 	}
// 
// 	public Operator getOps() {
// 		return ops;
// 	}
// 
// 	public void setOps(Operator ops) {
// 		this.ops = ops;
// 	}
// 
// 	public String toString() {
// 		StringBuilder stringBuilder = new StringBuilder();
// 		stringBuilder.append("host");
// 		if (ops.equals(Operator.INCLUDE)) {
// 			stringBuilder.append("=");
// 		} else {
// 			stringBuilder.append("!=");
// 		}
// 		for (String s : ips) {
// 			if (ips.indexOf(s) != (ips.size() - 1)) {
// 				stringBuilder.append(s + ",");
// 			} else {
// 				stringBuilder.append(s);
// 			}
// 		}
// 		return stringBuilder.toString();
// 	}
// }
// 
// Path: monica-cluster/src/main/java/monica/cluster/router/parse/object/Operator.java
// public enum Operator {
// 	INCLUDE, EXCLUDE
// }
// 
// Path: monica-cluster/src/main/java/monica/cluster/router/parse/object/Rule.java
// public class Rule implements Comparable<Rule> {
// 	private Host source;
// 	private Host destination;
// 	private int priority;
// 
// 	public Host getSource() {
// 		return source;
// 	}
// 
// 	public void setSource(Host source) {
// 		this.source = source;
// 	}
// 
// 	public Host getDestination() {
// 		return destination;
// 	}
// 
// 	public void setDestination(Host destination) {
// 		this.destination = destination;
// 	}
// 
// 	public int getPriority() {
// 		return priority;
// 	}
// 
// 	public void setPriority(int priority) {
// 
// 		this.priority = priority;
// 	}
// 
// 	public String toString() {
// 		if (null != source && null != destination) {
// 			return source + "=>" + destination;
// 		} else if (null != source && null == destination) {
// 			return source + "=>";
// 		} else if (null == source && null != destination) {
// 			return "=>" + destination;
// 		} else {
// 			return "";
// 		}
// 
// 	}
// 
// 	public int compareTo(Rule o) {
// 		if (this.priority < o.priority)
// 			return -1;
// 		return 1;
// 	}
// 
// }


// Path: monica-cluster/src/main/java/monica/cluster/router/parse/StringToObjectParser.java
import java.util.Arrays;
import monica.cluster.router.parse.object.Host;
import monica.cluster.router.parse.object.Operator;
import monica.cluster.router.parse.object.Rule;

package monica.cluster.router.parse;



/**
 * 
 * @author lucy@polarcoral.com
 *
 *         2017-08-29
 */
public class StringToObjectParser {

	public Rule parseRuleStringToRule(String rule) {
		int index = rule.lastIndexOf(",");
		int priorityIndex = rule.lastIndexOf("=");
		String hostString = rule.trim().substring(rule.indexOf("['") + 2, index - 1).trim();
		String[] hosts = hostString.split("=>");
		Rule r = new Rule();
		if (hostString.trim().indexOf("=>") == 0 && hosts.length > 0) {
			r.setDestination(this.parseHostStringToHost(hosts[0]));
		} else if (hostString.trim().indexOf("=>") == (hostString.trim().length() - 2) && hosts.length > 0) {
			r.setSource(this.parseHostStringToHost(hosts[0]));
		} else {
			r.setSource(this.parseHostStringToHost(hosts[0]));
			r.setSource(this.parseHostStringToHost(hosts[1]));
		}

	
		/*
		 * List<Rule> rList = new ArrayList<Rule>(); rList.sort(new
		 * Comparator<Rule>(){ public int compare(Rule o1, Rule o2) { return
		 * o1.compareTo(o2); }
		 * 
		 * });
		 */
		r.setPriority(Integer.valueOf(rule.substring(priorityIndex + 1, rule.length() - 1)));
		return r;
	}

	private Host parseHostStringToHost(String host) {
		Host h;
		if (null != host && !host.trim().equals("")) {
			h = new Host();
			if (host.indexOf("=") != -1) {
				if (host.indexOf("!") != -1) {
					h.setIps(Arrays.asList(host.substring(host.indexOf("!=")).split(",")));

					h.setOps(Operator.EXCLUDE);
