Coverage Report - org.seasar.teeda.core.util.PatternUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
PatternUtil
87%
13/15
100%
4/4
1.75
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 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" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 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  
  * @author shot
 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  
 }