雪花id学习

这是我根据雪花id的想法自己做的一个雪花id类

写法可能有点糙,有问题请指正

package org.hibernate.entity;

import java.lang.management.ManagementFactory;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Random;

public class SnowFlake {
    //机器码位数
    private static long machineBtis = 5L;
    //数据中心(进程号)位数
    private static long dataCenterBits = 5L;
    //时间戳位数
    private static long timeStampBits = 41L;
    //随机序列位数
    private static long randomSequenceBits = 12L;

    //时间戳左移位数
    private static long timeStampShift = dataCenterBits+randomSequenceBits+machineBtis;
    //机器码id左移位数
    private static long machineShift = dataCenterBits+randomSequenceBits;
    //数据中心左移位数
    private static long dataCenterShift = randomSequenceBits;

    //机器码id
    private static long machineId = 1L;
    //数据中心(进程号)id
    private static long dataCenterId = 1L;
    //时间戳
    private static long timeStampId = -1L;
    //随机序列位数
    private static long randomSequence = 1L;
    //上一次时间戳
    private static long lastTimeStamp = -1L;

    private static SnowFlake snowFlake = new SnowFlake();

    private static String id;


    public static String getNextId() throws Exception {
        id = String.valueOf((snowFlake.getTimeStampId()<<timeStampShift)|(snowFlake.getMachineId()<<machineShift)|(snowFlake.getDataCenterId()<<dataCenterShift)|(snowFlake.getRandomSequenceId()));
        return id;
    }

    private long getRandomSequenceId(){
        Random random = new Random();
        long randomMask = -1L ^ (-1L << randomSequenceBits);
        return random.nextLong()&randomMask;
    }

    private long getDataCenterId(){
        String processId = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
        long processMask = -1L ^ (-1L << dataCenterBits);
        return Long.valueOf(processId)&processMask;
    }

    private long getMachineId() throws SocketException {
        StringBuilder stringBuilder = new StringBuilder();
        Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        while(networkInterfaceEnumeration.hasMoreElements()){
            stringBuilder.append(networkInterfaceEnumeration.nextElement().getName());
        }
        long machineMask = -1L^ (-1L << machineBtis);
        return stringBuilder.toString().hashCode()&machineMask;
    }

    public long getTimeStampId() throws Exception {
        long time = System.currentTimeMillis();
        if(time<lastTimeStamp){
            throw new Exception("clock moved backward");
        }
        lastTimeStamp = time;
        long timeStampMask = -1L^ (-1L << timeStampBits);
        return time&timeStampMask;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原来是肖某人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值