com.cafesoft.core.field
Class StringFieldValuePattern

java.lang.Object
  |
  +--com.cafesoft.core.field.StringFieldValuePattern
All Implemented Interfaces:
FieldValuePattern

public class StringFieldValuePattern
extends Object
implements FieldValuePattern

StringFieldValuePattern implements a pattern for matching String values. This class supports String patterns of the following form:

NOTE:A pattern that contains multiple wildcard characters such as *abc*def* are illegal. If a pattern contains two wildcard characters those wildcard characters must be at the start and at the end of the pattern.

Since:
7/30/03

Constructor Summary
StringFieldValuePattern(String pattern, boolean matchCase)
          Create a new StringFieldValuePattern.
 
Method Summary
 boolean equals(Object obj)
          Indicates whether some other object is "equal to" this one.
 String getPattern()
          Get a String representation of the pattern.
 int hashCode()
          Get the hashCode of the StringFieldValuePattern object.
 boolean matchCase()
          Determine if this pattern matches case.
 boolean matches(Object fieldValue)
          Check for a field value match against this FieldValuePattern.
 BigDecimal score()
          Get the score of the FieldValuePattern.
 String toString()
          Returns a string representation of the object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StringFieldValuePattern

public StringFieldValuePattern(String pattern,
                               boolean matchCase)
                        throws InvalidPatternException
Create a new StringFieldValuePattern.

Parameters:
pattern - the String representation of the pattern, which may have any of the following forms:
matchCase - if true, then pattern matching is case sensitive, else the match is done case insensitively.
Throws:
InvalidPatternException - if pattern contains multiple wildcards that do not bookend the expected pattern. Therefore, the only valid pattern that contains multiple wildcards is **.
Method Detail

matches

public boolean matches(Object fieldValue)
Check for a field value match against this FieldValuePattern.

Algorithm for matching strings:

  1. If the pattern is the wildcard, then every field pattern matches.
  2. If the fieldValue is null and the pattern is null then the pattern matches.
  3. If the fieldValue is null and the pattern is not null then the pattern does not match.
  4. If the fieldValue object is not an instance of a String object then the pattern does not match.
  5. If the pattern is a String literal, and the fieldValue does not equal the pattern String then the pattern does not match.
  6. If the pattern is an endsWith pattern, and the fieldValue does not endWith the specified pattern then the pattern does not match.
  7. If the pattern is a startsWith pattern, and the fieldValue does not startsWith the specified pattern then the pattern does not match.
  8. If the pattern is a contains pattern, and the fieldValue does not contain the specified pattern then the pattern does not match.
  9. If the pattern is a startsAndEndsWith pattern, and the fieldValue does not startWith the specified prefix or does not endWith the specified prefix then the pattern does not match.
  10. If the case is important in any of the cases described above and the fieldValue does not adhere to the case constraint then the pattern does not match.

Specified by:
matches in interface FieldValuePattern
Parameters:
fieldValue - the field value.
Returns:
true if the fieldValue matches this FieldValuePattern, else return false.

getPattern

public String getPattern()
Get a String representation of the pattern.

Specified by:
getPattern in interface FieldValuePattern
Returns:
a String representation of the pattern.

matchCase

public boolean matchCase()
Determine if this pattern matches case.

Returns:
true if pattern is case-sensitive, false otherwise.

score

public BigDecimal score()
Get the score of the FieldValuePattern.

The FieldValuePattern's score is a value that determines how specific a particular a pattern is. For instance, a StringFieldValuePattern needs to know which pattern "*" or "abc" is more specific. This method allows scores a pattern. The absolute implementation of the score method is implementation specific.

The score for a StringFieldValuePattern is calculated using the following algorithm.

  1. If the string pattern itself is null the score is 0.
  2. The length of the pattern itself is calculated. For example, if the String representation of the pattern was "abc*" the length would be 4.
  3. Next a value of 1.25 is subtracted from the score for each wildcard. Therefore, a pattern of *abc* would have a intermediate score of 2.5. Another example would be the pattern of abc*, it's intermediate score would be 2.75.
  4. Next if the pattern itself is a String value and not a wildcard or a null value a bonus of 0.01 is added to the score.
  5. Finally, if a pattern is case sensitive a value of 0.01 is added to the score. This is done because two patterns may be equivalent from a String.equals() perspective but the pattern that is matching case is much more specific and thus would score more.

Therefore, applying these rules to the following list of String yields the following results:
Pattern Match Case Score How did we get that score.

*

false -0.25 1.00 - 1.25 = -0.25
* true -0.25 1.00 - 1.25 = -0.25
null false 0.00 null scores 0.00
null true 0.00 null scores 0.00
empty string false 0.01 0.00 + 0.01 = 0.01
empty string true 0.02 0.00 + 0.01 + 0.01 = 0.02
*b* false 0.51 3.00 - 1.25 - 1.25 + 0.01 = 0.51
*b* true 0.52 3.00 - 1.25 - 1.25 + 0.01 + 0.01= 0.52
b false 1.01 1.00 + 0.01 = 1.01
b true 1.02 1.00 + 0.01 + 0.01 = 1.02
*bc false 1.76 3.00 - 1.25 + 0.01 = 1.76
*bc true 1.77 3.00 - 1.25 + 0.01 + 0.01 = 1.77
a*c false 1.76 3.00 - 1.25 + 0.01 = 1.76
a*c true 1.77 3.00 - 1.25 + 0.01 + 0.01 = 1.77
ab* false 1.76 3.00 - 1.25 + 0.01 = 1.76
ab* true 1.77 3.00 - 1.25 + 0.01 + 0.01 = 1.77
ab false 2.01 2.00 + 0.01 = 2.01
ab true 2.02 2.00 + 0.01 + 0.01 = 2.02
abc false 3.01 3.00 + 0.01 = 3.01
abc true 3.02 3.00 + 0.01 + 0.01 = 3.02

Specified by:
score in interface FieldValuePattern
Returns:
a BigDecimal object that contains this FieldValuePattern objects score.

hashCode

public int hashCode()
Get the hashCode of the StringFieldValuePattern object.

Overrides:
hashCode in class Object
Returns:
the hashCode of the StringFieldValuePattern object.

equals

public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.

Overrides:
equals in class Object
Parameters:
obj - the reference object with which to compare.
Returns:
true if this object is the same as the obj argument; false otherwise.

toString

public String toString()
Returns a string representation of the object.

Overrides:
toString in class Object
Returns:
a string representation of the object.


Generated on 8:41:50 AM June 06, 2005, © 1996-2005 Cafésoft LLC. All rights reserved.