1 package org.apache.oro.text; 2 3 59 60 import java.util.*; 61 62 import org.apache.oro.text.regex.*; 63 import org.apache.oro.util.*; 64 65 82 public abstract class GenericPatternCache implements PatternCache { 83 PatternCompiler _compiler; 84 Cache _cache; 85 86 90 public static final int DEFAULT_CAPACITY = 20; 91 92 101 GenericPatternCache(Cache cache, PatternCompiler compiler) { 102 _cache = cache; 103 _compiler = compiler; 104 } 105 106 107 132 public final synchronized Pattern addPattern(String expression, int options) 133 throws MalformedPatternException 134 { 135 Object obj; 136 Pattern pattern; 137 138 obj = _cache.getElement(expression); 139 140 if(obj != null) { 141 pattern = (Pattern)obj; 142 143 if(pattern.getOptions() == options) 144 return pattern; 145 } 146 147 pattern = _compiler.compile(expression, options); 148 _cache.addElement(expression, pattern); 149 150 return pattern; 151 } 152 153 154 162 public final synchronized Pattern addPattern(String expression) 163 throws MalformedPatternException 164 { 165 return addPattern(expression, 0); 166 } 167 168 169 190 public final synchronized Pattern getPattern(String expression, int options) 191 throws MalformedCachePatternException 192 { 193 Pattern result = null; 194 195 try { 196 result = addPattern(expression, options); 197 } catch(MalformedPatternException e) { 198 throw new MalformedCachePatternException("Invalid expression: " + 199 expression + "\n" + 200 e.getMessage()); 201 } 202 203 return result; 204 } 205 206 207 213 public final synchronized Pattern getPattern(String expression) 214 throws MalformedCachePatternException 215 { 216 return getPattern(expression, 0); 217 } 218 219 220 228 public final int size() { return _cache.size(); } 229 230 235 public final int capacity() { return _cache.capacity(); } 236 } 237 | Popular Tags |