Package Torello.Java
Interface StrFilter
-
- All Superinterfaces:
java.util.function.Predicate<java.lang.Object>
,java.io.Serializable
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface StrFilter extends java.io.Serializable, java.util.function.Predicate<java.lang.Object>
A simple functional-interface that provides many 'builder' methods for creating standard JavaPredicate's
that operate on an Object's'toString'
method.
When Java was written, the concept of a "Memory Address" was eliminated, in its entirety, from the scope and range of the code that is written in Java. Java has many similarities to C and C++, but when it comes to memory-addresses, the diverge greatly. Java is a 'garbage collected language' and that means the internal JRE worries about deallocating objects that are no longer in use. In C, this was half of the work a programmer had to do. The biggest benefit of the garbage-collected aspect is the ability to work with 'immutable strings' in a way that the programmer has to think very little about irrelevant concepts like memory-leaks and invalid pointer references. In fact, the entire HTML-Search Package in this jar file would have been 100 times more difficult with the 'immutable' aspect of the java-string. All of the HTML node classes in this package are mostly just wrappers around java-strings.
This class is designed to make it easier to perform 'filter-operations' on a collections of java-strings. This class is an extremely 'general-purpose' class, and it can be used with any object whosetoString()
method is fully functioning, and returns something significant about the object. There are two primary classes in this library that utilize thisclass 'StrFilter'
interface FileNodeFilter
facilitates using instances of'StrFilter'
with a static-factory method provided in its interfaceinterface URLFilter
also provides a static-factory wrapper/builder in its interface. Theclass URLFilter
treats URL's as String's for the purposes of filtering URL's, and makes thisclass StrFilter
an invaluable too.
REDUNDANT-CODE NOTE: The features and functions provided byclass StrFilter
might appear to be 'eerily' similar to the functions inclass TextComparitor
(to the astute programmer). The primary impetus for having both is that theTextComparitor
does not wrap it's (input) comparison-strings inside the predicate test. The ability to separate the "Comparison Type" from the "Comparing String Data" is extremely important for syntactical reasons involving where and how theclass TextComparitor
is used.
This class,class StrFilter
, is often passed as a constructor to more complicated predicates, likeclass URLFilter
. On the other hand, the NodeSearch package usesTextComparitor's
in just about every one of it's search routines. And great care is taken to ensure that both the compare-operation, and the compare-data are kept together through-out the package. In the case ofTextComparitor
, running the constructor as often as would be required to use this library has forced this situation.
In any case, it is OK thatTextComparitor
does not "wrap" the strings against which the values are being compared, while the intended uses ofStrFilter
require that the compare-strings are included in the predicate body. Of course this means that theStrFilter
constructor (static factory methods) are used much more often.TextComparitor
doesn't have any factory build methods at all. Note, even that aStrFilter
can be constructed / built using aTextComparitor
as input. However, the opposite scenario - building aTextComparitor
using anStrFilter
- would not be a useful feature. The purpose in explaining this is to hopefully show whatStrFilter
does, and its similarities toTextComparitor
(which is a class that has copious amounts of documentation already).
Hi-Lited Source-Code:- View Here: Torello/Java/StrFilter.java
- Open New Browser-Tab: Torello/Java/StrFilter.java
-
-
Field Summary
Fields Modifier and Type Field static long
serialVersionUID
-
Method Summary
@FunctionalInterface (Lambda) Method Modifier and Type Method boolean
test(Object o)
Methods: Static Factory-Builder using TextComparitor Modifier and Type Method static StrFilter
comparitor(TextComparitor tc, String... compareStrs)
Methods: Static Factory-Builder using Regular-Expressions Modifier and Type Method static StrFilter
regExKEEP(Pattern regEx, boolean regExMustMatchEntireString)
static StrFilter
regExREJECT(Pattern regEx, boolean regExMustMatchEntireString)
static StrFilter
regExsAND(Pattern... regExs)
static StrFilter
regExsNAND(Pattern... regExs)
static StrFilter
regExsOR(Pattern... regExs)
static StrFilter
regExsXOR(Pattern... regExs)
Methods: Static Factory-Builder, White-List Strings Modifier and Type Method static StrFilter
strListKEEP(boolean ignoreCase, String... strList)
static StrFilter
strListKEEP(Iterable<String> strList, boolean ignoreCase)
static StrFilter
strListKEEP(String strListFileName, boolean ignoreCase)
static StrFilter
strListKEEP(Iterator<String> strList, boolean ignoreCase)
static StrFilter
strListKEEP_NOIOE(String strListFileName, boolean ignoreCase)
Methods: Static Factory-Builder, Black-List Strings Modifier and Type Method static StrFilter
strListREJECT(boolean ignoreCase, String... strList)
static StrFilter
strListREJECT(Iterable<String> strList, boolean ignoreCase)
static StrFilter
strListREJECT(String strListFileName, boolean ignoreCase)
static StrFilter
strListREJECT(Iterator<String> strList, boolean ignoreCase)
static StrFilter
strListREJECT_NOIOE(String strListFileName, boolean ignoreCase)
Methods: Static Factory-Builder, String-Equality Modifier and Type Method static StrFilter
isEqualKEEP(String s, boolean ignoreCase)
static StrFilter
isEqualREJECT(String s, boolean ignoreCase)
Generic Erasure Hack Modifier and Type Method default Predicate<String>
castToStrPred()
-
-
-
Field Detail
-
serialVersionUID
static final long serialVersionUID
This fulfils the SerialVersion UID requirement for all classes that implement Java'sinterface java.io.Serializable
. Using theSerializable
Implementation offered by java is very easy, and can make saving program state when debugging a lot easier. It can also be used in place of more complicated systems like "hibernate" to store data as well.
Functional Interfaces are usually not thought of as Data Objects that need to be saved, stored and retrieved; however, having the ability to store intermediate results along with the lambda-functions that helped get those results can make debugging easier.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final long serialVersionUID = 1;
-
-
Method Detail
-
test
boolean test(java.lang.Object o)
FUNCTIONAL-INTERFACE BOOLEAN METHOD: This is the method that fulfils thisfunctional-interface 'test'
method.
This method will receive ajava.lang.Object
. ThisObject
must have implemented itstoString()
method The purpose of this method is to provide an easy means to filter certainString's
(automatically).
PRECISE NOTE: This method should return FALSE if the passedString
should be skipped. A return value of TRUE implies that theString
is not to be ignored or passed over, but rather 'kept.'
NOTE: This behavior is compatible with the Java Stream's method"filter(Predicate<...>)".
- Specified by:
test
in interfacejava.util.function.Predicate<java.lang.Object>
- Parameters:
o
- This is theObject
that will be tested. If it passes the test, then this method must return TRUE which would imply that the object shall not be filtered. If the object fails the test, this method should return FALSE, and the object shall not be included in the result set as it has failed the test.- Returns:
- When implementing this method, returning TRUE must mean that the
Object
has passed the filter's test-requirements (and will subsequently be retained by whatever function is carrying out the filter operation).
-
castToStrPred
default java.util.function.Predicate<java.lang.String> castToStrPred()
Java is not perfect. This method is fine: sinceString
inheritsObject
, and theinterface Predicate
does not have any write-dependencies.
Erasure is like this withinterface Predicate
- because that's how it works with all of the generics. AVector<Object>
cannot be passed to aVector<String>
parameter (because a person might need to use theString's
). But aPredicate<Object>
can not have problems being passed to aPredicate<String>
...- Returns:
'this' StrFilter
cast into aPredicate<String>
- Code:
- Exact Method Body:
Predicate p = this; return (Predicate<String>) p;
-
strListKEEP
static StrFilter strListKEEP(java.lang.Iterable<java.lang.String> strList, boolean ignoreCase)
- Code:
- Exact Method Body:
return strListKEEP(strList.iterator(), ignoreCase);
-
strListKEEP
static StrFilter strListKEEP(boolean ignoreCase, java.lang.String... strList)
Convenience Method
Invokes:strListKEEP(Iterator, boolean)
Converts:String[]
VarArgs toIterator<String>
- Code:
- Exact Method Body:
return strListKEEP(Arrays.asList(strList).iterator(), ignoreCase);
-
strListKEEP
static StrFilter strListKEEP(java.lang.String strListFileName, boolean ignoreCase) throws java.io.IOException
Convenience Method
Invokes:strListKEEP(Iterator, boolean)
Loads:'strListFileName'
from disk to anIterator<String>
- Code:
- Exact Method Body:
return strListKEEP(FileRW.loadFileToVector(strListFileName, false).iterator(), ignoreCase);
-
strListKEEP_NOIOE
static StrFilter strListKEEP_NOIOE(java.lang.String strListFileName, boolean ignoreCase)
Convenience Method
Invokes:strListKEEP(Iterator, boolean)
Loads:'strListFileName'
from disk to anIterator<String>
Catches Exception, and Fails Fast - (SeeLFEC
)- Code:
- Exact Method Body:
return strListKEEP(LFEC.loadFileToVector(strListFileName, false).iterator(), ignoreCase);
-
strListKEEP
static StrFilter strListKEEP(java.util.Iterator<java.lang.String> strList, boolean ignoreCase)
This is astatic
-factory generator method. It helps the programmer automatically build an instance of thisFunctionalInterface 'StrFilter'
for one of the most common uses/tasks where aString
-FilterPredicate
would be needed.
Here, an input-object'stoString()
result is compared against theString's
contained in'strList'
- which would also be called an input-String
'white-list.'- Parameters:
strList
- A list ofString's
to be used as a 'comparison set' against an input-String
in aPredicate
-test.ignoreCase
- When this is TRUE all equality-comparisons performed in theString's
tested will ignore case-sensitivity.- Returns:
- A new
StrFilter
that can be used to test and filterString's
, according to each of theString's
in the input-parameter'strList'
- Code:
- Exact Method Body:
// Build a TreeSet<String>, this will be used, internally, in the instance of 'StrFilter' // (a Predicate<Object>) that is returned by this factory-builder method. // // Experienced Java users: TreeSet is a very convenient and easy-to-use "sorted list" // implementation that stores strings, in sorted order, in a binary-tree. This makes // looking them up as efficient as possible. final TreeSet<String> ts = new TreeSet<>(); // When building this sorted-list (TreeSet), if we are ignoring case (and-only-if), then // we must convert the strings contained by the input parameter string-list 'strList' to // lower-case first. if (ignoreCase) while (strList.hasNext()) ts.add(strList.next().toLowerCase()); else while (strList.hasNext()) ts.add(strList.next()); // The returned Predicate<Object> must call '.toString()' on it's object-input before // checking the "Sorted-List" (TreeSet<String>) to see if it contains the string. If a // case-insensitive predicate was requested, then we must build a Predicate that invokes // BOTH 'toString()' AND THEN invokes 'toLowerCase()' on the input Object. if (ignoreCase) return (Object o) -> ts.contains(o.toString().toLowerCase()); else return (Object o) -> ts.contains(o.toString());
-
strListREJECT
static StrFilter strListREJECT (java.lang.Iterable<java.lang.String> strList, boolean ignoreCase)
- Code:
- Exact Method Body:
return strListREJECT(strList.iterator(), ignoreCase);
-
strListREJECT
static StrFilter strListREJECT(boolean ignoreCase, java.lang.String... strList)
Convenience Method
Invokes:strListREJECT(Iterator, boolean)
Converts:String[]
VarArgs toIterator<String>
- Code:
- Exact Method Body:
return strListREJECT(Arrays.asList(strList).iterator(), ignoreCase);
-
strListREJECT
static StrFilter strListREJECT(java.lang.String strListFileName, boolean ignoreCase) throws java.io.IOException
Convenience Method
Invokes:strListREJECT(Iterator, boolean)
Loads:'strListFileName'
from disk to anIterator<String>
- Code:
- Exact Method Body:
return strListREJECT(FileRW.loadFileToVector(strListFileName, false).iterator(), ignoreCase);
-
strListREJECT_NOIOE
static StrFilter strListREJECT_NOIOE(java.lang.String strListFileName, boolean ignoreCase)
Convenience Method
Invokes:strListREJECT(Iterator, boolean)
Loads:'strListFileName'
from disk to anIterator<String>
Catches Exception, and Fails Fast - (SeeLFEC
)- Code:
- Exact Method Body:
return strListREJECT(LFEC.loadFileToVector(strListFileName, false).iterator(), ignoreCase);
-
strListREJECT
static StrFilter strListREJECT (java.util.Iterator<java.lang.String> strList, boolean ignoreCase)
This is astatic
-factory generator method. It helps the programmer automatically build an instance of thisFunctionalInterface 'StrFilter'
for one of the most common uses/tasks where aString
-FilterPredicate
would be needed.
Here, an input-object'stoString()
result is compared against theString's
contained in'strList'
- which would also be called an input-String
'black-list.'- Parameters:
strList
- A list ofString's
to be used as a 'comparison set' against an input-String
in aPredicate
-test.ignoreCase
- When this is TRUE all equality-comparisons performed in theString's
tested will ignore case-sensitivity.- Returns:
- A new
StrFilter
that can be used to test and filterString's
, according to each of theString's
in the input-parameter'strList'
. - Code:
- Exact Method Body:
// Build a TreeSet<String>, this will be used, internally, in the instance of 'StrFilter' // (a Predicate<Object>) that is returned by this factory-builder method. // // Experienced Java users: TreeSet is a very convenient and easy-to-use "sorted list" // implementation that stores strings, in sorted order, in a binary-tree. This makes // looking them up as efficient as possible. final TreeSet<String> ts = new TreeSet<>(); // When building this sorted-list (TreeSet), if we are ignoring case (and-only-if), then // we must convert the strings contained by the input parameter string-list 'strList' to // lower-case first. if (ignoreCase) while (strList.hasNext()) ts.add(strList.next().toLowerCase()); else while (strList.hasNext()) ts.add(strList.next()); // The returned Predicate<Object> must call '.toString()' on it's object-input before // checking the "Sorted-List" (TreeSet<String>) to see if it contains the string. If a // case-insensitive predicate was requested, then we must build a Predicate that invokes // BOTH 'toString()' AND THEN invokes 'toLowerCase()' on the input Object. if (ignoreCase) return (Object o) -> ! ts.contains(o.toString().toLowerCase()); else return (Object o) -> ! ts.contains(o.toString());
-
regExKEEP
static StrFilter regExKEEP(java.util.regex.Pattern regEx, boolean regExMustMatchEntireString)
This is astatic
-factory generator method. It helps the programmer automatically build an instance of thisFunctionalInterface 'StrFilter'
for one of the most common uses/tasks where aString
-FilterPredicate
would be needed.
Here, an input-object'stoString()
result is checked against Java's Regular-Expression matcher. ThePredicate
generated will choose to KEEP input-Object's
by returning TRUE when the Regular-Expression matches theObject.toString()
results.- Parameters:
regEx
- A regular-expression used to compare against an input-Object
(usingObject.toString()
in aPredicate
-test.regExMustMatchEntireString
- When this parameter is passed TRUE, then the regular-expressionPattern
must match the entire inputObject.toString()
. When this parameter is passed FALSE, it only need match some portion or part of the input-parameterString
in thePredicate-test
that is being generated.- Returns:
- A new
StrFilter
that can be used to test and filterString's
, according to the input-parameter'regEx'
- Code:
- Exact Method Body:
// FAIL-FAST: Perform this test BEFORE building the regex, to save the programmer // from later on (when using this factory generated predicate) having a // NullPointerException that is more difficult to isolate. if (regEx == null) throw new NullPointerException( "The Regular-Expression provided to the 'regEx' parameter of this static-factory-builder" + "method, 'regExKEEP,' was null." ); if (regExMustMatchEntireString) { // the java.util.regex.Pattern.asPredicate() method produces a predicate that must // match an entire input string. This would be identical (if it were possible) to insert // a '^' at the beginning of the regex, and an '$' at the end. final Predicate<String> p = regEx.asPredicate(); return (Object o) -> p.test(o.toString()); } else return (Object o) -> regEx.matcher(o.toString()).find();
-
regExREJECT
static StrFilter regExREJECT(java.util.regex.Pattern regEx, boolean regExMustMatchEntireString)
This is astatic
-factory generator method. It helps the programmer automatically build an instance of thisFunctionalInterface 'StrFilter'
for one of the most common uses/tasks where aString
-FilterPredicate
would be needed.
Here, an input-object'stoString()
result is checked against Java's Regular-Expression matcher. ThePredicate
generated will choose to REJECT input-Object's
by returning FALSE when the Regular-Expression matches theObject.toString()
results.- Parameters:
regEx
- A regular-expression used to compare against an input-Object
(usingObject.toString()
in aPredicate
-test.regExMustMatchEntireString
- When this parameter is passed TRUE, then the regular-expressionPattern
must match the entire inputObject.toString()
. When this parameter is passed FALSE, it only need match some portion or part of the input-parameterString
in thePredicate-test
that is being generated.- Returns:
- A new
StrFilter
that can be used to test and filterString's
, according to the input-parameter'regEx'
- Code:
- Exact Method Body:
// FAIL-FAST: Perform this test BEFORE building the regex, to save the programmer // from later on (when using this factory generated predicate) having a // NullPointerException that is more difficult to isolate. if (regEx == null) throw new NullPointerException( "The Regular-Expression provided to the 'regEx' parameter of this static-factory-builder" + "method, 'regExREJECT,' was null." ); if (regExMustMatchEntireString) { // the java.util.regex.Pattern.asPredicate() method produces a predicate that must // match an entire input string. This would be identical (if it were possible) to insert // a '^' at the beginning of the regex, and an '$' at the end. final Predicate<String> p = regEx.asPredicate().negate(); return (Object o) -> ! p.test(o.toString()); } else return (Object o) -> ! regEx.matcher(o.toString()).find();
-
regExsAND
static StrFilter regExsAND(java.util.regex.Pattern... regExs)
This is astatic
-factory generator method. It helps the programmer automatically build an instance of thisFunctionalInterface 'StrFilter'
for one of the most common uses/tasks where aString
-FilterPredicate
would be needed.
Here, a list of regular-expressions may be passed for input match-criteria. ThePredicate
that is created by this factory-method will have atest()
method that returns TRUE if an input object'sObject.toString()
output matches each-and-every-one of the provided regular-expressions.- Parameters:
regExs
- A list of regular-expressions to match against an input-String
.
NOTE: The regular-expression will be considered to match an inputObject.toString()
result, even if it only matches some part of the inputString
- which is the standard behaviour for Regular-Expressions. If the user wishes to find "Complete Matches" - then attempting to use multiple regular-expressions on the same inputString
is actually somewhat meaningless.
If such comparison-behaviour is required, use the tried-and-true Regular-Ex'^'
and'$'
operators.- Returns:
- A new
StrFilter
that can be used to test and filterString's
, according to the input-parameter'regExs'
- Code:
- Exact Method Body:
// FAIL-FAST: Check that the user-provided-parameters would not cause exceptions once // the lambda-predicate is invoked. for (Pattern regEx : regExs) if (regEx == null) throw new NullPointerException ("One or more of the elements passed to static-factory method 'regexsAND' are null."); // This over-comes a minor "possible complication" - without likely causing much // inefficiency. If a RegEx[] were passed (array, not a '...' list) and that array were // changed after building this Predicate, the Predicate's behavior would fail. // Avoid this (unlikely, but possible) problem by copying the array, and returning a // predicate that stores a different array pointer (because it is to a different array) // inside the Predicate's body. final Pattern[] pArr = regExs.clone(); // FAIL-FAST: Check that the user-provided-parameters would not cause exceptions once // the lambda-predicate is invoked. if (pArr.length == 0) throw new IllegalArgumentException( "This static-factory method 'regExsAND' has been invoked with zero-arguments to the" + "'regExs' var-args parameter." ); return (Object o) -> { String s=o.toString(); // Cycle the Regular-Expressions. "AND" means if even one fails, return FALSE immediately. for (Pattern p : pArr) if (! p.matcher(s).find()) return false; // None of them failed, return TRUE. return true; };
-
regExsOR
static StrFilter regExsOR(java.util.regex.Pattern... regExs)
This is astatic
-factory generator method. It helps the programmer automatically build an instance of thisFunctionalInterface 'StrFilter'
for one of the most common uses/tasks where aString
-FilterPredicate
would be needed.
Here, a list of regular-expressions may be passed for input match-criteria. ThePredicate
that is created by this factory-method will have atest()
method that returns TRUE if an input object'sObject.toString()
output matches any-one-of the provided regular-expressions.- Parameters:
regExs
- A list of regular-expressions to match against an input-String
.
NOTE: The regular-expression will be considered to match an inputObject.toString()
result, even if it only matches some part of the inputString
- which is the standard behaviour for Regular-Expressions. If the user wishes to find "Complete Matches" - then attempting to use multiple regular-expressions on the same inputString
is actually somewhat meaningless.
If such comparison-behaviour is required, use the tried-and-true Regular-Ex'^'
and'$'
operators.- Returns:
- A new
StrFilter
that can be used to test and filterString's
, according to the input-parameter'regExs'
- Code:
- Exact Method Body:
// FAIL-FAST: Check that the user-provided-parameters would not cause exceptions once the lambda-predicate is invoked. for (Pattern regEx : regExs) if (regEx == null) throw new NullPointerException ("One or more of the elements passed to static-factory method 'regExsOR' are null."); // This over-comes a minor "possible complication" - without likely causing much // inefficiency. If a RegEx[] were passed (array, not a '...' list) and that array were // changed after building this Predicate, the Predicate's behavior would fail. // Avoid this (unlikely, but possible) problem by copying the array, and returning a // predicate that stores a different array pointer (because it is to a different array) // inside the Predicate's body. final Pattern[] pArr = regExs.clone(); // FAIL-FAST: Check that the user-provided-parameters would not cause exceptions once // the lambda-predicate is invoked. if (pArr.length == 0) throw new IllegalArgumentException( "This static-factory method 'regExsOR' has been invoked with zero-arguments to the" + "'regExs' var-args parameter." ); return (Object o) -> { String s = o.toString(); // Cycle the Regular-Expressions. "OR" means if even one succeeds, return TRUE immediately. for (Pattern p : pArr) if (p.matcher(s).find()) return true; // None succeeded, so return FALSE. return false; };
-
regExsNAND
static StrFilter regExsNAND(java.util.regex.Pattern... regExs)
This is astatic
-factory generator method. It helps the programmer automatically build an instance of thisFunctionalInterface 'StrFilter'
for one of the most common uses/tasks where aString
-FilterPredicate
would be needed.
Here, a list of regular-expressions may be passed for input match-criteria. ThePredicate
that is created by this factory-method will have atest()
method that returns TRUE if an input object'sObject.toString()
output does-not-match-any of the provided regular-expressions.- Parameters:
regExs
- A list of regular-expressions to match against an input-String
.
NOTE: The regular-expression will be considered to match an inputObject.toString()
result, even if it only matches some part of the inputString
- which is the standard behaviour for Regular-Expressions. If the user wishes to find "Complete Matches" - then attempting to use multiple regular-expressions on the same inputString
is actually somewhat meaningless.
If such comparison-behaviour is required, use the tried-and-true Regular-Ex'^'
and'$'
operators.- Returns:
- A new
StrFilter
that can be used to test and filterString's
, according to the input-parameter'regExs'
. - Code:
- Exact Method Body:
// FAIL-FAST: Check that the user-provided-parameters would not cause exceptions once the // lambda-predicate is invoked. for (Pattern regEx : regExs) if (regEx == null) throw new NullPointerException ("One or more of the elements passed to static-factory method 'regExsNAND' are null."); // This over-comes a minor "possible complication" - without likely causing much // inefficiency. If a RegEx[] were passed (array, not a '...' list) and that array were // changed after building this Predicate, the Predicate's behavior would fail. // Avoid this (unlikely, but possible) problem by copying the array, and returning a // predicate that stores a different array pointer (because it is to a different array) // inside the Predicate's body. final Pattern[] pArr = regExs.clone(); // FAIL-FAST: Check that the user-provided-parameters would not cause exceptions once // the lambda-predicate is invoked. if (pArr.length == 0) throw new IllegalArgumentException( "This static-factory method 'regExsNAND' has been invoked with zero-arguments to the" + "'regExs' var-args parameter." ); return (Object o) -> { String s = o.toString(); // Cycle the Regular-Expressions. "NAND" means if even one succeeds, return FALSE immediately. for (Pattern p : pArr) if (p.matcher(s).find()) return false; // All regex's failed, so return TRUE return true; };
-
regExsXOR
static StrFilter regExsXOR(java.util.regex.Pattern... regExs)
This is astatic
-factory generator method. It helps the programmer automatically build an instance of thisFunctionalInterface 'StrFilter'
for one of the most common uses/tasks where aString
-FilterPredicate
would be needed.
Here, a list of regular-expressions may be passed for input match-criteria. ThePredicate
that is created by this factory-method will have atest()
method that returns TRUE if an input object'sObject.toString()
output matches precisely-one-of the provided regular-expressions.- Parameters:
regExs
- A list of regular-expressions to match against an input-String
.
NOTE: The regular-expression will be considered to match an inputObject.toString()
result, even if it only matches some part of the inputString
- which is the standard behaviour for Regular-Expressions. If the user wishes to find "Complete Matches" - then attempting to use multiple regular-expressions on the same inputString
is actually somewhat meaningless.
If such comparison-behaviour is required, use the tried-and-true Regular-Ex'^'
and'$'
operators.- Returns:
- A new
StrFilter
that can be used to test and filterString's
, according to the input-parameter'regExs'
- Code:
- Exact Method Body:
// FAIL-FAST: Check that the user-provided-parameters would not cause exceptions once the // lambda-predicate is invoked. for (Pattern regEx : regExs) if (regEx == null) throw new NullPointerException ("One or more of the elements passed to static-factory method 'regExsXOR' are null."); // This over-comes a minor "possible complication" - without likely causing much // inefficiency. If a RegEx[] were passed (array, not a '...' list) and that array were // changed after building this Predicate, the Predicate's behavior would fail. // Avoid this (unlikely, but possible) problem by copying the array, and returning a // predicate that stores a different array pointer (because it is to a different array) // inside the Predicate's body. final Pattern[] pArr = regExs.clone(); // FAIL-FAST: Check that the user-provided-parameters would not cause exceptions once // the lambda-predicate is invoked. if (pArr.length == 0) throw new IllegalArgumentException( "This static-factory method 'regExsXOR' has been invoked with zero-arguments to the" + "'regExs' var-args parameter." ); return (Object o) -> { String s = o.toString(); int count = 0; // Cycle the Regular-Expressions. Because this is "XOR" - we must keep a count. // If that count is > 1, we have to return FALSE immediately. for (Pattern p : pArr) if (p.matcher(s).find()) { if (++count > 1) return false; } // If there was precisely one match, return TRUE, otherwise return FALSE. return count == 1; };
-
comparitor
static StrFilter comparitor(TextComparitor tc, java.lang.String... compareStrs)
This is astatic
-factory generator method. It helps the programmer automatically build an instance of thisFunctionalInterface 'StrFilter'
for one of the most common uses/tasks where aString
-FilterPredicate
would be needed. Here, the classTextComparitor
is reused from the'NodeSearch'
package. Please review how each of the pre-defined,static
-instances of classTextComparitor
operate when presented withString
-input.
Example:
StrFilter filter = StrFilter.comparitor (TextComparitor.DOES_NOT_START_WITH, "Today in Washington,"); // The above fiter would REJECT any input Object whose 'toString()' method returned a // String that began with the words "Today in Washington," StrFilter filter2 = StrFilter.comparitor (TextComparitor.CONTAINS_CASE_INSENSITIVE, "Highway 61 Revisited"); // This filter would KEEP / RETAIN any input Object whose 'toString()' method returned a // String that contained the words "Highway 61 Revisited". This comparison to be performed // would be case-insensitive.
- Parameters:
tc
- This is an instance of theclass TextComparitor
, which is defined in the NodeSearch package.compareStrs
- These must be the comparison-String's
used by class'TextComparitor'
for performing the comparisons.- Returns:
- A new
StrFilter
that can be used to test and filterString's
, according to the input-parameters'tc'
and'compareStrs'
- See Also:
TextComparitor
- Code:
- Exact Method Body:
// FAIL-FAST: Check that the user-provided-parameters would not cause exceptions once // the lambda-predicate is invoked. if (tc == null) throw new NullPointerException ("A null TextComparitor has been passed to static-factory method 'comparitorKeep'"); TCCompareStrException.check(compareStrs); final String[] cmpStrs = compareStrs.clone(); // Mostly, this is over-board, but it doesn't slow anything down much, and it // it does force users to think about Object-References and Parameter-Argument // Marshaling. // Builds (and returns) a Predicate<Object> that re-uses the TextComparitor's // 'test(String, String...)' method. return (Object o) -> tc.test(o.toString(), cmpStrs);
-
isEqualKEEP
static StrFilter isEqualKEEP(java.lang.String s, boolean ignoreCase)
This is astatic
-factory generator method. It helps the programmer automatically build an instance of thisFunctionalInterface 'StrFilter'
for one of the most common uses/tasks where aString
-FilterPredicate
would be needed. Here, a singleString
is used as input. ThePredicate
that is created by this factory-method will have atest()
method that returns TRUE only if the an input object's'toString()'
output equals the the input-parameterString 's'
.- Parameters:
s
- This is aString
which will be used to test for equality in the generatedPredicate.test(Object)
ignoreCase
- When this is TRUE, the equality-test will ignore case when performing its comparisons.- Returns:
- A new
StrFilter
that can be used to test and filterObject's
via theObject.toString()
method, and the input-parameter's'
- Code:
- Exact Method Body:
if (s == null) throw new NullPointerException ("A null String has been passed to parameter 's' of static-factory method 'isEqualKEEP'."); // Builds & returns a predicate based on the whether or not 'ignoreCase' is true or false. return ignoreCase ? (Object o) -> o.toString().equalsIgnoreCase(s) : (Object o) -> o.toString().equals(s);
-
isEqualREJECT
static StrFilter isEqualREJECT(java.lang.String s, boolean ignoreCase)
This is astatic
-factory generator method. It helps the programmer automatically build an instance of thisFunctionalInterface 'StrFilter'
for one of the most common uses/tasks where aString
-FilterPredicate
would be needed. Here, a singleString
is used as input. ThePredicate
that is created by this factory-method will have atest()
method that returns TRUE only if an inputObject's
Object.toString()
output does-not-equal the the input-parameterString 's'
.- Parameters:
s
- This is aString
which will be used to test for equality in the generatedPredicate.test(Object)
.ignoreCase
- When this is TRUE, the equality-test will ignore case when performing its comparisons.- Returns:
- A new
StrFilter
that can be used to test and filterObject's
via theObject.toString()
method, and the input-parameter's'
- Code:
- Exact Method Body:
if (s == null) throw new NullPointerException ("A null String has been passed to parameter 's' of static-factory method 'isEqualREJECT'."); // Builds & returns a predicate based on the whether or not 'ignoreCase' is true or false. return ignoreCase ? (Object o) -> ! o.toString().equalsIgnoreCase(s) : (Object o) -> ! o.toString().equals(s);
-
-