mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
!1096 Snowflake增加方法:根据传入时间戳,计算ID起终点 (gitee/issues/I60M14)
* Snowflake add method:根据传入时间戳-计算ID起终点 (gitee/issues/I60M14)
This commit is contained in:
@@ -4,6 +4,7 @@ import cn.hutool.core.collection.ConcurrentHashSet;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
@@ -19,6 +20,54 @@ import java.util.Set;
|
||||
*/
|
||||
public class SnowflakeTest {
|
||||
|
||||
/**
|
||||
* 测试-根据传入时间戳-计算ID起终点
|
||||
*/
|
||||
@Test
|
||||
public void snowflakeTestGetIdScope() {
|
||||
long workerId = RandomUtil.randomLong(31);
|
||||
long dataCenterId = RandomUtil.randomLong(31);
|
||||
Snowflake idWorker = new Snowflake(workerId, dataCenterId);
|
||||
long generatedId = idWorker.nextId();
|
||||
// 随机忽略数据中心和工作机器的占位
|
||||
boolean ignore = RandomUtil.randomBoolean();
|
||||
long createTimestamp = idWorker.getGenerateDateTime(generatedId);
|
||||
Pair<Long, Long> idScope = idWorker.getIdScopeByTimestamp(createTimestamp, createTimestamp, ignore);
|
||||
long startId = idScope.getKey();
|
||||
long endId = idScope.getValue();
|
||||
|
||||
System.out.println(longToBinaryReadable(generatedId) + " = generatedId longToBinaryReadable");
|
||||
System.out.println(longToBinaryReadable(startId) + " = startId longToBinaryReadable");
|
||||
System.out.println(longToBinaryReadable(endId) + " = endId longToBinaryReadable");
|
||||
// 起点终点相差比较
|
||||
long trueOffSet = endId - startId;
|
||||
// 忽略数据中心和工作机器时差值为22个1,否则为12个1
|
||||
long expectedOffSet = ignore ? ~(-1 << 22) : ~(-1 << 12);
|
||||
System.out.println("计算差值 = " + trueOffSet + ", 预期差值 = " + expectedOffSet);
|
||||
Assert.assertEquals(trueOffSet, expectedOffSet);
|
||||
}
|
||||
|
||||
/**
|
||||
* long转雪花格式的2进制字符串
|
||||
*
|
||||
* @param number long值
|
||||
* @return 符号位(1bit)- 时间戳相对值(41bit)- 数据中心标志(5bit)- 机器标志(5bit)- 递增序号(12bit)
|
||||
*/
|
||||
private String longToBinaryReadable(long number) {
|
||||
String binaryString = Long.toBinaryString(number);
|
||||
StringBuilder sb = new StringBuilder(binaryString);
|
||||
while (sb.length() < 64) {
|
||||
sb.insert(0, '0'); // 在二进制字符串前面补零
|
||||
}
|
||||
sb
|
||||
.insert(52, "-")
|
||||
.insert(47, "-")
|
||||
.insert(42, "-")
|
||||
.insert(1, "-")
|
||||
;
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snowflakeTest1(){
|
||||
//构建Snowflake,提供终端ID和数据中心ID
|
||||
|
Reference in New Issue
Block a user