Record Class SnowflakeConfig

java.lang.Object
java.lang.Record
cloud.opencode.base.id.snowflake.SnowflakeConfig
Record Components:
workerId - the worker node ID | 工作节点ID
datacenterId - the datacenter ID | 数据中心ID
epoch - 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

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default datacenter bits 默认数据中心位数
    static final long
    Default epoch: 2021-01-01 00:00:00 UTC 默认起始时间: 2021-01-01 00:00:00 UTC
    static final int
    Default sequence bits 默认序列号位数
    static final int
    Default timestamp bits 默认时间戳位数
    static final int
    Default worker bits 默认工作节点位数
  • Constructor Summary

    Constructors
    Constructor
    Description
    SnowflakeConfig(long workerId, long datacenterId, long epoch, int timestampBits, int datacenterBits, int workerBits, int sequenceBits)
    Creates an instance of a SnowflakeConfig record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the value of the datacenterBits record component.
    long
    Returns the value of the datacenterId record component.
    int
    Gets the datacenter ID left shift 获取数据中心ID左移位数
    Creates a default configuration 创建默认配置
    long
    Returns the value of the epoch record component.
    Gets the epoch as Instant 获取起始时间的Instant表示
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    long
    Gets the maximum datacenter ID value 获取最大数据中心ID值
    long
    Gets the maximum sequence value 获取最大序列号值
    long
    Gets the maximum worker ID value 获取最大工作节点ID值
    of(long workerId, long datacenterId)
    Creates a configuration with worker and datacenter IDs 使用工作节点ID和数据中心ID创建配置
    int
    Returns the value of the sequenceBits record component.
    int
    Returns the value of the timestampBits record component.
    int
    Gets the timestamp left shift 获取时间戳左移位数
    final String
    Returns a string representation of this record class.
    void
    Validates this configuration 验证此配置
    int
    Returns the value of the workerBits record component.
    long
    Returns the value of the workerId record component.
    int
    Gets the worker ID left shift 获取工作节点ID左移位数

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • DEFAULT_EPOCH

      public static final long DEFAULT_EPOCH
      Default 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_BITS
      Default timestamp bits 默认时间戳位数
      See Also:
    • DEFAULT_DATACENTER_BITS

      public static final int DEFAULT_DATACENTER_BITS
      Default datacenter bits 默认数据中心位数
      See Also:
    • DEFAULT_WORKER_BITS

      public static final int DEFAULT_WORKER_BITS
      Default worker bits 默认工作节点位数
      See Also:
    • DEFAULT_SEQUENCE_BITS

      public static final int DEFAULT_SEQUENCE_BITS
      Default 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 a SnowflakeConfig record class.
      Parameters:
      workerId - the value for the workerId record component
      datacenterId - the value for the datacenterId record component
      epoch - the value for the epoch record component
      timestampBits - the value for the timestampBits record component
      datacenterBits - the value for the datacenterBits record component
      workerBits - the value for the workerBits record component
      sequenceBits - the value for the sequenceBits record component
  • Method Details

    • defaultConfig

      public static SnowflakeConfig defaultConfig()
      Creates a default configuration 创建默认配置
      Returns:
      default configuration | 默认配置
    • of

      public static SnowflakeConfig of(long workerId, long datacenterId)
      Creates a configuration with worker and datacenter IDs 使用工作节点ID和数据中心ID创建配置
      Parameters:
      workerId - the worker node ID | 工作节点ID
      datacenterId - 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

      public Instant 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

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • workerId

      public long workerId()
      Returns the value of the workerId record component.
      Returns:
      the value of the workerId record component
    • datacenterId

      public long datacenterId()
      Returns the value of the datacenterId record component.
      Returns:
      the value of the datacenterId record component
    • epoch

      public long epoch()
      Returns the value of the epoch record component.
      Returns:
      the value of the epoch record component
    • timestampBits

      public int timestampBits()
      Returns the value of the timestampBits record component.
      Returns:
      the value of the timestampBits record component
    • datacenterBits

      public int datacenterBits()
      Returns the value of the datacenterBits record component.
      Returns:
      the value of the datacenterBits record component
    • workerBits

      public int workerBits()
      Returns the value of the workerBits record component.
      Returns:
      the value of the workerBits record component
    • sequenceBits

      public int sequenceBits()
      Returns the value of the sequenceBits record component.
      Returns:
      the value of the sequenceBits record component