001: // ============================================================================
002: // $Id: Match.java,v 1.8 2006/01/08 00:52:25 davidahall Exp $
003: // Copyright (c) 2003-2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032: package net.sf.jga.fn.string;
033:
034: import java.util.regex.Pattern;
035: import net.sf.jga.fn.UnaryPredicate;
036:
037: /**
038: * Unary Functor that tests a string argument against a given regular
039: * expression. The expression is not presumed to be anchored: if any part of
040: * the input string matches the regular expression, then the result will be
041: * true.
042: * <p>
043: * Copyright © 2003-2005 David A. Hall
044: *
045: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
046: */
047:
048: public class Match extends UnaryPredicate<String> {
049:
050: static final long serialVersionUID = 4842042451096017684L;
051:
052: // The regular expression to be matched (in String form for convenience
053: // at construction and for reporting)
054: private String _regex;
055:
056: // The regular expression to be matched (in Pattern form for execution)
057: private Pattern _pattern;
058:
059: /**
060: * Builds a Match with an empty string pattern
061: */
062: public Match() {
063: this ("");
064: }
065:
066: /**
067: * Builds a Match with a given regular expression
068: */
069: public Match(String regex) {
070: _regex = (regex != null) ? regex : "";
071: _pattern = Pattern.compile(_regex);
072: }
073:
074: /**
075: * Builds a Match with a given Pattern
076: */
077: public Match(Pattern pattern) {
078: _pattern = pattern;
079: _regex = pattern.pattern();
080: }
081:
082: /**
083: * Returns the format object used to present values in formatted form.
084: * @return the format used to present values
085: */
086:
087: public String getRegex() {
088: return _regex;
089: }
090:
091: // UnaryPredicate interface
092:
093: /**
094: * Tests a string against the regular expression given at construction
095: * <p>
096: * @param arg the value to tested
097: * @return true if the string matches the regular expression given at
098: * construction
099: */
100:
101: public Boolean fn(String arg) {
102: if (arg == null)
103: throw new NullPointerException();
104:
105: return _pattern.matcher(arg).find();
106: }
107:
108: /**
109: * Calls the Visitor's <code>visit(Match)</code> method, if it
110: * implements the nested Visitor interface.
111: */
112: public void accept(net.sf.jga.fn.Visitor v) {
113: if (v instanceof Match.Visitor)
114: ((Match.Visitor) v).visit(this );
115: else
116: v.visit(this );
117: }
118:
119: // Object overrides
120:
121: public String toString() {
122: return "Match(" + _regex + ")";
123: }
124:
125: // Acyclic Visitor
126:
127: /**
128: * Interface for classes that may interpret a <b>Match</b>
129: * predicate.
130: */
131: public interface Visitor extends net.sf.jga.fn.Visitor {
132: public void visit(Match host);
133: }
134: }
|