Package Torello.HTML
Interface URLFilter
-
- All Superinterfaces:
java.util.function.Predicate<java.net.URL>,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 URLFilter extends java.util.function.Predicate<java.net.URL>, java.io.Serializable
A simple lambda-target which extendsPredicate<URL>.
The purpose of this filter is for skipping and selecting which links to follow. While crawling through HTML from a News-Site, there will eventually be links that are irrelevant. Implement thisFunctionalInterface, or use a Lambda-Expression to create aURL-Filter for choosing whichURL'sto follow.
Reusing classStrFilter:
Using aStrFilterto quickly build aURLFilterinstance can make the job of testing and filtering theString'sin aURLa lot easier. That class has several Factory-Builder Methods, and includes several consistent Exception-Messages.- See Also:
StrFilter
Hi-Lited Source-Code:- View Here: Torello/HTML/URLFilter.java
- Open New Browser-Tab: Torello/HTML/URLFilter.java
File Size: 9,418 Bytes Line Count: 214 '\n' Characters Found
-
-
Field Summary
Fields Modifier and Type Field static URLFilterimagesKEEPstatic URLFilterimagesREJECTstatic longserialVersionUID
-
Method Summary
@FunctionalInterface: (Lambda) Method Modifier and Type Method booleantest(URL url)Methods: Static Factory-Builder Modifier and Type Method static URLFilterfromStrFilter(StrFilter sf)Default Methods: Remove Elements with Iterator.remove() Modifier and Type Method default intfilter(Iterable<URL> urls)
-
-
-
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;
-
imagesKEEP
static final URLFilter imagesKEEP
ThisURLFilterwill KEEP any ImageURL'swhose name ends with the standard image filenames.
Warning:
There are occasions where an Image-URLis "handled" by a web-server internally, and the actualURLitself does not look like an image file-name at all. This has the inconvenient implication for this (factory-generated)Predicatethat it might return erroneous results. An actual image file that does not end with'.jpg'or'.bmp'could be rejected, and aURLthat happens to end with theseString'sbut is not an image, might also be kept.- See Also:
StrCmpr.endsWithXOR_CI(String, String[])- Code:
- Exact Field Declaration Expression:
public static final URLFilter imagesKEEP = (URL url) -> { return StrCmpr.endsWithXOR_CI (url.toString().trim(), ".jpg", ".jpeg", ".gif", ".png", ".bmp"); };
-
imagesREJECT
static final URLFilter imagesREJECT
ThisURLFilterwill REJECT any ImageURL'swhose name ends with the standard image filenames.
Warning:
There are occasions where an Image-URLis "handled" by a web-server internally, and the actualURLitself does not look like an image file-name at all. This has the inconvenient implication for this (factory-generated)Predicatethat it might return erroneous results. An actual image file that does not end with'.jpg'or'.bmp'could be kept, and aURLthat happens to end with theseString'sbut is not an image, could be rejected.- See Also:
StrCmpr.endsWithNAND_CI(String, String[])- Code:
- Exact Field Declaration Expression:
public static final URLFilter imagesREJECT = (URL url) -> { return StrCmpr.endsWithNAND_CI (url.toString().trim(), ".jpg", ".jpeg", ".gif", ".png", ".bmp"); };
-
-
Method Detail
-
test
boolean test(java.net.URL url)
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.
This method will receive aURL.The purpose of this method is to provide an easy means to filter certainURL'sfrom aURL-generating list.PRECISE NOTE: This method should returnFALSEif the passedURLshould be skipped. A return value ofTRUEimplies that theURLis not to be ignored or passed over, but rather 'kept.'
This behavior is compatible with the Java Stream's method"filter(Predicate<...>)".- Specified by:
testin interfacejava.util.function.Predicate<java.net.URL>- Parameters:
url- This is aURLthat will be checked against the constraints specified by'this'filter.- Returns:
- When implementing this method, returning
TRUEmust mean that theURLhas passed the filter's test-requirements (and will subsequently be retained by whatever code is carrying out the filter operation).
-
filter
default int filter(java.lang.Iterable<java.net.URL> urls)
This is similar to the java streams functionfilter(Predicate<>). Elements that do not meet the criteria specified by this (factory-generated)URLFilter- specifically, if an element of the input-parameter'urlList'would evaluate toFALSE- then that element shall be removed from the list.- Parameters:
urls- AnIterableofURL'swhich the user would like filtered using'this'filter.- Returns:
- The number of elements that were removed from parameter
'urls'based on the results of theURLFilter.test()of'this'instance. - Code:
- Exact Method Body:
int removeCount = 0; Iterator<URL> iter = urls.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 URLFilter fromStrFilter(StrFilter sf)
This wraps aStrFilterinside of aURLFilter. TheString-comparison that is performed will use the full-path-name of theURL.StrFilterNote:
The class'StrFilter'can be used in conjunction with the class-specific filters, for instance, this class'URLFilter'- Parameters:
sf- This is aString Predicatethat has (usually, but not required) been built by one of the manyString-Filter Factory-Build static-methods ofclass StrFilter.ThePredicate'sthat are constructed via the build methods ofStrFiltercall the standard methodjava.lang.Object.toString()on the objects they receive for testing.- Returns:
- FileNodeFilter This will return an instance of a
URLFilterthat will test theurlas 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 (URL url) -> sf.test(url);
-
-