Class FixedWorkerIdAssigner

java.lang.Object
cloud.opencode.base.id.snowflake.FixedWorkerIdAssigner
All Implemented Interfaces:
WorkerIdAssigner

public final class FixedWorkerIdAssigner extends Object implements WorkerIdAssigner
Fixed Worker ID Assigner - Explicitly configured, deterministic worker and datacenter IDs 固定工作节点ID分配器 - 显式配置的确定性工作节点和数据中心ID

Provides statically configured worker and datacenter IDs for the Snowflake generator. Prefer this class over RandomAssigner.of(long, long) when you need explicit, deterministic ID assignment — the class name clearly communicates its intent.

为雪花ID生成器提供静态配置的工作节点和数据中心ID。 当需要显式、确定性的ID分配时,优先使用此类而非RandomAssigner.of(long, long)—— 类名清晰地传达了意图。

Features | 主要功能:

  • Explicit, deterministic assignment - 显式、确定性分配
  • Suitable for static/on-premise deployments - 适用于静态/本地部署
  • Clear naming vs. RandomAssigner.of() - 比RandomAssigner.of()命名更清晰

Usage Examples | 使用示例:

// Node 3 in datacenter 1
WorkerIdAssigner assigner = FixedWorkerIdAssigner.of(3, 1);

SnowflakeGenerator gen = SnowflakeGenerator.builder()
    .workerIdAssigner(assigner)
    .build();

long id = gen.generate();

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-id V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the configured datacenter ID 返回配置的数据中心ID
    long
    Returns the configured worker ID 返回配置的工作节点ID
    Returns a descriptive strategy name including the configured IDs 返回包含已配置ID的描述性策略名称
    of(long workerId, long datacenterId)
    Creates a FixedWorkerIdAssigner with specified worker and datacenter IDs 创建具有指定工作节点和数据中心ID的FixedWorkerIdAssigner

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • of

      public static FixedWorkerIdAssigner of(long workerId, long datacenterId)
      Creates a FixedWorkerIdAssigner with specified worker and datacenter IDs 创建具有指定工作节点和数据中心ID的FixedWorkerIdAssigner

      Examples | 示例:

      FixedWorkerIdAssigner.of(0, 0)   // both default to 0
      FixedWorkerIdAssigner.of(7, 2)   // worker=7, datacenter=2
      
      Parameters:
      workerId - the worker node ID (0-31 for default bit config) | 工作节点ID(默认位配置下0-31)
      datacenterId - the datacenter ID (0-31 for default bit config) | 数据中心ID(默认位配置下0-31)
      Returns:
      assigner instance | 分配器实例
      Throws:
      IllegalArgumentException - if either ID is negative | 任一ID为负数时抛出
    • assignWorkerId

      public long assignWorkerId()
      Returns the configured worker ID 返回配置的工作节点ID
      Specified by:
      assignWorkerId in interface WorkerIdAssigner
      Returns:
      worker ID | 工作节点ID
    • assignDatacenterId

      public long assignDatacenterId()
      Returns the configured datacenter ID 返回配置的数据中心ID
      Specified by:
      assignDatacenterId in interface WorkerIdAssigner
      Returns:
      datacenter ID | 数据中心ID
    • getStrategyName

      public String getStrategyName()
      Returns a descriptive strategy name including the configured IDs 返回包含已配置ID的描述性策略名称
      Specified by:
      getStrategyName in interface WorkerIdAssigner
      Returns:
      strategy name | 策略名称