1 25 package org.archive.util; 26 27 import java.util.EmptyStackException ; 28 import java.util.Stack ; 29 import java.util.regex.Matcher ; 30 import java.util.regex.Pattern ; 31 32 38 public class PatternMatcherRecycler { 39 46 private final static int MAXIMUM_STACK_SIZE = 10; 47 48 private Pattern pattern; 49 private Stack <Matcher > matchers; 50 51 public PatternMatcherRecycler(Pattern p) { 52 this.pattern = p; 53 this.matchers = new Stack <Matcher >(); 54 } 55 56 public Pattern getPattern() { 57 return this.pattern; 58 } 59 60 68 public Matcher getMatcher(CharSequence input) { 69 if (input == null) { 70 throw new IllegalArgumentException ("CharSequence 'input' must not be null"); 71 } 72 try { 73 return ((Matcher )matchers.pop()).reset(input); 74 } catch (EmptyStackException e) { 75 return this.pattern.matcher(input); 76 } 77 } 78 79 85 public void freeMatcher(Matcher m) { 86 if(this.matchers.size() < MAXIMUM_STACK_SIZE) { 87 matchers.push(m); 88 } 89 } 90 } 91 92 | Popular Tags |