66import org .apache .commons .lang3 .StringUtils ;
77import org .prebid .server .proto .openrtb .ext .request .ExtRequestPrebidDataEidPermissions ;
88
9+ import java .util .Collection ;
910import java .util .HashMap ;
1011import java .util .HashSet ;
1112import java .util .List ;
1415
1516public final class EidPermissionIndex {
1617
17- // bitmask for which fields are present in a permission
18- private static final int INSERTER = 1 ; // 0001
19- private static final int SOURCE = 2 ; // 0010
20- private static final int MATCHER = 4 ; // 0100
21- private static final int MM = 8 ; // 1000
18+ // Bitmask for which fields are present in the EID permission
19+ private static final int INSERTER = 1 << 0 ;
20+ private static final int SOURCE = 1 << 1 ;
21+ private static final int MATCHER = 1 << 2 ;
22+ private static final int MM = 1 << 3 ;
2223 private static final String WILDCARD_BIDDER = "*" ;
2324
2425 private final Map <Integer , Map <Key , Set <String >>> ruleIndexByMask ;
@@ -30,13 +31,13 @@ private EidPermissionIndex(Map<Integer, Map<Key, Set<String>>> ruleIndexByMask)
3031 this .ruleIndexByMask = ruleIndexByMask ;
3132 }
3233
33- public static EidPermissionIndex build (List <ExtRequestPrebidDataEidPermissions > permissions ) {
34+ public static EidPermissionIndex build (Collection <ExtRequestPrebidDataEidPermissions > permissions ) {
35+ final Map <Integer , Map <Key , Set <String >>> idx = new HashMap <>();
36+
3437 if (ObjectUtils .isEmpty (permissions )) {
35- return null ;
38+ return new EidPermissionIndex ( idx ) ;
3639 }
3740
38- final Map <Integer , Map <Key , Set <String >>> idx = new HashMap <>();
39-
4041 for (ExtRequestPrebidDataEidPermissions permission : permissions ) {
4142 final List <String > bidders = CollectionUtils .emptyIfNull (permission .getBidders ())
4243 .stream ()
@@ -66,25 +67,17 @@ public static EidPermissionIndex build(List<ExtRequestPrebidDataEidPermissions>
6667 }
6768
6869 private static int maskOf (String inserter , String source , String matcher , Integer mm ) {
69- int mask = 0 ;
70-
71- if (inserter != null ) {
72- mask |= INSERTER ;
73- }
74- if (source != null ) {
75- mask |= SOURCE ;
76- }
77- if (matcher != null ) {
78- mask |= MATCHER ;
79- }
80- if (mm != null ) {
81- mask |= MM ;
82- }
83-
84- return mask ;
70+ return (inserter != null ? INSERTER : 0 )
71+ | (source != null ? SOURCE : 0 )
72+ | (matcher != null ? MATCHER : 0 )
73+ | (mm != null ? MM : 0 );
8574 }
8675
8776 public boolean isAllowed (Eid eid , String bidder ) {
77+ if (ObjectUtils .isEmpty (ruleIndexByMask )) {
78+ return true ;
79+ }
80+
8881 final int eidMask = maskOf (eid .getInserter (), eid .getSource (), eid .getMatcher (), eid .getMm ());
8982
9083 boolean ruleMatched = false ;
@@ -93,8 +86,7 @@ public boolean isAllowed(Eid eid, String bidder) {
9386 for (Map .Entry <Integer , Map <Key , Set <String >>> ruleBucket : ruleIndexByMask .entrySet ()) {
9487 final int ruleMask = ruleBucket .getKey ();
9588
96- // rule can only match if all its required fields exist on the Eid
97- if ((ruleMask & eidMask ) != ruleMask ) {
89+ if (!isMaskMatched (ruleMask , eidMask )) {
9890 continue ;
9991 }
10092
@@ -112,7 +104,11 @@ public boolean isAllowed(Eid eid, String bidder) {
112104 }
113105 }
114106
115- // allow-by- default: if no rule matched at all, allow
107+ // Allow by default if no rule matched at all
116108 return !ruleMatched ;
117109 }
110+
111+ private boolean isMaskMatched (int ruleMaks , int eidMask ) {
112+ return (ruleMaks & eidMask ) == ruleMaks ;
113+ }
118114}
0 commit comments