1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.util; |
17 | |
|
18 | |
import java.util.HashMap; |
19 | |
import java.util.Map; |
20 | |
import java.util.regex.Matcher; |
21 | |
import java.util.regex.Pattern; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class PatternUtil { |
28 | |
|
29 | 1 | private static Map patternCache = new HashMap(); |
30 | |
|
31 | 0 | private PatternUtil() { |
32 | 0 | } |
33 | |
|
34 | |
public static Pattern getPattern(String regex) { |
35 | 6 | Pattern pattern = (Pattern) patternCache.get(regex); |
36 | 6 | if (pattern == null) { |
37 | 2 | pattern = Pattern.compile(regex); |
38 | 2 | patternCache.put(regex, pattern); |
39 | |
} |
40 | 6 | return pattern; |
41 | |
} |
42 | |
|
43 | |
public static boolean matches(String regex, CharSequence value) { |
44 | 2 | if (regex == null) { |
45 | 1 | throw new IllegalArgumentException("regex"); |
46 | |
} |
47 | 1 | Pattern pattern = getPattern(regex); |
48 | 1 | Matcher matcher = pattern.matcher(value); |
49 | 1 | return matcher.matches(); |
50 | |
} |
51 | |
|
52 | |
public static void clearPatternCache() { |
53 | 1 | patternCache.clear(); |
54 | 1 | } |
55 | |
} |