org.pf.text
Class StringPattern

java.lang.Object
  extended by org.pf.text.StringPattern
All Implemented Interfaces:
Serializable

public class StringPattern
extends Object
implements Serializable

This class provides services for checking strings against string-patterns. Currently it supports the wildcards
'*' for any number of any character and
'?' for any one character. The API is very simple:

There are only the two class methods match() and matchIgnoreCase().
Example:
StringPattern.match( 'Hello World", "H* W*" ) ; --> evaluates to true
StringPattern.matchIgnoreCase( 'StringPattern", "str???pat*" ) ; --> evaluates to true

Version:
1.7
Author:
Manfred Duchrow
See Also:
Serialized Form

Field Summary
protected static String MULTI_WILDCARD
           
protected static char MULTICHAR_WILDCARD
           
protected static char SINGLECHAR_WILDCARD
           
 
Constructor Summary
StringPattern(String pattern)
          Initializes the new instance with the string pattern.
StringPattern(String pattern, boolean ignoreCase)
          Initializes the new instance with the string pattern and the selecteion, if case should be ignored when comparing characters.
StringPattern(String pattern, boolean ignoreCase, char digitWildcard)
          Initializes the new instance with the string pattern and the selecteion, if case should be ignored when comparing characters plus a wildcard character for digits.
StringPattern(String pattern, char digitWildcard)
          Initializes the new instance with the string pattern and a digit wildcard character.
 
Method Summary
protected  boolean charsAreEqual(char probeChar, char patternChar)
           
protected  Character digitWildcard()
           
protected  void digitWildcard(Character newValue)
           
protected  char digitWildcardChar()
           
protected  boolean endNotReached(char character)
           
protected  boolean endReached(char character)
           
 boolean getIgnoreCase()
          Returns whether or not the pattern matching ignores upper and lower case
 String getPattern()
          Returns the pattern as string.
protected  char getPatternChar(StringExaminer patternIterator, char probeCh)
           
protected  boolean hasDigitWildcard()
           
 boolean hasWildcard()
          Returns true if the pattern contains any '*' or '?' wildcard character.
static boolean match(String probe, String pattern)
          Returns true, if the given probe string matches the given pattern.
 boolean matches(String probe)
          Tests if a specified string matches the pattern.
static boolean matchIgnoreCase(String probe, String pattern)
          Returns true, if the given probe string matches the given pattern.
protected  boolean matchReverse(String pattern, StringExaminer probeIterator)
           
protected  StringExaminer newExaminer(String str)
           
 void setDigitWildcardChar(char digitWildcard)
          Sets the given character as a wildcard character in this pattern to match only digits ('0'-'9').
 void setIgnoreCase(boolean newValue)
          Sets whether the pattern matching should ignore case or not
 void setPattern(String newValue)
          Sets the pattern to a new value
protected  boolean skipAfter(StringExaminer examiner, String matchString)
          Increments the given iterator up to the last character that matched the character sequence in the given matchString.
protected  char skipWildcards(StringExaminer iterator)
          Moves the iterator position to the next character that is no wildcard.
protected  StringUtil strUtil()
           
 String toString()
          Returns the pattern string.
protected  String upToEnd(StringExaminer iterator)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MULTI_WILDCARD

protected static final String MULTI_WILDCARD
See Also:
Constant Field Values

MULTICHAR_WILDCARD

protected static final char MULTICHAR_WILDCARD
See Also:
Constant Field Values

SINGLECHAR_WILDCARD

protected static final char SINGLECHAR_WILDCARD
See Also:
Constant Field Values
Constructor Detail

StringPattern

public StringPattern(String pattern,
                     boolean ignoreCase)
Initializes the new instance with the string pattern and the selecteion, if case should be ignored when comparing characters.

Parameters:
pattern - The pattern to check against ( May contain '*' and '?' wildcards )
ignoreCase - Definition, if case sensitive character comparison or not.

StringPattern

public StringPattern(String pattern)
Initializes the new instance with the string pattern. The default is case sensitive checking.

Parameters:
pattern - The pattern to check against ( May contain '*' and '?' wildcards )

StringPattern

public StringPattern(String pattern,
                     char digitWildcard)
Initializes the new instance with the string pattern and a digit wildcard character. The default is case sensitive checking.

Parameters:
pattern - The pattern to check against ( May contain '*', '?' wildcards and the digit wildcard )
digitWildcard - A wildcard character that stands as placeholder for digits

StringPattern

public StringPattern(String pattern,
                     boolean ignoreCase,
                     char digitWildcard)
Initializes the new instance with the string pattern and the selecteion, if case should be ignored when comparing characters plus a wildcard character for digits.

Parameters:
pattern - The pattern to check against ( May contain '*' and '?' wildcards )
ignoreCase - Definition, if case sensitive character comparison or not.
digitWildcard - A wildcard character that stands as placeholder for digits
Method Detail

getIgnoreCase

public boolean getIgnoreCase()
Returns whether or not the pattern matching ignores upper and lower case


setIgnoreCase

public void setIgnoreCase(boolean newValue)
Sets whether the pattern matching should ignore case or not


getPattern

public String getPattern()
Returns the pattern as string.


setPattern

public void setPattern(String newValue)
Sets the pattern to a new value


digitWildcard

protected Character digitWildcard()

digitWildcard

protected void digitWildcard(Character newValue)

match

public static boolean match(String probe,
                            String pattern)
Returns true, if the given probe string matches the given pattern.
The character comparison is done case sensitive.

Parameters:
probe - The string to check against the pattern.
pattern - The patter, that probably contains wildcards ( '*' or '?' )

matchIgnoreCase

public static boolean matchIgnoreCase(String probe,
                                      String pattern)
Returns true, if the given probe string matches the given pattern.
The character comparison is done ignoring upper/lower-case.

Parameters:
probe - The string to check against the pattern.
pattern - The patter, that probably contains wildcards ( '*' or '?' )

matches

public boolean matches(String probe)
Tests if a specified string matches the pattern.

Parameters:
probe - The string to compare to the pattern
Returns:
true if and only if the probe matches the pattern, false otherwise.

toString

public String toString()
Returns the pattern string.

Overrides:
toString in class Object
See Also:
Object.toString()

hasWildcard

public boolean hasWildcard()
Returns true if the pattern contains any '*' or '?' wildcard character.


setDigitWildcardChar

public void setDigitWildcardChar(char digitWildcard)
Sets the given character as a wildcard character in this pattern to match only digits ('0'-'9').

Parameters:
digitWildcard - The placeholder character for digits

hasDigitWildcard

protected boolean hasDigitWildcard()

digitWildcardChar

protected char digitWildcardChar()

skipWildcards

protected char skipWildcards(StringExaminer iterator)
Moves the iterator position to the next character that is no wildcard. Doesn't skip digit wildcards !


skipAfter

protected boolean skipAfter(StringExaminer examiner,
                            String matchString)
Increments the given iterator up to the last character that matched the character sequence in the given matchString. Returns true, if the matchString was found, otherwise false.

Parameters:
matchString - The string to be found (must not contain *)

upToEnd

protected String upToEnd(StringExaminer iterator)

matchReverse

protected boolean matchReverse(String pattern,
                               StringExaminer probeIterator)

charsAreEqual

protected boolean charsAreEqual(char probeChar,
                                char patternChar)

endReached

protected boolean endReached(char character)

endNotReached

protected boolean endNotReached(char character)

getPatternChar

protected char getPatternChar(StringExaminer patternIterator,
                              char probeCh)

newExaminer

protected StringExaminer newExaminer(String str)

strUtil

protected StringUtil strUtil()