1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package nl.hippo.client.webapp.matchers;
17
18 import java.util.regex.Pattern;
19
20 import nl.hippo.client.api.ClientException;
21 import nl.hippo.client.webapp.Request;
22 import nl.hippo.client.webdav.WebdavResponse;
23
24 public abstract class Matcher {
25
26 protected Pattern regexp;
27
28 public Matcher(String pattern) {
29 this.regexp = Pattern.compile(pattern);
30 }
31
32 public boolean matches(Request request) {
33 return regexp.matcher(request.getPath()).matches();
34 }
35
36 public abstract WebdavResponse execute(Request request) throws ClientException;
37
38 protected interface MatcherCallback {
39 WebdavResponse doExec() throws ClientException;
40 }
41
42 protected WebdavResponse wrapExec(MatcherCallback callback) throws ClientException {
43 return callback.doExec();
44 }
45
46
47 public String toString() {
48 return getClass().getName() + " (" + regexp.toString() + ")";
49 }
50
51 /**
52 * equals and hashCode are based only on the regular expression.
53 */
54 public boolean equals(Object obj) {
55 boolean eq;
56 if (obj == null) {
57 eq = false;
58 } else if (!(obj instanceof Matcher)) {
59 eq = false;
60 } else {
61 Matcher rhs = (Matcher) obj;
62 eq = regexp.pattern().equals(rhs.regexp.pattern());
63 }
64 return eq;
65 }
66
67 /**
68 * equals and hashCode are only based on the regular expression exposed
69 * by the concrete implementation.
70 */
71 public int hashCode() {
72 return regexp.pattern().hashCode();
73 }
74
75 }