ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实用的ID生成器

2022-05-14 12:00:46  阅读:180  来源: 互联网

标签:int 生成器 id 实用 public static 0xff sb ID


/**
 * ClassName: IDGenerator <br/>
 * 业务层id生成器
 */
public final class IDGenerator {

    private static final long BASE_TS = 1478016000;

    private static int serviceUniqueIndex = RandomUtils.nextInt(0, 16);

    private static final char[] URL_SAFE_ENCODE_TABLE = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
    };

    private static final byte[] DECODE_TABLE = {
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54,
            55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 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, -1, -1, -1, -1, 63, -1, 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
    };

    private static final AtomicInteger counter = new AtomicInteger(0);
    
    private IDGenerator() {

    }

    public static String generate(IDTypeEnum idType) {
        byte[] barr = new byte[48];
        
        // 5位给type
        int2bytes(idType.getCode(), barr, 0, 5);
        
        // 29位给秒级时间戳
        int ts = (int) (System.currentTimeMillis() / 1000 - BASE_TS);
        int2bytes(ts, barr, 5, 34);

        //4位给机器,vtd-admin + osp-tdapi,算上预发布一共有12台注册的机器
        int2bytes(serviceUniqueIndex, barr, 34, 38);

        // 10位给自增
        int auto = counter.incrementAndGet() % 1024;
        int2bytes(auto, barr, 38, 48);
        
        return bitMaptoString(barr);
    }

    public static IDTypeEnum getIDType(String id) {
        if (id == null || id.length() != 8) {
            return null;
        }
        try {
            int type = DECODE_TABLE[id.charAt(0)] >> 1;
            return EnumUtils.valueOfInt(IDTypeEnum.class, type);
        } catch (Exception e) {
            return null;
        }
    }

    private static String bitMaptoString(byte[] bitmap) {
        assert bitmap.length % 6 == 0;
        int len = bitmap.length / 6;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            int baseIdx = i * 6;
            int idx = bitmap[baseIdx] << 5
                    | bitmap[baseIdx + 1] << 4
                    | bitmap[baseIdx + 2] << 3
                    | bitmap[baseIdx + 3] << 2
                    | bitmap[baseIdx + 4] << 1
                    | bitmap[baseIdx + 5];
            sb.append(URL_SAFE_ENCODE_TABLE[idx]);
        }
        return sb.toString();
    }

    private static void int2bytes(int val, byte[] arr, int st, int ed) {
        int len = ed - st;
        int value = val;
        for (int i = 1; i <= len; i++) {
            arr[ed - i] = (byte) (value & 0x1);
            value >>= 1;
        }
    }

    public static long toLongId(String id) {
        if (id != null && id.length() == 8) {
            byte[] bytes = id.getBytes();
            if (bytes.length == 8) {
                return ((((long) bytes[0] & 0xff) << 56)
                        | (((long) bytes[1] & 0xff) << 48)
                        | (((long) bytes[2] & 0xff) << 40)
                        | (((long) bytes[3] & 0xff) << 32)
                        | (((long) bytes[4] & 0xff) << 24)
                        | (((long) bytes[5] & 0xff) << 16)
                        | (((long) bytes[6] & 0xff) << 8)
                        | (((long) bytes[7] & 0xff) << 0));
            }
        }
        return 0;
    }

    public static String fromLong(long id) {
        StringBuilder sb = new StringBuilder();
        sb.append((char)(id >> 56 & 0xff));
        sb.append((char)(id >> 48 & 0xff));
        sb.append((char)(id >> 40 & 0xff));
        sb.append((char)(id >> 32 & 0xff));
        sb.append((char)(id >> 24 & 0xff));
        sb.append((char)(id >> 16 & 0xff));
        sb.append((char)(id >> 8 & 0xff));
        sb.append((char)(id & 0xff));
        return sb.toString();
    }

    public static Set<String> batchGenerate(IDTypeEnum idType, int size) {
        Set<String> results = new HashSet<>();
        if (size <= 0) {
            return results;
        }
        while (results.size() != size) {
            results.add(IDGenerator.generate(idType));
        }
        return results;
    }
    
}

 调用:

public static void main(String[] args) {
        String id = IDGenerator.generate(IDTypeEnum.USER_ID);
        System.out.println(id);
    }

 

public enum IDTypeEnum {
    USER_ID(1),
            ;

    private final int code;

    IDTypeEnum(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }

}

 

标签:int,生成器,id,实用,public,static,0xff,sb,ID
来源: https://www.cnblogs.com/len0031/p/15966045.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有