66
77import java .util .List ;
88import java .util .Optional ;
9+ import java .util .Set ;
910
1011/**
1112 * Utility class to detect links.
1213 */
1314public class LinkDetections {
1415
16+
1517 private LinkDetections () {
1618 throw new UnsupportedOperationException ("Utility class" );
1719 }
@@ -20,15 +22,13 @@ private LinkDetections() {
2022 * Extracts all links from the given content.
2123 *
2224 * @param content the content to search through
23- * @param filterSuppressed filters links suppressed with {@literal <url>}
24- * @param filterNonHttpSchemes filters links that are not using http scheme
25+ * @param filter the filters applied to the urls
2526 * @return a list of all found links, can be empty
2627 */
27- public static List <String > extractLinks (String content , boolean filterSuppressed ,
28- boolean filterNonHttpSchemes ) {
28+ public static List <String > extractLinks (String content , Set <LinkFilter > filter ) {
2929 return new UrlDetector (content , UrlDetectorOptions .BRACKET_MATCH ).detect ()
3030 .stream ()
31- .map (url -> toLink (url , filterSuppressed , filterNonHttpSchemes ))
31+ .map (url -> toLink (url , filter ))
3232 .flatMap (Optional ::stream )
3333 .toList ();
3434 }
@@ -43,16 +43,15 @@ public static boolean containsLink(String content) {
4343 return !(new UrlDetector (content , UrlDetectorOptions .BRACKET_MATCH ).detect ().isEmpty ());
4444 }
4545
46- private static Optional <String > toLink (Url url , boolean filterSuppressed ,
47- boolean filterNonHttpSchemes ) {
46+ private static Optional <String > toLink (Url url , Set <LinkFilter > filter ) {
4847 String raw = url .getOriginalUrl ();
49- if (filterSuppressed && raw .contains (">" )) {
48+ if (filter . contains ( LinkFilter . SUPPRESSED ) && raw .contains (">" )) {
5049 // URL escapes, such as "<http://example.com>" should be skipped
5150 return Optional .empty ();
5251 }
5352 // Not interested in other schemes, also to filter out matches without scheme.
5453 // It detects a lot of such false-positives in Java snippets
55- if (filterNonHttpSchemes && !raw .startsWith ("http" )) {
54+ if (filter . contains ( LinkFilter . NON_HTTP_SCHEME ) && !raw .startsWith ("http" )) {
5655 return Optional .empty ();
5756 }
5857
0 commit comments