package com.parship.roperty;

import com.parship.commons.util.Ensure;


/**
 * @author mfinsterwalder
 * @since 2013-04-09 18:20
 */
class DomainPattern implements Comparable<DomainPattern> {
	private final String patternStr;
	private final int ordering;
	private final Object value;

	public DomainPattern(final String domainPattern, final int order, Object value) {
		Ensure.notNull(domainPattern, "domainPattern");
		Ensure.notNull(value, "value");
		this.patternStr = domainPattern;
		this.ordering = order;
		this.value = value;
	}

	@Override
	public int compareTo(final DomainPattern other) {
		int order = this.ordering - other.ordering;
		if (order == 0) {
			return patternStr.compareTo(other.patternStr);
		}
		return order;
	}

	@Override
	public String toString() {
		return "DomainPattern{" +
			"pattern=" + patternStr +
			", ordering=" + ordering +
			", value=" + value +
			'}';
	}

	public Object getValue() {
		return value;
	}

	public boolean matches(final String domainStr) {
		boolean equals = patternStr.equals(domainStr.substring(0, Math.min(domainStr.length(), patternStr.length())));
		if (!equals && patternStr.contains("*")) {
			String patternStr = domainStr.replaceAll("\\|", "\\\\|").replaceAll("\\*", "[^|]*") + ".*";
			return domainStr.matches(patternStr);
		}
		return equals;
	}
}

@@ -1011,16 +1011,263 @@
 equals =
+ prefixEquals(patternStr, domainStr);%0A%09%09if (!equals && patternStr.contains(%22*%22)) %7B%0A%09%09%09return domainStr.matches(toRegEx(patternStr));%0A%09%09%7D%0A%09%09return equals;%0A%09%7D%0A%0A%09private boolean prefixEquals(final String patternStr, final String domainStr) %7B%0A%09%09return
  pattern
@@ -1358,74 +1358,69 @@
 );%0A%09
-%09if (!equals && patternStr.contains(%22*%22)) %7B%0A%09%09%09String patternStr =
+%7D%0A%0A%09private String toRegEx(final String domainStr) %7B%0A%09%09return
  dom
@@ -1492,70 +1492,8 @@
 *%22;%0A
-%09%09%09return domainStr.matches(patternStr);%0A%09%09%7D%0A%09%09return equals;%0A
 %09%7D%0A%7D

prefix equals extracted