View Javadoc
1   /*
2    * Copyright (C) 2016 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.base;
16  
17  import com.google.common.annotations.GwtIncompatible;
18  import java.io.Serializable;
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  
22  /**
23   * A regex pattern implementation which is backed by the {@link Pattern}.
24   */
25  @GwtIncompatible
26  final class JdkPattern extends CommonPattern implements Serializable {
27    private final Pattern pattern;
28  
29    JdkPattern(Pattern pattern) {
30      this.pattern = Preconditions.checkNotNull(pattern);
31    }
32  
33    @Override
34    CommonMatcher matcher(CharSequence t) {
35      return new JdkMatcher(pattern.matcher(t));
36    }
37  
38    @Override
39    String pattern() {
40      return pattern.pattern();
41    }
42  
43    @Override
44    int flags() {
45      return pattern.flags();
46    }
47  
48    @Override
49    public String toString() {
50      return pattern.toString();
51    }
52  
53    @Override
54    public int hashCode() {
55      return pattern.hashCode();
56    }
57  
58    @Override
59    public boolean equals(Object o) {
60      if (!(o instanceof JdkPattern)) {
61        return false;
62      }
63      return pattern.equals(((JdkPattern) o).pattern);
64    }
65  
66    private static final class JdkMatcher extends CommonMatcher {
67      final Matcher matcher;
68  
69      JdkMatcher(Matcher matcher) {
70        this.matcher = Preconditions.checkNotNull(matcher);
71      }
72  
73      @Override
74      boolean matches() {
75        return matcher.matches();
76      }
77  
78      @Override
79      boolean find() {
80        return matcher.find();
81      }
82  
83      @Override
84      boolean find(int index) {
85        return matcher.find(index);
86      }
87  
88      @Override
89      String replaceAll(String replacement) {
90        return matcher.replaceAll(replacement);
91      }
92  
93      @Override
94      int end() {
95        return matcher.end();
96      }
97  
98      @Override
99      int start() {
100       return matcher.start();
101     }
102   }
103 
104   private static final long serialVersionUID = 0;
105 }