tribble.util
Class SQLPattern

java.lang.Object
  extended by tribble.util.SQLPattern

public class SQLPattern
extends java.lang.Object

SQL 'LIKE' pattern matching operator.

This class provides methods that compare patterns to strings in the manner of the SQL 'LIKE' operator. Specifically, the SQL LIKE operator takes two operands, a pattern string and a value string, and determines if the value string matches the pattern.

The LIKE pattern string is composed of regular characters and wildcard characters. Regular characters must match exactly. The wildcard character '_' matches any single character, and the wildcard character '%' matches zero or more characters.

The matches(String) method is used to match an SQL pattern against a string. A static matches(String, String) method is also provided for convenience.

Since:
2003-08-01
Version:
$Revision: 1.3 $ $Date: 2005/04/23 18:13:50 $
Author:
David R. Tribble (david@tribble.com).
Copyright ©2003-2005 by David R. Tribble, all rights reserved.
Permission is granted to freely use and distribute this source code provided that the original copyright and authorship notices remain intact.
See Also:
FilenamePattern

Field Summary
protected  FilenamePattern m_pat
          SQL 'LIKE' pattern matcher.
static char PAT_ANY
          Default pattern character: Any single character.
static char PAT_CLOSURE
          Default pattern character: Zero or more characters.
static char PAT_ESCAPE
          Default pattern character: Escape any special meaning.
 
Constructor Summary
SQLPattern(java.lang.String pat)
          Constructor.
 
Method Summary
 boolean matches(java.lang.String s)
          Determine if a string matches the SQL pattern.
static boolean matches(java.lang.String pat, java.lang.String s)
          Determine if a string matches an SQL pattern.
static boolean matchSQLPattern(java.lang.String pat, java.lang.String s)
          Determine if a string matches a pattern string.
 boolean setIgnoreCase(boolean flag)
          Establish whether or not the SQL pattern matcher ignores case.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PAT_ESCAPE

public static final char PAT_ESCAPE
Default pattern character: Escape any special meaning.

See Also:
Constant Field Values

PAT_ANY

public static final char PAT_ANY
Default pattern character: Any single character.

See Also:
Constant Field Values

PAT_CLOSURE

public static final char PAT_CLOSURE
Default pattern character: Zero or more characters.

See Also:
Constant Field Values

m_pat

protected FilenamePattern m_pat
SQL 'LIKE' pattern matcher.

Constructor Detail

SQLPattern

public SQLPattern(java.lang.String pat)
Constructor.

Parameters:
pat - An SQL 'LIKE' pattern.
Throws:
java.lang.IllegalArgumentException - (unchecked) Thrown if pat is malformed.
java.lang.NullPointerException - (unchecked) Thrown if pat is null.
Since:
1.1, 2003-08-01
Method Detail

matches

public static boolean matches(java.lang.String pat,
                              java.lang.String s)
Determine if a string matches an SQL pattern.

Parameters:
pat - An SQL pattern string.
s - A string to compare against pat.
Returns:
True if s matches pat, otherwise false.
Throws:
java.lang.IllegalArgumentException - (unchecked) Thrown if pat is malformed.
java.lang.NullPointerException - (unchecked) Thrown if pat or s is null.
Since:
1.2, 2003-08-02

matchSQLPattern

public static boolean matchSQLPattern(java.lang.String pat,
                                      java.lang.String s)
Determine if a string matches a pattern string.

Parameters:
pat - A pattern string. A pattern is composed of regular characters and special pattern matching characters, which are:
_ (underscore) - Matches any single character.
% - Matches zero or more characters.
\ - Removes (escapes) the special meaning of the next character.
s - A string to compare against pattern pat.
Returns:
True if string s matches pattern pat, otherwise false.
Since:
1.3, 2005-04-23

setIgnoreCase

public boolean setIgnoreCase(boolean flag)
Establish whether or not the SQL pattern matcher ignores case. (By default, the pattern matcher ignores case when matching alphabetic characters.)

Parameters:
flag - If true, upper and lower case letters are considered to be identical when matching strings (e.g., strings "abc", "Abc", and "ABC" will all be considered to be the same). If false, the pattern matcher is case-sensitive, and equivalent upper and lower case letters are considered to be different.
Returns:
The previous setting for this pattern matcher.
Since:
1.1, 2003-08-01

matches

public boolean matches(java.lang.String s)
Determine if a string matches the SQL pattern.

Parameters:
s - A string to match against the SQL pattern.
Returns:
True if the pattern matches s, otherwise false.
Throws:
java.lang.NullPointerException - (unchecked) Thrown if s is null.
Since:
1.1, 2003-08-01