From 1166c6774afdb3393155ca316253f2cb05f0d209 Mon Sep 17 00:00:00 2001 From: siujamo Date: Thu, 24 Jul 2025 16:51:18 +0800 Subject: [PATCH] refactor: make data centre id bits and worker id as static final --- .../impl/SnowflakeIdentityGenerator.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/identity-generator/src/main/java/com/onixbyte/identitygenerator/impl/SnowflakeIdentityGenerator.java b/identity-generator/src/main/java/com/onixbyte/identitygenerator/impl/SnowflakeIdentityGenerator.java index 51bc9ab..4f4c152 100644 --- a/identity-generator/src/main/java/com/onixbyte/identitygenerator/impl/SnowflakeIdentityGenerator.java +++ b/identity-generator/src/main/java/com/onixbyte/identitygenerator/impl/SnowflakeIdentityGenerator.java @@ -29,9 +29,9 @@ import java.time.LocalDateTime; import java.time.ZoneId; /** - * The {@code SnowflakeIdentityGenerator} generates unique identifiers using the Snowflake algorithm, - * which combines a timestamp, worker ID, and data centre ID to create 64-bit long integers. The bit - * distribution for the generated IDs is as follows: + * The {@code SnowflakeIdentityGenerator} generates unique identifiers using the + * Snowflake algorithm, which combines a timestamp, worker ID, and data centre ID to create 64-bit + * long integers. The bit distribution for the generated IDs is as follows: * *

- * When initializing a {@link SnowflakeIdentityGenerator}, you must provide the worker ID and data + * When initialising a {@link SnowflakeIdentityGenerator}, you must provide the worker ID and data * centre ID, ensuring they are within the valid range defined by the bit size. The generator * maintains an internal sequence number that increments for IDs generated within the * same millisecond. If the system clock moves backward, an exception is thrown to prevent @@ -63,15 +63,15 @@ public final class SnowflakeIdentityGenerator implements IdentityGenerator */ private final long startEpoch; - /** - * The number of bits reserved for the worker ID. - */ - private final long workerIdBits = 5L; - /** * The number of bits reserved for the data centre ID. */ - private final long dataCentreIdBits = 5L; + private static final long DATA_CENTRE_ID_BITS = 5L; + + /** + * The number of bits reserved for the worker ID. + */ + private static final long WORKER_ID_BITS = 5L; /** * The worker ID assigned to this generator. @@ -118,13 +118,13 @@ public final class SnowflakeIdentityGenerator implements IdentityGenerator throw new IllegalArgumentException("Start Epoch can not be greater than current timestamp!"); } - var maxWorkerId = ~(-1L << workerIdBits); + var maxWorkerId = ~(-1L << WORKER_ID_BITS); if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("Worker Id can't be greater than %d or less than 0", maxWorkerId)); } - var maxDataCentreId = ~(-1L << dataCentreIdBits); + var maxDataCentreId = ~(-1L << DATA_CENTRE_ID_BITS); if (dataCentreId > maxDataCentreId || dataCentreId < 0) { throw new IllegalArgumentException(String.format("Data Centre Id can't be greater than %d or less than 0", maxDataCentreId)); @@ -172,8 +172,8 @@ public final class SnowflakeIdentityGenerator implements IdentityGenerator lastTimestamp = timestamp; // shifted and put together by or operations to form a 64-bit ID - var timestampLeftShift = sequenceBits + workerIdBits + dataCentreIdBits; - var dataCentreIdShift = sequenceBits + workerIdBits; + var timestampLeftShift = sequenceBits + WORKER_ID_BITS + DATA_CENTRE_ID_BITS; + var dataCentreIdShift = sequenceBits + WORKER_ID_BITS; return ((timestamp - startEpoch) << timestampLeftShift) | (dataCentreId << dataCentreIdShift) | (workerId << sequenceBits)