Record Class SnowflakeConfig
java.lang.Object
java.lang.Record
cloud.opencode.base.id.snowflake.SnowflakeConfig
- Record Components:
workerId- the worker node ID | 工作节点IDdatacenterId- the datacenter ID | 数据中心IDepoch- the epoch timestamp in milliseconds | 起始时间戳(毫秒)timestampBits- the number of bits for timestamp | 时间戳位数datacenterBits- the number of bits for datacenter ID | 数据中心ID位数workerBits- the number of bits for worker ID | 工作节点ID位数sequenceBits- the number of bits for sequence | 序列号位数
public record SnowflakeConfig(long workerId, long datacenterId, long epoch, int timestampBits, int datacenterBits, int workerBits, int sequenceBits)
extends Record
Snowflake ID Generator Configuration
雪花ID生成器配置
Configuration record for Snowflake ID generator including bit allocations and epoch settings.
雪花ID生成器的配置记录,包括位分配和起始时间设置。
Default Bit Allocation | 默认位分配:
- Sign bit: 1 - 符号位: 1
- Timestamp bits: 41 (69 years) - 时间戳位: 41 (约69年)
- Datacenter bits: 5 (32 datacenters) - 数据中心位: 5 (32个)
- Worker bits: 5 (32 workers) - 工作节点位: 5 (32个)
- Sequence bits: 12 (4096/ms) - 序列号位: 12 (每毫秒4096个)
Usage Examples | 使用示例:
SnowflakeConfig config = SnowflakeConfig.defaultConfig();
SnowflakeConfig custom = new SnowflakeConfig(1, 1, 1609459200000L, 41, 5, 5, 12);
Features | 主要功能:
- Configuration record for Snowflake ID generator - Snowflake ID生成器的配置记录
- Configurable bit allocation for timestamp, worker, sequence - 可配置时间戳、工作节点、序列的位分配
Security | 安全性:
- Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
- Null-safe: No - 空值安全: 否
- Since:
- JDK 25, opencode-base-id V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault datacenter bits 默认数据中心位数static final longDefault epoch: 2021-01-01 00:00:00 UTC 默认起始时间: 2021-01-01 00:00:00 UTCstatic final intDefault sequence bits 默认序列号位数static final intDefault timestamp bits 默认时间戳位数static final intDefault worker bits 默认工作节点位数 -
Constructor Summary
ConstructorsConstructorDescriptionSnowflakeConfig(long workerId, long datacenterId, long epoch, int timestampBits, int datacenterBits, int workerBits, int sequenceBits) Creates an instance of aSnowflakeConfigrecord class. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the value of thedatacenterBitsrecord component.longReturns the value of thedatacenterIdrecord component.intGets the datacenter ID left shift 获取数据中心ID左移位数static SnowflakeConfigCreates a default configuration 创建默认配置longepoch()Returns the value of theepochrecord component.Gets the epoch as Instant 获取起始时间的Instant表示final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.longGets the maximum datacenter ID value 获取最大数据中心ID值longGets the maximum sequence value 获取最大序列号值longGets the maximum worker ID value 获取最大工作节点ID值static SnowflakeConfigof(long workerId, long datacenterId) Creates a configuration with worker and datacenter IDs 使用工作节点ID和数据中心ID创建配置intReturns the value of thesequenceBitsrecord component.intReturns the value of thetimestampBitsrecord component.intGets the timestamp left shift 获取时间戳左移位数final StringtoString()Returns a string representation of this record class.voidvalidate()Validates this configuration 验证此配置intReturns the value of theworkerBitsrecord component.longworkerId()Returns the value of theworkerIdrecord component.intGets the worker ID left shift 获取工作节点ID左移位数
-
Field Details
-
DEFAULT_EPOCH
public static final long DEFAULT_EPOCHDefault epoch: 2021-01-01 00:00:00 UTC 默认起始时间: 2021-01-01 00:00:00 UTC- See Also:
-
DEFAULT_TIMESTAMP_BITS
public static final int DEFAULT_TIMESTAMP_BITSDefault timestamp bits 默认时间戳位数- See Also:
-
DEFAULT_DATACENTER_BITS
public static final int DEFAULT_DATACENTER_BITSDefault datacenter bits 默认数据中心位数- See Also:
-
DEFAULT_WORKER_BITS
public static final int DEFAULT_WORKER_BITSDefault worker bits 默认工作节点位数- See Also:
-
DEFAULT_SEQUENCE_BITS
public static final int DEFAULT_SEQUENCE_BITSDefault sequence bits 默认序列号位数- See Also:
-
-
Constructor Details
-
SnowflakeConfig
public SnowflakeConfig(long workerId, long datacenterId, long epoch, int timestampBits, int datacenterBits, int workerBits, int sequenceBits) Creates an instance of aSnowflakeConfigrecord class.- Parameters:
workerId- the value for theworkerIdrecord componentdatacenterId- the value for thedatacenterIdrecord componentepoch- the value for theepochrecord componenttimestampBits- the value for thetimestampBitsrecord componentdatacenterBits- the value for thedatacenterBitsrecord componentworkerBits- the value for theworkerBitsrecord componentsequenceBits- the value for thesequenceBitsrecord component
-
-
Method Details
-
defaultConfig
Creates a default configuration 创建默认配置- Returns:
- default configuration | 默认配置
-
of
Creates a configuration with worker and datacenter IDs 使用工作节点ID和数据中心ID创建配置- Parameters:
workerId- the worker node ID | 工作节点IDdatacenterId- the datacenter ID | 数据中心ID- Returns:
- configuration | 配置
-
maxWorkerId
public long maxWorkerId()Gets the maximum worker ID value 获取最大工作节点ID值- Returns:
- maximum worker ID | 最大工作节点ID
-
maxDatacenterId
public long maxDatacenterId()Gets the maximum datacenter ID value 获取最大数据中心ID值- Returns:
- maximum datacenter ID | 最大数据中心ID
-
maxSequence
public long maxSequence()Gets the maximum sequence value 获取最大序列号值- Returns:
- maximum sequence | 最大序列号
-
epochInstant
Gets the epoch as Instant 获取起始时间的Instant表示- Returns:
- epoch instant | 起始时间
-
workerIdShift
public int workerIdShift()Gets the worker ID left shift 获取工作节点ID左移位数- Returns:
- shift bits | 移位数
-
datacenterIdShift
public int datacenterIdShift()Gets the datacenter ID left shift 获取数据中心ID左移位数- Returns:
- shift bits | 移位数
-
timestampShift
public int timestampShift()Gets the timestamp left shift 获取时间戳左移位数- Returns:
- shift bits | 移位数
-
validate
public void validate()Validates this configuration 验证此配置- Throws:
IllegalArgumentException- if configuration is invalid | 如果配置无效则抛出异常
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with thecomparemethod from their corresponding wrapper classes. -
workerId
-
datacenterId
public long datacenterId()Returns the value of thedatacenterIdrecord component.- Returns:
- the value of the
datacenterIdrecord component
-
epoch
-
timestampBits
public int timestampBits()Returns the value of thetimestampBitsrecord component.- Returns:
- the value of the
timestampBitsrecord component
-
datacenterBits
public int datacenterBits()Returns the value of thedatacenterBitsrecord component.- Returns:
- the value of the
datacenterBitsrecord component
-
workerBits
public int workerBits()Returns the value of theworkerBitsrecord component.- Returns:
- the value of the
workerBitsrecord component
-
sequenceBits
public int sequenceBits()Returns the value of thesequenceBitsrecord component.- Returns:
- the value of the
sequenceBitsrecord component
-