/**
* 0 | 0001100 10100010 10111110 10001001 01011100 00 | 10001 | 00001 | 0000 00000000
*
* 0 | 0001100 10100010 10111110 10001001 01011100 00 | 10001 | 00001 | 0000 00000000
* 0 | timestamp |datacenterId| workerId | sequence
* 正数(占位) | 时间戳二进制 | 数据中心ID | 机器ID | 同一机房同一机器相同时间产生的序列
*/
public class SnowflakeIdWorker
{
// 数据中心(机房) id
private long datacenterId;
// 机器ID
private long workerId;
// 同一时间的序列
private long sequence;
/**
* 构造方法
*
* @param workerId 工作ID(机器ID)
* @param datacenterId 数据中心ID(机房ID)
* sequence 从0开始
*/
public SnowflakeIdWorker(long workerId, long datacenterId)
{
this(workerId, datacenterId, 0);
}
/**
* 构造方法
*
* @param workerId 工作ID(机器ID)
* @param datacenterId 数据中心ID(机房ID)
* @param sequence 序列号
*/
public SnowflakeIdWorker(long workerId, long datacenterId, long sequence)
{
// sanity check for workerId and datacenterId
// 机房id和机器id不能超过32,不能小于0
if (workerId > maxWorkerId || workerId < 0)
{
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacente