Skip to content

Commit 9a711eb

Browse files
committed
Minor refactoring
1 parent a3153a7 commit 9a711eb

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

src/main/java/org/prebid/server/auction/ExchangeService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ private EidPermissionIndex getEidPermissions(ExtRequestPrebid prebid) {
584584
return Optional.ofNullable(prebid)
585585
.map(ExtRequestPrebid::getData)
586586
.map(ExtRequestPrebidData::getEidPermissions)
587+
.map(CollectionUtils::emptyIfNull)
587588
.map(EidPermissionIndex::build)
588589
.orElse(null);
589590
}
@@ -680,10 +681,7 @@ private List<Eid> resolveAllowedEids(List<Eid> userEids, String bidder, EidPermi
680681
private boolean isUserEidAllowed(Eid eid,
681682
EidPermissionIndex eidPermissionIndex,
682683
String bidder) {
683-
if (eidPermissionIndex == null) {
684-
return true;
685-
}
686-
return eidPermissionIndex.isAllowed(eid, bidder);
684+
return eidPermissionIndex == null || eidPermissionIndex.isAllowed(eid, bidder);
687685
}
688686

689687
private List<AuctionParticipation> getAuctionParticipation(

src/main/java/org/prebid/server/auction/model/EidPermissionIndex.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.apache.commons.lang3.StringUtils;
77
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidDataEidPermissions;
88

9+
import java.util.Collection;
910
import java.util.HashMap;
1011
import java.util.HashSet;
1112
import java.util.List;
@@ -14,11 +15,11 @@
1415

1516
public 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

Comments
 (0)