refactor: make data centre id bits and worker id as static final

This commit is contained in:
siujamo
2025-07-24 16:51:18 +08:00
parent 88c529e263
commit 1166c6774a
@@ -29,9 +29,9 @@ import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
/** /**
* The {@code SnowflakeIdentityGenerator} generates unique identifiers using the Snowflake algorithm, * The {@code SnowflakeIdentityGenerator} generates unique identifiers using the
* which combines a timestamp, worker ID, and data centre ID to create 64-bit long integers. The bit * Snowflake algorithm, which combines a timestamp, worker ID, and data centre ID to create 64-bit
* distribution for the generated IDs is as follows: * long integers. The bit distribution for the generated IDs is as follows:
* <ul> * <ul>
* <li>1 bit for sign</li> * <li>1 bit for sign</li>
* <li>41 bits for timestamp (in milliseconds)</li> * <li>41 bits for timestamp (in milliseconds)</li>
@@ -40,7 +40,7 @@ import java.time.ZoneId;
* <li>12 bits for sequence number (per millisecond)</li> * <li>12 bits for sequence number (per millisecond)</li>
* </ul> * </ul>
* <p> * <p>
* 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 * 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 * 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 * same millisecond. If the system clock moves backward, an exception is thrown to prevent
@@ -63,15 +63,15 @@ public final class SnowflakeIdentityGenerator implements IdentityGenerator<Long>
*/ */
private final long startEpoch; 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. * 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. * The worker ID assigned to this generator.
@@ -118,13 +118,13 @@ public final class SnowflakeIdentityGenerator implements IdentityGenerator<Long>
throw new IllegalArgumentException("Start Epoch can not be greater than current timestamp!"); 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) { if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("Worker Id can't be greater than %d or less than 0", throw new IllegalArgumentException(String.format("Worker Id can't be greater than %d or less than 0",
maxWorkerId)); maxWorkerId));
} }
var maxDataCentreId = ~(-1L << dataCentreIdBits); var maxDataCentreId = ~(-1L << DATA_CENTRE_ID_BITS);
if (dataCentreId > maxDataCentreId || dataCentreId < 0) { if (dataCentreId > maxDataCentreId || dataCentreId < 0) {
throw new IllegalArgumentException(String.format("Data Centre Id can't be greater than %d or less than 0", throw new IllegalArgumentException(String.format("Data Centre Id can't be greater than %d or less than 0",
maxDataCentreId)); maxDataCentreId));
@@ -172,8 +172,8 @@ public final class SnowflakeIdentityGenerator implements IdentityGenerator<Long>
lastTimestamp = timestamp; lastTimestamp = timestamp;
// shifted and put together by or operations to form a 64-bit ID // shifted and put together by or operations to form a 64-bit ID
var timestampLeftShift = sequenceBits + workerIdBits + dataCentreIdBits; var timestampLeftShift = sequenceBits + WORKER_ID_BITS + DATA_CENTRE_ID_BITS;
var dataCentreIdShift = sequenceBits + workerIdBits; var dataCentreIdShift = sequenceBits + WORKER_ID_BITS;
return ((timestamp - startEpoch) << timestampLeftShift) return ((timestamp - startEpoch) << timestampLeftShift)
| (dataCentreId << dataCentreIdShift) | (dataCentreId << dataCentreIdShift)
| (workerId << sequenceBits) | (workerId << sequenceBits)