Class TsidParser
java.lang.Object
cloud.opencode.base.id.tsid.TsidParser
- All Implemented Interfaces:
IdParser<String, TsidParser.ParsedTsid>
TSID Parser
TSID解析器
Parses TSID values and strings to extract timestamp and random components.
解析TSID值和字符串以提取时间戳和随机组件。
Features | 主要功能:
- Extract timestamp - 提取时间戳
- Extract node ID (if configured) - 提取节点ID(如已配置)
- Extract counter/random bits - 提取计数器/随机位
- Validate format - 验证格式
- Parse both Long and String formats - 解析Long和String两种格式
Usage Examples | 使用示例:
TsidParser parser = TsidParser.create();
// Parse from string
ParsedTsid parsed = parser.parse("0ARYZ1J8P0X0R");
System.out.println("Time: " + parsed.time());
System.out.println("Random: " + parsed.random());
// Parse from long
ParsedTsid parsed2 = parser.parse(123456789012345L);
// With custom epoch
TsidParser customParser = TsidParser.create(customEpoch);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
Performance | 性能特性:
- Time complexity: O(1) - parse() extracts timestamp (42 bits) and random bits (22 bits) from a fixed-size 64-bit value using bitwise operations - 时间复杂度: O(1) - parse() 通过位运算从固定大小的 64 位值中提取时间戳(42 位)和随机位(22 位)
- Space complexity: O(1) - produces a fixed-size ParsedTsid record - 空间复杂度: O(1) - 生成固定大小的 ParsedTsid 记录
- Since:
- JDK 25, opencode-base-id V1.4.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordParsed TSID Result 解析的TSID结果 -
Method Summary
Modifier and TypeMethodDescriptionstatic TsidParsercreate()Creates a TSID parser with default epoch (2020-01-01) 使用默认起始时间(2020-01-01)创建TSID解析器static TsidParsercreate(long epoch) Creates a TSID parser with custom epoch 使用自定义起始时间创建TSID解析器extractTimestamp(long tsid) Extracts the timestamp from a TSID long value 从TSID长整型值提取时间戳extractTimestamp(String tsidStr) Extracts the timestamp from an ID 从ID中提取时间戳longgetEpoch()Gets the epoch used by this parser 获取此解析器使用的起始时间booleanisValid(long tsid) Validates a TSID long value 验证TSID长整型值booleanValidates the ID format 验证ID格式parse(long tsid) Parses a TSID from long value 从长整型值解析TSIDParses an ID to extract embedded information 解析ID以提取嵌入信息
-
Method Details
-
create
Creates a TSID parser with default epoch (2020-01-01) 使用默认起始时间(2020-01-01)创建TSID解析器- Returns:
- parser | 解析器
-
create
Creates a TSID parser with custom epoch 使用自定义起始时间创建TSID解析器- Parameters:
epoch- the custom epoch in milliseconds | 自定义起始时间(毫秒)- Returns:
- parser | 解析器
-
parse
Description copied from interface:IdParserParses an ID to extract embedded information 解析ID以提取嵌入信息- Specified by:
parsein interfaceIdParser<String, TsidParser.ParsedTsid>- Parameters:
tsidStr- the ID to parse | 要解析的ID- Returns:
- parse result | 解析结果
-
parse
Parses a TSID from long value 从长整型值解析TSID- Parameters:
tsid- the TSID value | TSID值- Returns:
- parsed result | 解析结果
- Throws:
IllegalArgumentException- if the TSID is negative
-
extractTimestamp
Description copied from interface:IdParserExtracts the timestamp from an ID 从ID中提取时间戳- Specified by:
extractTimestampin interfaceIdParser<String, TsidParser.ParsedTsid>- Parameters:
tsidStr- the ID | ID- Returns:
- timestamp | 时间戳
-
extractTimestamp
Extracts the timestamp from a TSID long value 从TSID长整型值提取时间戳- Parameters:
tsid- the TSID value | TSID值- Returns:
- the timestamp instant | 时间戳
-
isValid
-
isValid
public boolean isValid(long tsid) Validates a TSID long value 验证TSID长整型值- Parameters:
tsid- the TSID value | TSID值- Returns:
- true if valid (non-negative) | 如果有效(非负)返回true
-
getEpoch
public long getEpoch()Gets the epoch used by this parser 获取此解析器使用的起始时间- Returns:
- epoch in milliseconds | 起始时间(毫秒)
-