001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.text;
034:
035: import java.util.*;
036:
037: import org.apache.oro.text.*;
038: import org.apache.oro.text.regex.*;
039:
040: public class MaximumCapacityPatternCache implements PatternCache {
041: PatternCompiler compiler;
042:
043: Map<String, Pattern> map = new HashMap<String, Pattern>();
044:
045: public MaximumCapacityPatternCache() {
046: this (new Perl5Compiler());
047: }
048:
049: public MaximumCapacityPatternCache(PatternCompiler compiler) {
050: this .compiler = compiler;
051: }
052:
053: public Pattern addPattern(String expression)
054: throws MalformedPatternException {
055: Pattern result = map.get(expression);
056:
057: if (result == null) {
058: result = compiler.compile(expression);
059: map.put(expression, result);
060: }
061:
062: return result;
063: }
064:
065: public Pattern addPattern(String expression, int options)
066: throws MalformedPatternException {
067: Pattern result = map.get(expression);
068:
069: if (result == null) {
070: result = compiler.compile(expression, options);
071: map.put(expression, result);
072: }
073:
074: return result;
075: }
076:
077: public Pattern getPattern(String expression)
078: throws MalformedCachePatternException {
079: Pattern result = map.get(expression);
080:
081: if (result == null) {
082: try {
083: result = compiler.compile(expression);
084: } catch (MalformedPatternException ex) {
085: throw new MalformedCachePatternException(ex
086: .getMessage());
087: }
088: map.put(expression, result);
089: }
090:
091: return result;
092: }
093:
094: public Pattern getPattern(String expression, int options)
095: throws MalformedCachePatternException {
096: Pattern result = map.get(expression);
097:
098: if (result == null) {
099: try {
100: result = compiler.compile(expression, options);
101: } catch (MalformedPatternException ex) {
102: throw new MalformedCachePatternException(ex
103: .getMessage());
104: }
105: map.put(expression, result);
106: }
107:
108: return result;
109: }
110:
111: public int size() {
112: return map.size();
113: }
114:
115: public int capacity() {
116: return 20;
117: }
118: }
|