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 is used to filterFileNode-instances when searching through a File-System. ClassFileNodeuses these filters to allow a programmer to decide which File-Names to include or reject when building many of the containers and / or trees utilized by that class.
Since this class is aFunctional-Interface, it is also (therefore) a Lambda-Target, meaning that any Java Lambda-Expression may be passed to a method that receives this class as a parameter (in place of an actual instance of this class).
Using StrFilter:
This class may also be built from an instance of classStrFilter. This allows a programmer to leverage the constructors written for classStrFilterinto this class as well.
Using the classStrFilterto quickly buildFileNodeFilterinstances can (on occasion) make the task of testing and filtering the file-name and directoryString'seasier. The example provided below should hilite the advantages.
Example:
String[] myFilesList = { "file.java", "file2.java", ... }; FileNodeFilter myFilter = FileNodeFilter.fromStrFilter(StrFilter.strListKEEP(myFilesList)); String[] foundFilesList = FileNode .createRoot("src/main/java/") .loadTree(-1, myFilter, 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
File Size: 5,445 Bytes Line Count: 140 '\n' Characters Found
-
-
Field Summary
Fields Modifier and Type Field static longserialVersionUID
-
Method Summary
@FunctionalInterface: (Lambda) Method Modifier and Type Method booleantest(FileNode fn)Methods: Static Factory-Builder Modifier and Type Method static FileNodeFilterfromStrFilter(StrFilter sf)Methods: Default Composition & Builder Modifier and Type Method default FileNodeFilterand(FileNodeFilter other)default FileNodeFilternegate()default FileNodeFilteror(FileNodeFilter other)Default Methods: Remove Elements with Iterator.remove() Modifier and Type Method default intfilter(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 theSerializableImplementation 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)
FunctionalInterface Target-Method:
This method corresponds to the@FunctionalInterfaceAnnotation's method requirement. It is the only non-default, non-staticmethod in this interface, and may be the target of a Lambda-Expression or'::'(double-colon) Function-Pointer.- Specified by:
testin interfacejava.util.function.Predicate<FileNode>- Parameters:
fn- Any file node- Returns:
- A
TRUEvalue should mean that theFileNodehas passed the test (will be kept / retained). A return value ofFALSEshould indicate that theFileNodeneeds to be filtered.
-
and
default FileNodeFilter and(FileNodeFilter other)
Standard Java-Predicate, Java Functional-Interface AND routine.
Logically AND's'this'FileNodeFilterwith the filter parameter'other'- Parameters:
other- A 2ndFileNodeFilterwith which'this'filter may be AND'ed.- Returns:
- A new
FileNodeFilterthat 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'FileNodeFilterwith the filter parameter'other'- Parameters:
other- A 2ndFileNodeFilterwith 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 aFileNodeFilterthat is the "logical-not" of'this'filter.- Specified by:
negatein interfacejava.util.function.Predicate<FileNode>- Returns:
- A new
FileNodeFilterthat would returnTRUEwhenever'this'would returnFALSE, 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 toFALSE- then that element shall be removed from the list.- Parameters:
fileNodes- AnIterableofFileNode'swhich 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 aStrFilterinside of aFileNodeFilter. TheString-comparison that is performed will use the full-path-name of theFileNode.- 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 ofStrFiltercall theObject.toString()method on the objects they receive for testing.- Returns:
- FileNodeFilter This will return an instance of a
FileNodeFilterthat 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);
-
-