1
0
mirror of synced 2025-12-15 09:38:11 +08:00

修改约束条件

This commit is contained in:
zhouzj
2021-03-14 12:38:12 +08:00
parent cac6ede6b7
commit 6ae25ac33a
8 changed files with 23 additions and 31 deletions

View File

@@ -25,12 +25,13 @@ public class IdGeneratorOptions {
/**
* 机器码,必须由外部系统设置
* 与 WorkerIdBitLength 有关系
* short类型最大值32766如果有更高要求请修改数据类型或联系作者)
*/
public short WorkerId = 0;
/**
* 机器码位长
* 范围:2-21要求序列数位长+机器码位长不超过22
* 范围:1-21要求序列数位长+机器码位长不超过22
* 建议范围6-12。
*/
public byte WorkerIdBitLength = 6;
@@ -50,7 +51,7 @@ public class IdGeneratorOptions {
/**
* 最小序列数(含)
* 默认5不小于1不大于MaxSeqNumber-1
* 默认5不小于1不大于MaxSeqNumber
*/
public short MinSeqNumber = 5;

View File

@@ -23,24 +23,21 @@ public class SnowWorkerM1 implements ISnowWorker {
/**
* 机器码位长
* (机器码+序列数<=22位
*/
protected final byte WorkerIdBitLength;
/**
* 自增序列数位长
* (机器码+序列数<=22位
*/
protected final byte SeqBitLength;
/**
* 最大序列数(含此值
* 超过最大值就会从MinSeqNumber开始
* 最大序列数(含)
*/
protected final int MaxSeqNumber;
/**
* 最小序列数(含此值
* 最小序列数(含)
*/
protected final short MinSeqNumber;
@@ -64,7 +61,7 @@ public class SnowWorkerM1 implements ISnowWorker {
WorkerId = options.WorkerId;
WorkerIdBitLength = options.WorkerIdBitLength == 0 ? 6 : options.WorkerIdBitLength;
SeqBitLength = options.SeqBitLength == 0 ? 6 : options.SeqBitLength;
MaxSeqNumber = options.MaxSeqNumber > 0 ? options.MaxSeqNumber : (int) Math.pow(2, SeqBitLength);
MaxSeqNumber = options.MaxSeqNumber > 0 ? options.MaxSeqNumber : (int) Math.pow(2, SeqBitLength) - 1;
MinSeqNumber = options.MinSeqNumber;
TopOverCostCount = options.TopOverCostCount;
BaseTime = options.BaseTime != 0 ? options.BaseTime : 1582136402000L;

View File

@@ -30,7 +30,7 @@ public class DefaultIdGenerator implements IIdGenerator {
}
double maxWorkerIdNumber = Math.pow(2, options.WorkerIdBitLength) - 1;
if (options.WorkerId < 1 || options.WorkerId > maxWorkerIdNumber) {
if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber) {
throw new IdGeneratorException("WorkerId error. (range:[1, " + maxWorkerIdNumber + "]");
}
@@ -43,9 +43,9 @@ public class DefaultIdGenerator implements IIdGenerator {
throw new IdGeneratorException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]");
}
double maxValue = maxSeqNumber - 2;
if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxValue) {
throw new IdGeneratorException("MinSeqNumber error. (range:[5, " + maxValue + "]");
double maxValue = maxSeqNumber;
if (options.MinSeqNumber < 1 || options.MinSeqNumber > maxValue) {
throw new IdGeneratorException("MinSeqNumber error. (range:[1, " + maxValue + "]");
}
switch (options.Method) {