Class Extend
java.lang.Object
cloud.opencode.base.id.snowflake.Extend
- All Implemented Interfaces:
ClockBackwardStrategy
Extension Strategy for Clock Backward Handling
时钟回拨扩展策略
Uses extension bits to handle clock backward by incrementing an extension value while keeping the last timestamp. This allows continued ID generation without waiting.
使用扩展位处理时钟回拨,通过递增扩展值并保持上次时间戳来实现。 这允许在不等待的情况下继续生成ID。
Features | 主要功能:
- Non-blocking backward handling - 非阻塞回拨处理
- Configurable extension bits - 可配置扩展位数
- Fail-fast when extension exhausted - 扩展耗尽时快速失败
Usage Examples | 使用示例:
// 2 bits = up to 4 backward events
Extend extend = new Extend(2);
SnowflakeGenerator gen = SnowflakeBuilder.create()
.clockBackwardStrategy(new Extend(2))
.build();
Security | 安全性:
- Thread-safe: Yes (uses AtomicInteger) - 线程安全: 是(使用AtomicInteger)
- Since:
- JDK 25, opencode-base-id V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface ClockBackwardStrategy
ClockBackwardStrategy.Custom -
Constructor Summary
ConstructorsConstructorDescriptionExtend(int extensionBits) Creates an extension strategy with specified bits 使用指定位数创建扩展策略 -
Method Summary
Modifier and TypeMethodDescriptionintGets the number of extension bits 获取扩展位数intGets the current extension value 获取当前扩展值longhandle(long lastTimestamp, long currentTimestamp) Handles clock backward movement 处理时钟回拨intGets the maximum extension value 获取最大扩展值voidreset()Resets the extension value 重置扩展值toString()
-
Constructor Details
-
Extend
public Extend(int extensionBits) Creates an extension strategy with specified bits 使用指定位数创建扩展策略- Parameters:
extensionBits- the number of extension bits | 扩展位数
-
-
Method Details
-
extensionBits
public int extensionBits()Gets the number of extension bits 获取扩展位数- Returns:
- extension bits | 扩展位数
-
extensionValue
public int extensionValue()Gets the current extension value 获取当前扩展值- Returns:
- extension value | 扩展值
-
maxExtension
public int maxExtension()Gets the maximum extension value 获取最大扩展值- Returns:
- maximum extension value | 最大扩展值
-
reset
public void reset()Resets the extension value 重置扩展值 -
handle
public long handle(long lastTimestamp, long currentTimestamp) Description copied from interface:ClockBackwardStrategyHandles clock backward movement 处理时钟回拨- Specified by:
handlein interfaceClockBackwardStrategy- Parameters:
lastTimestamp- the last timestamp used | 上次使用的时间戳currentTimestamp- the current (backward) timestamp | 当前(回拨的)时间戳- Returns:
- the timestamp to use | 要使用的时间戳
-
toString
-