From 3537721a2e14d27690021c49933bb7ba385840e4 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 19 Jun 2023 18:10:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=20IdGenerator=20=E7=BB=B4=E6=8A=A4=E7=9A=84?= =?UTF-8?q?=20SnowflakeIdGenerator=20=E5=AE=9E=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/IdGenerator.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/IdGenerator.java b/src/main/java/xyz/zhouxy/plusone/commons/util/IdGenerator.java index 5f88b36..e40a988 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/IdGenerator.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/IdGenerator.java @@ -31,11 +31,22 @@ public class IdGenerator { digits(uuid.getLeastSignificantBits(), 12)); } + /** Returns val represented by the specified number of hex digits. */ + private static String digits(long val, int digits) { + long hi = 1L << (digits * 4); + return Long.toHexString(hi | (val & (hi - 1))).substring(1); + } + // ===== SnowflakeId ===== private static final Table snowflakePool = HashBasedTable.create(); public static long nextSnowflakeId(long workerId, long datacenterId) { + SnowflakeIdGenerator generator = getSnowflakeIdGenerator(workerId, datacenterId); + return generator.nextId(); + } + + public static SnowflakeIdGenerator getSnowflakeIdGenerator(long workerId, long datacenterId) { SnowflakeIdGenerator generator = snowflakePool.get(workerId, datacenterId); if (generator == null) { synchronized (IdGenerator.class) { @@ -46,13 +57,7 @@ public class IdGenerator { } } } - return generator.nextId(); - } - - /** Returns val represented by the specified number of hex digits. */ - private static String digits(long val, int digits) { - long hi = 1L << (digits * 4); - return Long.toHexString(hi | (val & (hi - 1))).substring(1); + return generator; } private IdGenerator() {