View Javadoc

1   /*
2    * Copyright 2008 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
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  }