Skip to content

Instantly share code, notes, and snippets.

@michael-simons
Created March 9, 2011 07:58
Show Gist options
  • Save michael-simons/861852 to your computer and use it in GitHub Desktop.
Save michael-simons/861852 to your computer and use it in GitHub Desktop.
package ac.simons.utils;
import java.util.Comparator;
/**
* @author michael.simons, 2011-02-15
*/
public class ExtendedAntPathMatcher implements PathMatcher {
private final AntPathMatcher delegate = new AntPathMatcher();
@Override
public boolean isPattern(String path) {
return this.delegate.isPattern(path);
}
@Override
public boolean match(String pattern, String path) {
return this.delegate.match(pattern, path);
}
@Override
public boolean matchStart(String pattern, String path) {
return this.matchStart(pattern, path);
}
@Override
public String extractPathWithinPattern(String pattern, String path) {
return this.delegate.extractPathWithinPattern(pattern, path);
}
@Override
public Map<String, String> extractUriTemplateVariables(String pattern, String path) {
return this.delegate.extractUriTemplateVariables(pattern, path);
}
@Override
public Comparator<String> getPatternComparator(final String path) {
final Comparator<String> delegateComparator = this.delegate.getPatternComparator(path);
return new Comparator<String>() {
@Override
public int compare(String pattern1, String pattern2) {
int rv = 0;
if(pattern1 == null && pattern2 == null)
rv = 0;
else if (pattern1 == null)
rv = 1;
else if (pattern2 == null)
rv = -1;
boolean pattern1EqualsPath = pattern1.equals(path);
boolean pattern2EqualsPath = pattern2.equals(path);
if (pattern1EqualsPath && pattern2EqualsPath)
rv = 0;
else if(pattern1EqualsPath)
rv = -1;
else if (pattern2EqualsPath)
rv = 1;
else {
int sign = 1;
String s1 = pattern1;
String s2 = pattern2;
if(s1.length() >= s2.length()) {
sign = -1;
s1 = pattern2;
s2 = pattern1;
}
s1 = s1.replaceAll(Pattern.quote(".*")+"$", "");
if(s2.startsWith(s1) && ".{format}".equals(s2.replaceAll(Pattern.quote(s1), "")))
rv = sign*1;
else
rv = delegateComparator.compare(pattern1, pattern2);
}
return rv;
}
};
}
@Override
public String combine(String pattern1, String pattern2) {
return this.delegate.combine(pattern1, pattern2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment