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;
public enum HtmlFeature {
MEDIA(0, "special:media"),
JS(1, "special:scripts"),
AFFILIATE_LINK(2, "special:affiliate"),
TRACKING(3, "special:tracking"),
COOKIES(4, "special:cookies")
MEDIA( "special:media"),
JS("special:scripts"),
AFFILIATE_LINK( "special:affiliate"),
TRACKING("special:tracking"),
COOKIES("special:cookies")
;
public final int bit;
private final String keyword;
HtmlFeature(int bit, String keyword) {
this.bit = bit;
HtmlFeature(String keyword) {
this.keyword = keyword;
}
@ -23,12 +21,14 @@ public enum HtmlFeature {
}
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) {
return (value & (1<< feature.bit)) != 0;
}
public static int addFeature(int value, HtmlFeature feature) {
return (value | (1<< feature.bit));
return (value & (1<< feature.ordinal())) != 0;
}
}