Package Torello.Java
Interface FileNodeFilter
-
- All Superinterfaces:
java.util.function.Predicate<FileNode>
,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 FileNodeFilter extends java.util.function.Predicate<FileNode>, java.io.Serializable
A simple functional-interface (lambda) for filtering lists ofFileNode
.
This class can be used to buildjava.util.function.Predicate<FileNode>
when searching through a file-system. Theclass FileNode
uses these to allow a programmer to decide which file-names should be read from disk when building file-trees into Java Memory. This class is in accordance with Java'sFunctional Interface
rules, so a simple "lambda-expression" will almost always do the trick.
UsingStrFilter:
Using the classStrFilter
to quickly buildFileNodeFilter
instances can (on occasion) make the task of testing and filtering the file-name and directory strings easier.
Example:
String[] myFilesList = { "file.java", "file2.java", ... }; String[] foundFilesList = FileNode .createRoot("src/main/java/") .loadedTree(FileNodeFilter.fromStrFilter(-1, StrFilter.strListKEEP(myFilesList), null)); .flattenJustFiles(SORTED_FILENAME_ARRAY);
- See Also:
StrFilter
Hi-Lited Source-Code:- View Here: Torello/Java/FileNodeFilter.java
- Open New Browser-Tab: Torello/Java/FileNodeFilter.java
-
-
Field Summary
Fields Modifier and Type Field static long
serialVersionUID
-
Method Summary
@FunctionalInterface (Lambda) Method Modifier and Type Method boolean
test(FileNode fn)
Methods: Static Factory-Builder Modifier and Type Method static FileNodeFilter
fromStrFilter(StrFilter sf)
Methods: Default Composition & Builder Modifier and Type Method default FileNodeFilter
and(FileNodeFilter other)
default FileNodeFilter
negate()
default FileNodeFilter
or(FileNodeFilter other)
Default Methods: Remove Elements with Iterator.remove() Modifier and Type Method default int
filter(Iterable<FileNode> fileNodes)
-
-
-
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(FileNode fn)
FUNCTIONAL-INTERFACE BOOLEAN METHOD: This is the method that fulfils thisFunctional-Interface 'test(...)'
method.- Specified by:
test
in interfacejava.util.function.Predicate<FileNode>
- Parameters:
fn
- Any file node- Returns:
- A TRUE value should mean that the
FileNode
has passed the test (will be kept / retained). A return value of FALSE should indicate that theFileNode
needs to be filtered.
-
and
default FileNodeFilter and(FileNodeFilter other)
Standard Java-Predicate
, Java Functional-Interface AND routine.
Logically AND's'this'
FileNodeFilter
with the filter parameter'other'
- Parameters:
other
- A 2ndFileNodeFilter
with which'this'
filter may be AND'ed.- Returns:
- A new
FileNodeFilter
that is the logical-and of'this'
filter and'other'
- Code:
- Exact Method Body:
// FAIL-FAST: Check that the lambda will not throw a null pointer exception prior to // creating the lambda. if (other == null) throw new NullPointerException ("The parameter 'other' to FileNodeFilter.and(other) was null."); return (FileNode f) -> this.test(f) && other.test(f);
-
or
default FileNodeFilter or(FileNodeFilter other)
Standard Java-Predicate
, Java Functional-Interface OR routine.
Logically OR's'this'
FileNodeFilter
with the filter parameter'other'
- Parameters:
other
- A 2ndFileNodeFilter
with which'this'
filter may be or'ed.- Returns:
- A new FileNodeFilter that is the logical-or of
'this'
filter and "other" - Code:
- Exact Method Body:
// FAIL-FAST: Check that the lambda will not throw a null pointer exception prior to // creating the lambda. if (other == null) throw new NullPointerException ("The parameter 'other' to FileNodeFilter.or(other) was null.."); return (FileNode f) -> this.test(f) || other.test(f);
-
negate
default FileNodeFilter negate()
Standard Java-Predicate
, Java Functional-Interface NEGATE routine.
Generates 's aFileNodeFilter
that is the "logical-not" of'this'
filter.- Specified by:
negate
in interfacejava.util.function.Predicate<FileNode>
- Returns:
- A new
FileNodeFilter
that would return TRUE whenever'this'
would return FALSE, and vice-versa. - Code:
- Exact Method Body:
return (FileNode f) -> ! this.test(f);
-
filter
default int filter(java.lang.Iterable<FileNode> fileNodes)
This is similar to the java streams functionfilter(Predicate<>)
. Elements that do not meet the criteria specified by thisFileNodeFilter
- specifically, if an element of the input-parameter'urlList'
would evaluate to FALSE - then that element shall be removed from the list.- Parameters:
fileNodes
- AnIterable
ofFileNode's
which the user would like filtered using'this'
filter.- Returns:
- The number of elements that were removed from parameter
'fileNodes'
based on the results of the'test(FileNode)'
lambda-methodFileNodeFilter.test()
of'this'
instance. - Code:
- Exact Method Body:
int removeCount = 0; Iterator<FileNode> iter = fileNodes.iterator(); // If the filter test returns FALSE, then remove the URL from the collection. // Increment the removeCount Counter. while (iter.hasNext()) if (! test(iter.next())) { removeCount++; iter.remove(); } return removeCount;
-
fromStrFilter
static FileNodeFilter fromStrFilter(StrFilter sf)
This wraps aStrFilter
inside of aFileNodeFilter
. TheString
-comparison that is performed will use the full-path-name of theFileNode
.
StrFilter NOTE: The class'StrFilter'
can be used in conjunction with the class-specific filters, for instance, thisclass FileNodeFilter
- Parameters:
sf
- This is a "String Predicate" that has (usually, but not required) been built by one of the manyString
-Filter Factory-Build static-methods ofclass StrFilter
. The Predicate's that are constructed via the build methods ofStrFilter
call theObject.toString()
method on the objects they receive for testing.- Returns:
- FileNodeFilter This will return an instance of a
FileNodeFilter
that will test the full-path-name as aString
. - See Also:
StrFilter
- Code:
- Exact Method Body:
if (sf == null) throw new NullPointerException( "The String-Filter Predicate Parameter 'sf' in static-factory builder method " + "'fromStrFilter' was passed a null value." ); return (FileNode fn) -> sf.test(fn);
-
-