GetDevIdUtil.java 2.47 KB
Newer Older
wang-jinfeng committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
package mobvista.dmp.datasource.adn.mapreduce;

import mobvista.dmp.util.MRUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * @package: mobvista.dmp.datasource.adn.mapreduce
 * @author: wangjf
 * @date: 2019-07-04
 * @time: 14:37
 * @emial: jinfeng.wang@mobvista.com
 * @phone: 152-1062-7698
 */
public class GetDevIdUtil {
    private static Pattern idPtn = Pattern.compile("0*-0*-0*-0*-0*");
    private static Pattern didPtn = Pattern.compile("^[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}$");
    private static Pattern splitPtn = Pattern.compile("-");

    public static String getIdByIdAndPlatform(String gaid, String idfa, String extSysid, String platform) {

        String devId = "";
        String deviceType = "";
        if (!idPtn.matcher(gaid).matches() && splitPtn.split(gaid).length == 5 && "android".equals(platform.toLowerCase())) {
            devId = gaid;
            deviceType = "gaid";
        } else if (!idPtn.matcher(idfa).matches() && splitPtn.split(idfa).length == 5 && "ios".equals(platform.toLowerCase())) {
            devId = idfa;
            deviceType = "idfa";
        }
        if (StringUtils.isNotBlank(devId)) {
            return MRUtils.JOINER.join(devId, deviceType);
        } else {
            return null;
        }
    }

    public static String getExtSysId(String extSysid) {

        String devId = "";
        String deviceType = "sysid";
        if (StringUtils.isNotBlank(extSysid) && !",".equals(extSysid)) {
            String[] exts = extSysid.split(",");
            if (StringUtils.isNotBlank(exts[0]) && !idPtn.matcher(exts[0]).matches() && splitPtn.split(exts[0]).length == 5) {
                devId = exts[0];
            } else if (exts.length == 2 && !idPtn.matcher(exts[1]).matches() && splitPtn.split(exts[1]).length == 5) {
                devId = exts[1];
            }
        }
        if (StringUtils.isNotBlank(devId)) {
            return MRUtils.JOINER.join(devId, deviceType);
        } else {
            return null;
        }
    }

    public static String getIdfv(String cdn_ab) {

        String devId = "";
        if (StringUtils.isNotBlank(cdn_ab) && !",".equals(cdn_ab)) {
            String[] exts = cdn_ab.split(",", -1);
            if (StringUtils.isNotBlank(exts[0]) && didPtn.matcher(exts[0]).matches()) {
                devId = exts[0];
            }
        }
        if (StringUtils.isNotBlank(devId)) {
            return devId;
        } else {
            return "";
        }
    }
}