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

im.shs.tick.sequence.builder.SnowflakeSeqBuilder Maven / Gradle / Ivy

package im.shs.tick.sequence.builder;

import im.shs.tick.sequence.seq.Sequence;
import im.shs.tick.sequence.seq.impl.SnowflakeSequence;

/**
 * 基于雪花算法,序列号生成器构建者
 */
public class SnowflakeSeqBuilder implements SeqBuilder {

    /**
     * 数据中心ID,值的范围在[0,31]之间,一般可以设置机房的IDC[必选]
     */
    private long datacenterId;
    /**
     * 工作机器ID,值的范围在[0,31]之间,一般可以设置机器编号[必选]
     */
    private long workerId;

    public static SnowflakeSeqBuilder create() {
        SnowflakeSeqBuilder builder = new SnowflakeSeqBuilder();
        return builder;
    }

    @Override
    public Sequence build() {
        SnowflakeSequence sequence = new SnowflakeSequence();
        sequence.setDatacenterId(this.datacenterId);
        sequence.setWorkerId(this.workerId);
        sequence.setClock(true);
        return sequence;
    }

    public SnowflakeSeqBuilder datacenterId(long datacenterId) {
        this.datacenterId = datacenterId;
        return this;
    }

    public SnowflakeSeqBuilder workerId(long workerId) {
        this.workerId = workerId;
        return this;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy