Class SnowflakeIdStateParser

java.lang.Object
me.ahoo.cosid.snowflake.SnowflakeIdStateParser
Direct Known Subclasses:
MillisecondSnowflakeIdStateParser, SecondSnowflakeIdStateParser

@ThreadSafe public abstract class SnowflakeIdStateParser extends Object
Abstract parser for converting between Snowflake IDs and their component state.

This class provides methods to parse a snowflake ID into its components (timestamp, machineId, sequence) and to reconstruct the raw ID from a friendly string format.

The friendly format is: timestamp-machineId-sequence (e.g., "20210623131730192-1-0")

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Delimiter used in friendly ID format.
    protected final long
    Epoch timestamp in milliseconds.
    protected final int
    Number of bits for machine ID portion.
    protected final int
    Number of bits to shift machine ID left.
    protected final long
    Mask for extracting machine ID from raw ID.
    protected final boolean
    Whether to pad numeric fields with leading zeros.
    protected final int
    Number of bits for sequence portion.
    protected final long
    Mask for extracting sequence from raw ID.
    protected final int
    Number of bits for timestamp portion.
    protected final int
    Number of bits to shift timestamp left.
    protected final long
    Mask for extracting timestamp from raw ID.
    protected final ZoneId
    Time zone used for timestamp conversion.
  • Constructor Summary

    Constructors
    Constructor
    Description
    SnowflakeIdStateParser(long epoch, int timestampBit, int machineBit, int sequenceBit)
    Creates a parser with default zone and no padding.
    SnowflakeIdStateParser(long epoch, int timestampBit, int machineBit, int sequenceBit, ZoneId zoneId, boolean padStart)
    Creates a parser with custom zone and padding settings.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract DateTimeFormatter
    Gets the date time formatter for parsing timestamps.
    protected abstract long
    Converts a LocalDateTime to time difference from epoch.
    int
    Gets the maximum character size for machine ID in decimal representation.
    int
    Gets the maximum character size for sequence in decimal representation.
    protected abstract LocalDateTime
    getTimestamp(long diffTime)
    Converts a time difference to a LocalDateTime.
    Gets the time zone used for timestamp conversion.
    boolean
    Checks if numeric fields are padded with leading zeros.
    of(SnowflakeId snowflakeId)
    Creates a parser for the given SnowflakeId instance.
    of(SnowflakeId snowflakeId, ZoneId zoneId, boolean padStart)
    Creates a parser for the given SnowflakeId instance with custom settings.
    parse(long id)
    Parses a raw snowflake ID into SnowflakeIdState.
    parse(String friendlyId)
    Parses a friendly ID string into SnowflakeIdState.
    int
    parseMachineId(long id)
    Extracts the machine ID portion of an ID.
    long
    parseSequence(long id)
    Extracts the sequence portion of an ID.
    parseTimestamp(long id)
    Extracts and parses the timestamp portion of an ID.

    Methods inherited from class java.lang.Object

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

    • DELIMITER

      public static final String DELIMITER
      Delimiter used in friendly ID format.
      See Also:
    • zoneId

      protected final ZoneId zoneId
      Time zone used for timestamp conversion.
    • epoch

      protected final long epoch
      Epoch timestamp in milliseconds.
    • sequenceBit

      protected final int sequenceBit
      Number of bits for sequence portion.
    • sequenceMask

      protected final long sequenceMask
      Mask for extracting sequence from raw ID.
    • machineBit

      protected final int machineBit
      Number of bits for machine ID portion.
    • machineMask

      protected final long machineMask
      Mask for extracting machine ID from raw ID.
    • machineLeft

      protected final int machineLeft
      Number of bits to shift machine ID left.
    • timestampBit

      protected final int timestampBit
      Number of bits for timestamp portion.
    • timestampMask

      protected final long timestampMask
      Mask for extracting timestamp from raw ID.
    • timestampLeft

      protected final int timestampLeft
      Number of bits to shift timestamp left.
    • padStart

      protected final boolean padStart
      Whether to pad numeric fields with leading zeros.
  • Constructor Details

    • SnowflakeIdStateParser

      public SnowflakeIdStateParser(long epoch, int timestampBit, int machineBit, int sequenceBit)
      Creates a parser with default zone and no padding.
      Parameters:
      epoch - epoch timestamp in milliseconds
      timestampBit - number of bits for timestamp
      machineBit - number of bits for machine ID
      sequenceBit - number of bits for sequence
    • SnowflakeIdStateParser

      public SnowflakeIdStateParser(long epoch, int timestampBit, int machineBit, int sequenceBit, ZoneId zoneId, boolean padStart)
      Creates a parser with custom zone and padding settings.
      Parameters:
      epoch - epoch timestamp in milliseconds
      timestampBit - number of bits for timestamp
      machineBit - number of bits for machine ID
      sequenceBit - number of bits for sequence
      zoneId - time zone for timestamp conversion
      padStart - whether to pad numeric fields with leading zeros
  • Method Details

    • getZoneId

      public ZoneId getZoneId()
      Gets the time zone used for timestamp conversion.
      Returns:
      the zone ID
    • isPadStart

      public boolean isPadStart()
      Checks if numeric fields are padded with leading zeros.
      Returns:
      true if padding is enabled
    • getMachineCharSize

      public int getMachineCharSize()
      Gets the maximum character size for machine ID in decimal representation.
      Returns:
      the machine ID character size
    • getSequenceCharSize

      public int getSequenceCharSize()
      Gets the maximum character size for sequence in decimal representation.
      Returns:
      the sequence character size
    • getDateTimeFormatter

      protected abstract DateTimeFormatter getDateTimeFormatter()
      Gets the date time formatter for parsing timestamps.
      Returns:
      the date time formatter
    • getTimestamp

      protected abstract LocalDateTime getTimestamp(long diffTime)
      Converts a time difference to a LocalDateTime.
      Parameters:
      diffTime - time difference from epoch in the appropriate unit
      Returns:
      the corresponding LocalDateTime
    • getDiffTime

      protected abstract long getDiffTime(LocalDateTime timestamp)
      Converts a LocalDateTime to time difference from epoch.
      Parameters:
      timestamp - the LocalDateTime to convert
      Returns:
      time difference from epoch
    • parse

      public SnowflakeIdState parse(String friendlyId)
      Parses a friendly ID string into SnowflakeIdState.

      Expected format: timestamp-machineId-sequence

      Parameters:
      friendlyId - the friendly ID string to parse
      Returns:
      the parsed state
      Throws:
      IllegalArgumentException - if format is invalid
      NullPointerException - if friendlyId is null
    • parse

      public SnowflakeIdState parse(long id)
      Parses a raw snowflake ID into SnowflakeIdState.
      Parameters:
      id - the raw snowflake ID
      Returns:
      the parsed state
    • parseTimestamp

      public LocalDateTime parseTimestamp(long id)
      Extracts and parses the timestamp portion of an ID.
      Parameters:
      id - the raw snowflake ID
      Returns:
      the parsed timestamp as LocalDateTime
    • parseMachineId

      public int parseMachineId(long id)
      Extracts the machine ID portion of an ID.
      Parameters:
      id - the raw snowflake ID
      Returns:
      the machine ID
    • parseSequence

      public long parseSequence(long id)
      Extracts the sequence portion of an ID.
      Parameters:
      id - the raw snowflake ID
      Returns:
      the sequence number
    • of

      public static SnowflakeIdStateParser of(SnowflakeId snowflakeId)
      Creates a parser for the given SnowflakeId instance.
      Parameters:
      snowflakeId - the snowflake ID to create a parser for
      Returns:
      the appropriate parser for the snowflake ID type
    • of

      public static SnowflakeIdStateParser of(SnowflakeId snowflakeId, ZoneId zoneId, boolean padStart)
      Creates a parser for the given SnowflakeId instance with custom settings.
      Parameters:
      snowflakeId - the snowflake ID to create a parser for
      zoneId - time zone for timestamp conversion
      padStart - whether to pad numeric fields
      Returns:
      the appropriate parser for the snowflake ID type