Class SnowflakeBuilder

java.lang.Object
cloud.opencode.base.id.snowflake.SnowflakeBuilder

public final class SnowflakeBuilder extends Object
Snowflake ID Generator Builder 雪花ID生成器构建器

Fluent builder for creating customized SnowflakeGenerator instances.

用于创建自定义SnowflakeGenerator实例的流式构建器。

Features | 主要功能:

  • Worker/Datacenter ID configuration - 工作节点/数据中心ID配置
  • Custom epoch setting - 自定义起始时间设置
  • Bit allocation customization - 位分配自定义
  • Clock backward strategy selection - 时钟回拨策略选择

Usage Examples | 使用示例:

SnowflakeGenerator gen = new SnowflakeBuilder()
    .workerId(1)
    .datacenterId(1)
    .epochMillis(1609459200000L)
    .clockBackwardStrategy(Wait.ofSeconds(5))
    .build();

Security | 安全性:

  • Thread-safe: No (use before build) - 线程安全: 否(在build前使用)

Performance | 性能特性:

  • Time complexity: O(1) for build() and all setter methods - 时间复杂度: build() 及所有 setter 方法均为 O(1)
  • Space complexity: O(1) - stores only primitive configuration values - 空间复杂度: O(1) - 仅存储原始配置值
Since:
JDK 25, opencode-base-id V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • SnowflakeBuilder

      public SnowflakeBuilder()
      Creates a new builder 创建新的构建器
  • Method Details

    • workerId

      public SnowflakeBuilder workerId(long workerId)
      Sets the worker ID 设置工作节点ID
      Parameters:
      workerId - the worker ID (0-31 default) | 工作节点ID(默认0-31)
      Returns:
      this builder | 此构建器
    • datacenterId

      public SnowflakeBuilder datacenterId(long datacenterId)
      Sets the datacenter ID 设置数据中心ID
      Parameters:
      datacenterId - the datacenter ID (0-31 default) | 数据中心ID(默认0-31)
      Returns:
      this builder | 此构建器
    • epoch

      public SnowflakeBuilder epoch(Instant epoch)
      Sets the epoch from Instant 使用Instant设置起始时间
      Parameters:
      epoch - the epoch instant | 起始时间
      Returns:
      this builder | 此构建器
    • epochMillis

      public SnowflakeBuilder epochMillis(long epochMillis)
      Sets the epoch in milliseconds 使用毫秒设置起始时间
      Parameters:
      epochMillis - the epoch in milliseconds | 起始时间(毫秒)
      Returns:
      this builder | 此构建器
    • clockBackwardStrategy

      public SnowflakeBuilder clockBackwardStrategy(ClockBackwardStrategy strategy)
      Sets the clock backward strategy 设置时钟回拨策略
      Parameters:
      strategy - the strategy | 策略
      Returns:
      this builder | 此构建器
    • timestampBits

      public SnowflakeBuilder timestampBits(int bits)
      Sets the timestamp bits 设置时间戳位数
      Parameters:
      bits - the number of bits | 位数
      Returns:
      this builder | 此构建器
    • datacenterBits

      public SnowflakeBuilder datacenterBits(int bits)
      Sets the datacenter ID bits 设置数据中心ID位数
      Parameters:
      bits - the number of bits | 位数
      Returns:
      this builder | 此构建器
    • workerBits

      public SnowflakeBuilder workerBits(int bits)
      Sets the worker ID bits 设置工作节点ID位数
      Parameters:
      bits - the number of bits | 位数
      Returns:
      this builder | 此构建器
    • sequenceBits

      public SnowflakeBuilder sequenceBits(int bits)
      Sets the sequence bits 设置序列号位数
      Parameters:
      bits - the number of bits | 位数
      Returns:
      this builder | 此构建器
    • workerIdAssigner

      public SnowflakeBuilder workerIdAssigner(WorkerIdAssigner assigner)
      Sets the worker ID assigner for automatic ID assignment 设置工作节点ID分配器以自动分配ID

      When set, workerId and datacenterId will be automatically assigned from this assigner, overriding any manually set values.

      设置后,workerId和datacenterId将从此分配器自动分配, 覆盖任何手动设置的值。

      Parameters:
      assigner - the worker ID assigner | 工作节点ID分配器
      Returns:
      this builder | 此构建器
      See Also:
    • build

      public SnowflakeGenerator build()
      Builds the SnowflakeGenerator 构建SnowflakeGenerator
      Returns:
      the generator | 生成器
      Throws:
      IllegalArgumentException - if configuration is invalid | 如果配置无效