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 entirety, from all code written in Java. Java is a Garbage-Collected Language, and one of the most salient benefits of this is the ability to work with 'immutable strings' - such that the programmer has to worry very little about memory leaks and invalid pointer references!
The entire Java-HTML Search Package is based on the 'immutable' aspect of a Java-String. All of theHTMLNode
classes in this package are just wrappers around JavaString's
.
This class is designed to make it easier to perform 'filter-operations' on a set ofString's
of java-strings. This is extremely simple, and may be used with any POJO that has a workingtoString()
method. In fact, both the interfaceFileNodeFilter
and the interfaceURLFilter
will accept a'StrFilter'
as a Constructor-Initializer.
Hi-Lited Source-Code:- View Here: Torello/Java/StrFilter.java
- Open New Browser-Tab: Torello/Java/StrFilter.java
File Size: 32,506 Bytes Line Count: 694 '\n' Characters Found
-
-
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)
FunctionalInterface Target-Method:
This method corresponds to the@FunctionalInterface
Annotation's method requirement. It is the only non-default
, non-static
method in this interface, and may be the target of a Lambda-Expression or'::'
(double-colon) Function-Pointer.
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).
Standard Filter Behavior:
This method should returnFALSE
if the passedString
should be skipped. A return value ofTRUE
implies that theString
is not to be ignored or passed over, but rather 'kept.'
This behavior is consisten with the Java Stream's methodStream.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 returnTRUE
which would imply that the object shall not be filtered. If the object fails the test, this method should returnFALSE
, 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 theObject
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)
-
strListKEEP
static StrFilter strListKEEP(boolean ignoreCase, java.lang.String... strList)
Convenience Method
Invokes:strListKEEP(Iterator, boolean)
Converts:String[]
VarArgs toIterator<String>
-
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
)
-
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 a'StrFilter'
for some of the most common uses/tasks in which 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 isTRUE
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)
-
strListREJECT
static StrFilter strListREJECT(boolean ignoreCase, java.lang.String... strList)
Convenience Method
Invokes:strListREJECT(Iterator, boolean)
Converts:String[]
VarArgs toIterator<String>
-
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
)
-
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 a'StrFilter'
for some of the most common uses/tasks in which 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 isTRUE
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 a'StrFilter'
for some of the most common uses/tasks in which 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 returningTRUE
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 passedTRUE
, then the regular-expressionPattern
must match the entire inputObject.toString()
. When this parameter is passedFALSE
, 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 a'StrFilter'
for some of the most common uses/tasks in which 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 returningFALSE
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 passedTRUE
, then the regular-expressionPattern
must match the entire inputObject.toString()
. When this parameter is passedFALSE
, 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 a'StrFilter'
for some of the most common uses/tasks in which 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 returnsTRUE
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 a'StrFilter'
for some of the most common uses/tasks in which 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 returnsTRUE
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 a'StrFilter'
for some of the most common uses/tasks in which 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 returnsTRUE
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 a'StrFilter'
for some of the most common uses/tasks in which 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 returnsTRUE
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 a'StrFilter'
for some of the most common uses/tasks in which 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); // Mostly, this is over-board, but it doesn't slow anything down much, and it does force // users to think about Object-References and Parameter-Argument Marshaling. final String[] cmpStrs = compareStrs.clone(); // 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 a'StrFilter'
for some of the most common uses/tasks in which 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 returnsTRUE
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 isTRUE
, 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 a'StrFilter'
for some of the most common uses/tasks in which 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 returnsTRUE
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 isTRUE
, 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);
-
-