Class TsidParser

java.lang.Object
cloud.opencode.base.id.tsid.TsidParser
All Implemented Interfaces:
IdParser<String, TsidParser.ParsedTsid>

public final class TsidParser extends Object implements 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 Classes
    Modifier and Type
    Class
    Description
    static final record 
    Parsed TSID Result 解析的TSID结果
  • Method Summary

    Modifier and Type
    Method
    Description
    static TsidParser
    Creates a TSID parser with default epoch (2020-01-01) 使用默认起始时间(2020-01-01)创建TSID解析器
    static TsidParser
    create(long epoch)
    Creates a TSID parser with custom epoch 使用自定义起始时间创建TSID解析器
    extractTimestamp(long tsid)
    Extracts the timestamp from a TSID long value 从TSID长整型值提取时间戳
    Extracts the timestamp from an ID 从ID中提取时间戳
    long
    Gets the epoch used by this parser 获取此解析器使用的起始时间
    boolean
    isValid(long tsid)
    Validates a TSID long value 验证TSID长整型值
    boolean
    isValid(String tsidStr)
    Validates the ID format 验证ID格式
    parse(long tsid)
    Parses a TSID from long value 从长整型值解析TSID
    parse(String tsidStr)
    Parses an ID to extract embedded information 解析ID以提取嵌入信息

    Methods inherited from class Object

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

    • create

      public static TsidParser create()
      Creates a TSID parser with default epoch (2020-01-01) 使用默认起始时间(2020-01-01)创建TSID解析器
      Returns:
      parser | 解析器
    • create

      public static TsidParser create(long epoch)
      Creates a TSID parser with custom epoch 使用自定义起始时间创建TSID解析器
      Parameters:
      epoch - the custom epoch in milliseconds | 自定义起始时间(毫秒)
      Returns:
      parser | 解析器
    • parse

      public TsidParser.ParsedTsid parse(String tsidStr)
      Description copied from interface: IdParser
      Parses an ID to extract embedded information 解析ID以提取嵌入信息
      Specified by:
      parse in interface IdParser<String, TsidParser.ParsedTsid>
      Parameters:
      tsidStr - the ID to parse | 要解析的ID
      Returns:
      parse result | 解析结果
    • parse

      public TsidParser.ParsedTsid parse(long tsid)
      Parses a TSID from long value 从长整型值解析TSID
      Parameters:
      tsid - the TSID value | TSID值
      Returns:
      parsed result | 解析结果
      Throws:
      IllegalArgumentException - if the TSID is negative
    • extractTimestamp

      public Instant extractTimestamp(String tsidStr)
      Description copied from interface: IdParser
      Extracts the timestamp from an ID 从ID中提取时间戳
      Specified by:
      extractTimestamp in interface IdParser<String, TsidParser.ParsedTsid>
      Parameters:
      tsidStr - the ID | ID
      Returns:
      timestamp | 时间戳
    • extractTimestamp

      public Instant extractTimestamp(long tsid)
      Extracts the timestamp from a TSID long value 从TSID长整型值提取时间戳
      Parameters:
      tsid - the TSID value | TSID值
      Returns:
      the timestamp instant | 时间戳
    • isValid

      public boolean isValid(String tsidStr)
      Description copied from interface: IdParser
      Validates the ID format 验证ID格式
      Specified by:
      isValid in interface IdParser<String, TsidParser.ParsedTsid>
      Parameters:
      tsidStr - the ID to validate | 要验证的ID
      Returns:
      true if valid | 如果有效返回true
    • 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 | 起始时间(毫秒)