001: /*
002: * $Id: GenericPatternCache.java,v 1.7 2003/11/07 20:16:24 dfs Exp $
003: *
004: * ====================================================================
005: * The Apache Software License, Version 1.1
006: *
007: * Copyright (c) 2000 The Apache Software Foundation. All rights
008: * reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions
012: * are met:
013: *
014: * 1. Redistributions of source code must retain the above copyright
015: * notice, this list of conditions and the following disclaimer.
016: *
017: * 2. Redistributions in binary form must reproduce the above copyright
018: * notice, this list of conditions and the following disclaimer in
019: * the documentation and/or other materials provided with the
020: * distribution.
021: *
022: * 3. The end-user documentation included with the redistribution,
023: * if any, must include the following acknowledgment:
024: * "This product includes software developed by the
025: * Apache Software Foundation (http://www.apache.org/)."
026: * Alternately, this acknowledgment may appear in the software itself,
027: * if and wherever such third-party acknowledgments normally appear.
028: *
029: * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"
030: * must not be used to endorse or promote products derived from this
031: * software without prior written permission. For written
032: * permission, please contact apache@apache.org.
033: *
034: * 5. Products derived from this software may not be called "Apache"
035: * or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their
036: * name, without prior written permission of the Apache Software Foundation.
037: *
038: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
039: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
040: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
041: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
042: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
043: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
044: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
045: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
046: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
047: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
048: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: * ====================================================================
051: *
052: * This software consists of voluntary contributions made by many
053: * individuals on behalf of the Apache Software Foundation. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.oro.text;
059:
060: import java.util.*;
061:
062: import org.apache.oro.text.regex.*;
063: import org.apache.oro.util.*;
064:
065: /**
066: * This is the base class for all cache implementations provided in the
067: * org.apache.oro.text package.
068: * Although 4 subclasses of GenericPatternCache are provided with this
069: * package, users may not derive subclasses from this class.
070: * Rather, users should create their own implmentations of the
071: * {@link PatternCache} interface.
072: *
073: * @version @version@
074: * @since 1.0
075: * @see PatternCache
076: * @see PatternCacheLRU
077: * @see PatternCacheFIFO
078: * @see PatternCacheFIFO2
079: * @see PatternCacheRandom
080: */
081: public abstract class GenericPatternCache implements PatternCache {
082: PatternCompiler _compiler;
083: Cache _cache;
084:
085: /**
086: * The default capacity to be used by the GenericPatternCache subclasses
087: * provided with this package. Its value is 20.
088: */
089: public static final int DEFAULT_CAPACITY = 20;
090:
091: /**
092: * The primary constructor for GenericPatternCache. It has default
093: * access so it will only be used within the package. It initializes
094: * _cache and _compiler to the arguments provided.
095: * <p>
096: * @param cache The cache with which to store patterns.
097: * @param compiler The PatternCompiler that should be used to compile
098: * patterns.
099: */
100: GenericPatternCache(Cache cache, PatternCompiler compiler) {
101: _cache = cache;
102: _compiler = compiler;
103: }
104:
105: /**
106: * Adds a pattern to the cache and returns the compiled pattern. This
107: * method is in principle almost identical to
108: * {@link #getPattern getPattern()} except for the fact that
109: * it throws a MalformedPatternException if an expression cannot be
110: * compiled.
111: * <p>
112: * addPattern() is meant to be used when you expressly intend to add
113: * an expression to the cache and is useful for front-loading a cache
114: * with expressions before use. If the expression added does not
115: * already exist in the cache, it is compiled, added to the cache,
116: * and returned. If the compiled expression is already in the cache, it
117: * is simply returned.
118: * <p>
119: * The expected behavior of this method should be to start replacing
120: * patterns in the cache only after the cache has been filled to capacity.
121: * <p>
122: * @param expression The regular expression to add to the cache.
123: * @param options The compilation options to use when compiling the
124: * expression.
125: * @return The Pattern corresponding to the String representation of the
126: * regular expression.
127: * @exception MalformedPatternException If there is an error in compiling
128: * the regular expression.
129: */
130: public final synchronized Pattern addPattern(String expression,
131: int options) throws MalformedPatternException {
132: Object obj;
133: Pattern pattern;
134:
135: obj = _cache.getElement(expression);
136:
137: if (obj != null) {
138: pattern = (Pattern) obj;
139:
140: if (pattern.getOptions() == options)
141: return pattern;
142: }
143:
144: pattern = _compiler.compile(expression, options);
145: _cache.addElement(expression, pattern);
146:
147: return pattern;
148: }
149:
150: /**
151: * Same as calling
152: * <blockquote><pre>
153: * addPattern(expression, 0);
154: * </pre></blockquote>
155: * @exception MalformedPatternException If there is an error in compiling
156: * the regular expression.
157: */
158: public final synchronized Pattern addPattern(String expression)
159: throws MalformedPatternException {
160: return addPattern(expression, 0);
161: }
162:
163: /**
164: * This method fetches a pattern from the cache. It is nearly identical
165: * to {@link #addPattern addPattern()} except that it doesn't
166: * throw a MalformedPatternException. If the pattern is not in the
167: * cache, it is compiled, placed in the cache, and returned. If
168: * the pattern cannot be compiled successfully, it
169: * throws a MalformedCachePatternException.
170: * Note that this exception is derived from RuntimeException, which means
171: * you are NOT forced to catch it by the compiler. Please refer to
172: * {@link MalformedCachePatternException} for a discussion of
173: * when you should and shouldn't catch this exception.
174: * <p>
175: * @param expression The regular expression to fetch from the cache in
176: * compiled form.
177: * @param options The compilation options to use when compiling the
178: * expression.
179: * @return The Pattern corresponding to the String representation of the
180: * regular expression.
181: * @exception MalformedCachePatternException If there is an error in
182: * compiling the regular expression.
183: */
184: public final synchronized Pattern getPattern(String expression,
185: int options) throws MalformedCachePatternException {
186: Pattern result = null;
187:
188: try {
189: result = addPattern(expression, options);
190: } catch (MalformedPatternException e) {
191: throw new MalformedCachePatternException(
192: "Invalid expression: " + expression + "\n"
193: + e.getMessage());
194: }
195:
196: return result;
197: }
198:
199: /**
200: * Same as calling
201: * <blockquote><pre>
202: * getPattern(expression, 0)
203: * </pre></blockquote>
204: */
205: public final synchronized Pattern getPattern(String expression)
206: throws MalformedCachePatternException {
207: return getPattern(expression, 0);
208: }
209:
210: /**
211: * Returns the number of elements in the cache, not to be confused with
212: * the {@link #capacity()} which returns the number
213: * of elements that can be held in the cache at one time.
214: * <p>
215: * @return The current size of the cache (i.e., the number of elements
216: * currently cached).
217: */
218: public final int size() {
219: return _cache.size();
220: }
221:
222: /**
223: * Returns the maximum number of patterns that can be cached at one time.
224: * <p>
225: * @return The maximum number of patterns that can be cached at one time.
226: */
227: public final int capacity() {
228: return _cache.capacity();
229: }
230: }
|