org.pfsw.text
Class StringPatternCollection

java.lang.Object
  extended by org.pfsw.text.AStringFilter
      extended by org.pfsw.text.StringPatternCollection
All Implemented Interfaces:
org.pfsw.bif.filter.IObjectFilter<java.lang.String>, org.pfsw.bif.text.IJSONConvertible, org.pfsw.bif.text.IStringFilter

public class StringPatternCollection
extends AStringFilter
implements org.pfsw.bif.text.IJSONConvertible

Holds a collection of StringPattern objects and allows to check if a given string matches any or none of the patterns.

Attention: Since version 1.1 of this class there is an incompatibility to its predecessors.
It now creates StringPattern objects that match '*' to empty string. That is, the pattern "Tom*" now matches string "Tom" which it doesn't before version 1.1.


Field Summary
protected static java.lang.String DEFAULT_PATTERN_SEPARATOR
           
 
Fields inherited from interface org.pfsw.bif.text.IJSONConvertible
JSON_ARRAY_END, JSON_ARRAY_START, JSON_ELEMENT_SEPARATOR, JSON_LITERAL_FALSE, JSON_LITERAL_NULL, JSON_LITERAL_TRUE, JSON_OBJECT_END, JSON_OBJECT_START, JSON_PAIR_SEPARATOR, JSON_STRING_DELIMITER, JSON_STRING_ESCAPE
 
Fields inherited from interface org.pfsw.bif.text.IStringFilter
ALL, NONE
 
Constructor Summary
StringPatternCollection()
          Initialize the new instance with default values.
StringPatternCollection(java.util.Collection<java.lang.String> strPatterns)
          Initialize the new instance with a collection of patterns.
StringPatternCollection(java.lang.String... strPatterns)
          Initialize the new instance with a pattern array.
StringPatternCollection(StringPattern pattern)
          Initialize the new instance with one pattern.
 
Method Summary
 void add(java.lang.String pattern)
          Adds the given pattern as pattern to the collection of patterns, if it is not yet there.
 void add(StringPattern aPattern)
          Adds the given pattern to the collection of patterns, if it is not yet there
 void addPatterns(java.util.Collection<java.lang.String> strPatterns)
          Adds all patterns strings in the given collection.
 void addPatterns(java.lang.String patternList)
          Adds all patterns in the given string which must be separated by ';'.
 void addPatterns(java.lang.String[] patternCollection)
          Adds all patterns in the given array.
 void addPatterns(StringPatternCollection patternCollection)
          Adds all patterns from the given pattern collection.
 void addPatterns(java.lang.String patternList, java.lang.String separators)
          Adds all patterns in the given string which must be separated by ';'.
 void appendAsJSONString(java.lang.Appendable output)
           
 StringPatternCollection copy()
          Returns a copy of this pattern collection.
static StringPatternCollection create(java.lang.String[] patterns, boolean ignoreCase)
          Returns a new instance with the given string patterns and the selection, if case should be ignored when comparing characters.
static StringPatternCollection create(java.lang.String patterns, boolean ignoreCase)
          Returns a new instance with the given string patterns and the selection, if case should be ignored when comparing characters.
protected  java.lang.String getDefaultPatternSeparator()
          Returns the default separators for patterns
protected  java.lang.Boolean getIgnoreCase()
          Returns whether or not the pattern matching ignores upper and lower case Might return null if not specified.
protected  java.util.Collection<StringPattern> getPatterns()
           
 java.lang.String[] getPatternStrings()
          Returns an array with all pattern strings contained in this object.
 boolean isEmpty()
          Returns true if no pattern definition is currently in this collection
 boolean matches(java.lang.String aString)
          Returns true if the given string matches the filter otherwise false.
 boolean matchesAll(java.lang.String probe)
          Returns true if the given probe matches all patterns in this collection.
 boolean matchesAny(java.lang.String probe)
          Returns true, if the given probe matches any of the patterns.
 boolean matchesNone(java.lang.String probe)
          Returns true, if the given probe matches none of the patterns.
protected  StringPattern newPattern(java.lang.String pattern)
          Creates and returns a new pattern object out of the given string.
 java.util.Collection<java.lang.String> reject(java.util.Collection<java.lang.String> strings)
          Returns a collection containing all of the given strings that do NOT match any pattern of this pattern collection.
 java.lang.String[] reject(java.lang.String[] strings)
          Returns an array containing all of the given strings that do NOT match any pattern of this pattern collection.
 boolean remove(StringPattern aPattern)
          Removes the given pattern from the collection if it exists.
 void reset()
          Removes all patterns and starts with a new empty collection.
 java.util.Collection<java.lang.String> select(java.util.Collection<java.lang.String> strings)
          Returns an array containing all of the given strings that matches any pattern of this pattern collection.
 java.lang.String[] select(java.lang.String[] strings)
          Returns an array containing all of the given strings that matches any pattern of this pattern collection.
protected  java.util.Collection<java.lang.String> selectOrReject(java.util.Collection<java.lang.String> strings, boolean select)
          If possible, the result collection is of the same type as the input collection.
protected  java.lang.String[] selectOrReject(java.lang.String[] strings, boolean select)
           
 void setIgnoreCase(boolean ignore)
          Sets whether all the patterns inside this collection should ignore case or not.
protected  void setIgnoreCase(java.lang.Boolean ignoreCase)
          Sets whether the pattern matching should ignore case or not
protected  void setPatterns(java.util.Collection<StringPattern> patterns)
           
 int size()
          returns the number of patterns in this collection.
protected  StringUtil str()
           
 java.lang.String toJSON()
          Returns a JSON string representation of this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PATTERN_SEPARATOR

protected static final java.lang.String DEFAULT_PATTERN_SEPARATOR
See Also:
Constant Field Values
Constructor Detail

StringPatternCollection

public StringPatternCollection()
Initialize the new instance with default values.


StringPatternCollection

public StringPatternCollection(StringPattern pattern)
Initialize the new instance with one pattern.


StringPatternCollection

public StringPatternCollection(java.lang.String... strPatterns)
Initialize the new instance with a pattern array.


StringPatternCollection

public StringPatternCollection(java.util.Collection<java.lang.String> strPatterns)
Initialize the new instance with a collection of patterns.

Parameters:
strPatterns - A collection of String.
Method Detail

create

public static StringPatternCollection create(java.lang.String patterns,
                                             boolean ignoreCase)
Returns a new instance with the given string patterns and the selection, if case should be ignored when comparing characters.
The patterns must be separated by semi-colon ';'.

Parameters:
patterns - The pattern definitions.
ignoreCase - Definition, if case sensitive character comparison or not.

create

public static StringPatternCollection create(java.lang.String[] patterns,
                                             boolean ignoreCase)
Returns a new instance with the given string patterns and the selection, if case should be ignored when comparing characters.

Parameters:
patterns - The pattern definitions.
ignoreCase - Definition, if case sensitive character comparison or not.

matches

public boolean matches(java.lang.String aString)
Returns true if the given string matches the filter otherwise false. If the given string is null false will be returned.

Specified by:
matches in interface org.pfsw.bif.filter.IObjectFilter<java.lang.String>
Specified by:
matches in interface org.pfsw.bif.text.IStringFilter
Parameters:
aString - Any string or even null

matchesAny

public boolean matchesAny(java.lang.String probe)
Returns true, if the given probe matches any of the patterns.

Parameters:
probe - The string to match against all patterns

matchesNone

public boolean matchesNone(java.lang.String probe)
Returns true, if the given probe matches none of the patterns.

Parameters:
probe - The string to check against all patterns

matchesAll

public boolean matchesAll(java.lang.String probe)
Returns true if the given probe matches all patterns in this collection.

Parameters:
probe - The string to check against all patterns

select

public java.lang.String[] select(java.lang.String[] strings)
Returns an array containing all of the given strings that matches any pattern of this pattern collection. If the input array is null then the result of this method is also null.

Parameters:
strings - The strings to be matched against this pattern

reject

public java.lang.String[] reject(java.lang.String[] strings)
Returns an array containing all of the given strings that do NOT match any pattern of this pattern collection. If the input array is null then the result of this method is also null.

Parameters:
strings - The strings to be matched against this pattern

select

public java.util.Collection<java.lang.String> select(java.util.Collection<java.lang.String> strings)
Returns an array containing all of the given strings that matches any pattern of this pattern collection. If possible, the result collection is of the same type as the input collection. Otherwise it is an ArrayList. If the input collection is null then the result of this method is also null.

Parameters:
strings - The strings to be matched against this pattern

reject

public java.util.Collection<java.lang.String> reject(java.util.Collection<java.lang.String> strings)
Returns a collection containing all of the given strings that do NOT match any pattern of this pattern collection. If possible, the result collection is of the same type as the input collection. Otherwise it is an ArrayList. If the input collection is null then the result of this method is also null.

Parameters:
strings - The strings to be matched against this pattern

reset

public void reset()
Removes all patterns and starts with a new empty collection.


add

public void add(StringPattern aPattern)
Adds the given pattern to the collection of patterns, if it is not yet there

Parameters:
aPattern - The pattern to add

addPatterns

public void addPatterns(java.lang.String patternList)
Adds all patterns in the given string which must be separated by ';'.

Parameters:
patternList - A list of patterns separated by ';'

addPatterns

public void addPatterns(java.lang.String patternList,
                        java.lang.String separators)
Adds all patterns in the given string which must be separated by ';'.


addPatterns

public void addPatterns(java.util.Collection<java.lang.String> strPatterns)
Adds all patterns strings in the given collection.

Parameters:
strPatterns - A collection of String

addPatterns

public void addPatterns(StringPatternCollection patternCollection)
Adds all patterns from the given pattern collection.

Parameters:
patternCollection - A pattern collection like this one

addPatterns

public void addPatterns(java.lang.String[] patternCollection)
Adds all patterns in the given array.


add

public void add(java.lang.String pattern)
Adds the given pattern as pattern to the collection of patterns, if it is not yet there.

Since V1.1 the pattern internally created supports '*' matching empty strings (e.g. "AC" matches "A*C")!

Parameters:
pattern - The pattern to add

remove

public boolean remove(StringPattern aPattern)
Removes the given pattern from the collection if it exists. The lookup is done by using the equals() method.

Parameters:
aPattern - The pattern to remove
Returns:
true, if the pattern was found and removed, otherwise false

size

public int size()
returns the number of patterns in this collection.


isEmpty

public boolean isEmpty()
Returns true if no pattern definition is currently in this collection


getPatternStrings

public java.lang.String[] getPatternStrings()
Returns an array with all pattern strings contained in this object. If no pattern is contained an empty array will be returned.


copy

public StringPatternCollection copy()
Returns a copy of this pattern collection. The copy is indipendent of this object. That means, it can be modified without impact on this object.


setIgnoreCase

public void setIgnoreCase(boolean ignore)
Sets whether all the patterns inside this collection should ignore case or not. Be aware that this methods modifies all contained patterns according to the given parameter. All patterns that are added to this collection afterwards will also be set to the specified ignore case setting. This behavior can only be switched off with calling reset().

Parameters:
ignore - true if pattern matching should be case insensitive from now on

appendAsJSONString

public void appendAsJSONString(java.lang.Appendable output)
Specified by:
appendAsJSONString in interface org.pfsw.bif.text.IJSONConvertible

toJSON

public java.lang.String toJSON()
Returns a JSON string representation of this object.

Specified by:
toJSON in interface org.pfsw.bif.text.IJSONConvertible
Returns:
JSON object: {"string1":"string2"}

newPattern

protected StringPattern newPattern(java.lang.String pattern)
Creates and returns a new pattern object out of the given string.
Subclasses may override this method to instantiate their own subclass of StringPattern.


selectOrReject

protected java.lang.String[] selectOrReject(java.lang.String[] strings,
                                            boolean select)

selectOrReject

protected java.util.Collection<java.lang.String> selectOrReject(java.util.Collection<java.lang.String> strings,
                                                                boolean select)
If possible, the result collection is of the same type as the input collection. Otherwise its an ArrayList.


getDefaultPatternSeparator

protected java.lang.String getDefaultPatternSeparator()
Returns the default separators for patterns

See Also:
addPatterns(String)

getPatterns

protected java.util.Collection<StringPattern> getPatterns()

setPatterns

protected void setPatterns(java.util.Collection<StringPattern> patterns)

getIgnoreCase

protected java.lang.Boolean getIgnoreCase()
Returns whether or not the pattern matching ignores upper and lower case Might return null if not specified.


setIgnoreCase

protected void setIgnoreCase(java.lang.Boolean ignoreCase)
Sets whether the pattern matching should ignore case or not


str

protected StringUtil str()