Class SnowflakeGenerator

java.lang.Object
cloud.opencode.base.id.snowflake.SnowflakeGenerator
All Implemented Interfaces:
IdGenerator<Long>

public final class SnowflakeGenerator extends Object implements IdGenerator<Long>
Snowflake ID Generator 雪花ID生成器

Generates 64-bit unique IDs with the following structure:

生成具有以下结构的64位唯一ID:

  • 1 bit - sign (always 0) | 符号位(始终为0)
  • 41 bits - timestamp (69 years) | 时间戳(约69年)
  • 5 bits - datacenter ID (0-31) | 数据中心ID(0-31)
  • 5 bits - worker ID (0-31) | 工作节点ID(0-31)
  • 12 bits - sequence (0-4095/ms) | 序列号(每毫秒0-4095)

Features | 主要功能:

  • Time-ordered IDs - 时间有序ID
  • High throughput (4096/ms) - 高吞吐量(每毫秒4096个)
  • Clock backward handling - 时钟回拨处理
  • Configurable bit allocation - 可配置位分配

Usage Examples | 使用示例:

// Default generator
SnowflakeGenerator gen = SnowflakeGenerator.create();
long id = gen.generate();

// Custom worker/datacenter
SnowflakeGenerator gen2 = SnowflakeGenerator.create(1, 1);
long id2 = gen2.generate();

// Using builder
SnowflakeGenerator gen3 = SnowflakeGenerator.builder()
    .workerId(1)
    .datacenterId(1)
    .clockBackwardStrategy(Wait.ofSeconds(5))
    .build();

Security | 安全性:

  • Thread-safe: Yes (ReentrantLock) - 线程安全: 是(可重入锁)
Since:
JDK 25, opencode-base-id V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static SnowflakeGenerator create()
      Creates a default generator 创建默认生成器
      Returns:
      generator | 生成器
    • create

      public static SnowflakeGenerator create(long workerId, long datacenterId)
      Creates a generator with worker and datacenter IDs 使用工作节点ID和数据中心ID创建生成器
      Parameters:
      workerId - the worker node ID (0-31) | 工作节点ID(0-31)
      datacenterId - the datacenter ID (0-31) | 数据中心ID(0-31)
      Returns:
      generator | 生成器
    • builder

      public static SnowflakeBuilder builder()
      Creates a builder for customized generator 创建用于自定义生成器的构建器
      Returns:
      builder | 构建器
    • generate

      public Long generate()
      Description copied from interface: IdGenerator
      Generates the next ID 生成下一个ID
      Specified by:
      generate in interface IdGenerator<Long>
      Returns:
      generated ID | 生成的ID
    • generateStr

      public String generateStr()
      Generates an ID and returns as string 生成ID并返回字符串
      Returns:
      ID string | ID字符串
    • getWorkerId

      public long getWorkerId()
      Gets the worker ID 获取工作节点ID
      Returns:
      worker ID | 工作节点ID
    • getDatacenterId

      public long getDatacenterId()
      Gets the datacenter ID 获取数据中心ID
      Returns:
      datacenter ID | 数据中心ID
    • getEpoch

      public long getEpoch()
      Gets the epoch timestamp 获取起始时间戳
      Returns:
      epoch timestamp | 起始时间戳
    • getConfig

      public SnowflakeConfig getConfig()
      Gets the configuration 获取配置
      Returns:
      configuration | 配置
    • parse

      public SnowflakeIdParser.ParsedId parse(long id)
      Parses a snowflake ID 解析雪花ID
      Parameters:
      id - the ID to parse | 要解析的ID
      Returns:
      parsed result | 解析结果
    • getType

      public String getType()
      Description copied from interface: IdGenerator
      Gets the generator type name 获取生成器类型名称
      Specified by:
      getType in interface IdGenerator<Long>
      Returns:
      type name | 类型名称