Cleaned up HTML features code a bit.

This commit is contained in:
vlofgren 2022-07-08 19:52:12 +02:00
parent 7dea94d36d
commit b0c40136ca

View File

@ -3,18 +3,16 @@ package nu.marginalia.wmsa.edge.converting.processor.logic;
import java.util.Collection; import java.util.Collection;
public enum HtmlFeature { public enum HtmlFeature {
MEDIA(0, "special:media"), MEDIA( "special:media"),
JS(1, "special:scripts"), JS("special:scripts"),
AFFILIATE_LINK(2, "special:affiliate"), AFFILIATE_LINK( "special:affiliate"),
TRACKING(3, "special:tracking"), TRACKING("special:tracking"),
COOKIES(4, "special:cookies") COOKIES("special:cookies")
; ;
public final int bit;
private final String keyword; private final String keyword;
HtmlFeature(int bit, String keyword) { HtmlFeature(String keyword) {
this.bit = bit;
this.keyword = keyword; this.keyword = keyword;
} }
@ -23,12 +21,14 @@ public enum HtmlFeature {
} }
public static int encode(Collection<HtmlFeature> featuresAll) { public static int encode(Collection<HtmlFeature> featuresAll) {
return featuresAll.stream().mapToInt(f -> 1 << f.bit).reduce(0, (l, r) -> (l|r)); int ret = 0;
for (var feature : featuresAll) {
ret |= (1 << (feature.ordinal()));
}
return ret;
} }
public static boolean hasFeature(int value, HtmlFeature feature) { public static boolean hasFeature(int value, HtmlFeature feature) {
return (value & (1<< feature.bit)) != 0; return (value & (1<< feature.ordinal())) != 0;
}
public static int addFeature(int value, HtmlFeature feature) {
return (value | (1<< feature.bit));
} }
} }