All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.dengchen2020.id.snowflake.SnowflakeIdGenerator Maven / Gradle / Ivy

There is a newer version: 0.0.28
Show newest version
package io.github.dengchen2020.id.snowflake;

import io.github.dengchen2020.id.IdGenerator;
import io.github.dengchen2020.id.exception.IdGeneratorException;

/**
 * 雪花算法生成器
 * @author dengchen
 * @since 2024/7/1
 */
public class SnowflakeIdGenerator implements IdGenerator {

    private static SnowWorker _SnowWorker = null;

    public SnowflakeIdGenerator(SnowflakeIdGeneratorOptions options) throws IdGeneratorException {
        if (options == null) {
            throw new RuntimeException("雪花算法配置错误");
        }

        // 1.BaseTime
        if (options.getBaseTime() < 315504000000L || options.getBaseTime() > System.currentTimeMillis()) {
            throw new IdGeneratorException("基础时间(BaseTime)配置错误,取值范围:315504000000-"+System.currentTimeMillis());
        }

        // 2.WorkerIdBitLength
        if (options.getWorkerIdBitLength() <= 0) {
            throw new IdGeneratorException("序列数位长(WorkerIdBitLength)配置错误,取值范围:1-21");
        }
        if (options.getWorkerIdBitLength() + options.getSeqBitLength() > 22) {
            throw new IdGeneratorException("序列数位长(WorkerIdBitLength)配置错误,WorkerIdBitLength + SeqBitLength <= 22");
        }

        // 3.WorkerId
        int maxWorkerIdNumber = (1 << options.getWorkerIdBitLength()) - 1;
        if (maxWorkerIdNumber == 0) {
            maxWorkerIdNumber = 63;
        }
        if (options.getWorkerId() < 0 || options.getWorkerId() > maxWorkerIdNumber) {
            throw new IdGeneratorException("机器码(WorkerId)配置错误,取值范围:0-" + (maxWorkerIdNumber > 0 ? maxWorkerIdNumber : 63));
        }

        // 4.SeqBitLength
        if (options.getSeqBitLength() < 2 || options.getSeqBitLength() > 21) {
            throw new IdGeneratorException("序列数位长(SeqBitLength)配置错误,取值范围:2-21");
        }

        // 5.MaxSeqNumber
        int maxSeqNumber = (1 << options.getSeqBitLength()) - 1;
        if (maxSeqNumber == 0) {
            maxSeqNumber = 63;
        }
        if (options.getMaxSeqNumber() < 0 || options.getMaxSeqNumber() > maxSeqNumber) {
            throw new IdGeneratorException("最大序列数(含)(MaxSeqNumber)配置错误,取值范围:1-"+ maxSeqNumber);
        }

        // 6.MinSeqNumber
        if (options.getMinSeqNumber() < 5 || options.getMinSeqNumber() > maxSeqNumber) {
            throw new IdGeneratorException("最小序列数(含)(MinSeqNumber)配置错误,取值范围:5-" + maxSeqNumber);
        }

        switch (options.getMethod()) {
            case 2:
                _SnowWorker = new SnowWorker2(options);
                break;
            case 1:
            default:
                _SnowWorker = new SnowWorker(options);
                break;
        }

    }

    @Override
    public long newLong() {
        return _SnowWorker.nextId();
    }
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy