package org.nanopub.extra.server;

import net.trustyuri.TrustyUriUtils;

// TODO Make pattern processing more efficient
public class SurfacePatterns {

	private final String[] uriPattern;
	private final String[] hashPattern;

	public SurfacePatterns(String uriPattern, String hashPattern) {
		if (uriPattern.isEmpty()) {
			this.uriPattern = null;
		} else {
			this.uriPattern = uriPattern.split(" ");
		}
		if (hashPattern.isEmpty()) {
			this.hashPattern = null;
		} else {
			this.hashPattern = hashPattern.split(" ");
		}
	}

	public SurfacePatterns(ServerInfo serverInfo) {
		this(serverInfo.getUriPattern(), serverInfo.getHashPattern());
	}

	public boolean matchesUri(String uri) {
		if (!matchesHash(TrustyUriUtils.getArtifactCode(uri))) {
			return false;
		}
		if (uriPattern == null) return true;
		boolean match = false;
		for (String p : uriPattern) {
			if (uri.startsWith(p)) {
				match = true;
				break;
			}
		}
		return match;
	}

	public boolean matchesHash(String artifactCode) {
		if (hashPattern == null) return true;
		boolean match = false;
		String hash = TrustyUriUtils.getDataPart(artifactCode);
		for (String p : hashPattern) {
			if (hash.startsWith(p)) {
				match = true;
				break;
			}
		}
		return match;
	}

}

@@ -1239,10 +1239,1489 @@
 ch;%0A%09%7D%0A%0A
+%09public boolean overlapsWith(SurfacePatterns other) %7B%0A%09%09// TODO This is inefficient: improve!%0A%09%09if (uriPattern != null && other.uriPattern != null) %7B%0A%09%09%09boolean overlapEncountered = false;%0A%09%09%09for (String p1 : uriPattern) %7B%0A%09%09%09%09for (String p2 : other.uriPattern) %7B%0A%09%09%09%09%09if (p1.startsWith(p2) %7C%7C p2.startsWith(p1)) %7B%0A%09%09%09%09%09%09overlapEncountered = true;%0A%09%09%09%09%09%09break;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09if (overlapEncountered) break;%0A%09%09%09%7D%0A%09%09%09if (!overlapEncountered) return false;%0A%09%09%7D%0A%09%09if (hashPattern != null && other.hashPattern != null) %7B%0A%09%09%09boolean overlapEncountered = false;%0A%09%09%09for (String p1 : hashPattern) %7B%0A%09%09%09%09for (String p2 : other.hashPattern) %7B%0A%09%09%09%09%09if (p1.startsWith(p2) %7C%7C p2.startsWith(p1)) %7B%0A%09%09%09%09%09%09overlapEncountered = true;%0A%09%09%09%09%09%09break;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09if (overlapEncountered) break;%0A%09%09%09%7D%0A%09%09%09if (!overlapEncountered) return false;%0A%09%09%7D%0A%09%09return true;%0A%09%7D%0A%0A%09public static boolean matchesUri(String uri, String uriPattern, String hashPattern) %7B%0A%09%09return new SurfacePatterns(uriPattern, hashPattern).matchesUri(uri);%0A%09%7D%0A%0A%09public static boolean matchesUri(String uri, ServerInfo serverInfo) %7B%0A%09%09return new SurfacePatterns(serverInfo).matchesUri(uri);%0A%09%7D%0A%0A%09public static boolean matchesHash(String artifactCode, String uriPattern, String hashPattern) %7B%0A%09%09return new SurfacePatterns(uriPattern, hashPattern).matchesHash(artifactCode);%0A%09%7D%0A%0A%09public static boolean matchesHash(String artifactCode, ServerInfo serverInfo) %7B%0A%09%09return new SurfacePatterns(serverInfo).matchesHash(artifactCode);%0A%09%7D%0A%0A
 %7D%0A

Add methods