| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package com.adx.tencent.honor;
- import java.util.List;
- import java.util.Map;
- public class HonorPlacement {
- private long baiduMediaId;
- private String baiduAppId;
- private String baiduTagId;
- private Map<String, HonorAppPlacement> platforms;
- private long bidFloor;
- private List<Integer> actionTypes;
- public long getBaiduMediaId() { return baiduMediaId; }
- public void setBaiduMediaId(long v) { this.baiduMediaId = v; }
- public String getBaiduAppId() { return baiduAppId; }
- public void setBaiduAppId(String v) { this.baiduAppId = v; }
- public String getBaiduTagId() { return baiduTagId; }
- public void setBaiduTagId(String v) { this.baiduTagId = v; }
- public Map<String, HonorAppPlacement> getPlatforms() { return platforms; }
- public void setPlatforms(Map<String, HonorAppPlacement> v) { this.platforms = v; }
- public long getBidFloor() { return bidFloor; }
- public void setBidFloor(long v) { this.bidFloor = v; }
- public List<Integer> getActionTypes() { return actionTypes; }
- public void setActionTypes(List<Integer> v) { this.actionTypes = v; }
- public Resolved resolve(Map<String, String> params) {
- String tagId = firstParam(params, "tagId", "tag_id", "baidu_tag_id");
- if (tagId != null && !tagId.isBlank()) {
- Resolved r = resolveByTagId(tagId);
- if (r != null) return r;
- throw new IllegalArgumentException("unknown honor baidu tag id: " + tagId);
- }
- String platform = normalizePlatform(firstParam(params, "platform", "os", "system", "device_os"));
- if (platform != null && !platform.isBlank()) {
- Resolved r = resolveByPlatform(platform);
- if (r != null) return r;
- throw new IllegalArgumentException("unsupported honor baidu platform: " + platform);
- }
- if (baiduAppId != null && !baiduAppId.isBlank()
- && baiduTagId != null && !baiduTagId.isBlank()) {
- return new Resolved(null, baiduAppId, baiduTagId);
- }
- if (platforms != null && platforms.size() == 1) {
- String p = platforms.keySet().iterator().next();
- Resolved r = resolveByPlatform(p);
- if (r != null) return r;
- }
- throw new IllegalArgumentException("missing honor tagId or platform");
- }
- private Resolved resolveByTagId(String tagId) {
- if (platforms != null) {
- for (Map.Entry<String, HonorAppPlacement> e : platforms.entrySet()) {
- HonorAppPlacement p = e.getValue();
- if (p.getAppId() != null && p.getTagIds() != null && p.getTagIds().contains(tagId)) {
- return new Resolved(e.getKey(), p.getAppId(), tagId);
- }
- }
- }
- if (baiduAppId != null && tagId.equals(baiduTagId)) {
- return new Resolved(null, baiduAppId, baiduTagId);
- }
- return null;
- }
- private Resolved resolveByPlatform(String platform) {
- if (platforms == null) return null;
- HonorAppPlacement p = platforms.get(platform);
- if (p == null || p.getAppId() == null || p.getTagIds() == null || p.getTagIds().isEmpty()) {
- return null;
- }
- if (p.getTagIds().size() > 1) {
- throw new IllegalArgumentException(
- "tagId is required for honor platform " + platform + " because multiple tag ids are configured");
- }
- return new Resolved(platform, p.getAppId(), p.getTagIds().get(0));
- }
- private static String firstParam(Map<String, String> p, String... keys) {
- if (p == null) return null;
- for (String k : keys) {
- String v = p.get(k);
- if (v != null && !v.isBlank()) return v.trim();
- }
- return null;
- }
- static String normalizePlatform(String value) {
- if (value == null) return null;
- return switch (value.toLowerCase().trim()) {
- case "ios", "iphone", "ipad", "1" -> "ios";
- case "android", "安卓", "0", "2" -> "android";
- default -> null;
- };
- }
- public static class HonorAppPlacement {
- private String appId;
- private List<String> tagIds;
- public String getAppId() { return appId; }
- public void setAppId(String v) { this.appId = v; }
- public List<String> getTagIds() { return tagIds; }
- public void setTagIds(List<String> v) { this.tagIds = v; }
- }
- public record Resolved(String platform, String appId, String tagId) {}
- }
|