Package me.ahoo.cosid.snowflake
Class SnowflakeIdStateParser
java.lang.Object
me.ahoo.cosid.snowflake.SnowflakeIdStateParser
- Direct Known Subclasses:
MillisecondSnowflakeIdStateParser,SecondSnowflakeIdStateParser
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
FieldsModifier and TypeFieldDescriptionstatic final StringDelimiter used in friendly ID format.protected final longEpoch timestamp in milliseconds.protected final intNumber of bits for machine ID portion.protected final intNumber of bits to shift machine ID left.protected final longMask for extracting machine ID from raw ID.protected final booleanWhether to pad numeric fields with leading zeros.protected final intNumber of bits for sequence portion.protected final longMask for extracting sequence from raw ID.protected final intNumber of bits for timestamp portion.protected final intNumber of bits to shift timestamp left.protected final longMask for extracting timestamp from raw ID.protected final ZoneIdTime zone used for timestamp conversion. -
Constructor Summary
ConstructorsConstructorDescriptionSnowflakeIdStateParser(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 TypeMethodDescriptionprotected abstract DateTimeFormatterGets the date time formatter for parsing timestamps.protected abstract longgetDiffTime(LocalDateTime timestamp) Converts a LocalDateTime to time difference from epoch.intGets the maximum character size for machine ID in decimal representation.intGets the maximum character size for sequence in decimal representation.protected abstract LocalDateTimegetTimestamp(long diffTime) Converts a time difference to a LocalDateTime.Gets the time zone used for timestamp conversion.booleanChecks if numeric fields are padded with leading zeros.static SnowflakeIdStateParserof(SnowflakeId snowflakeId) Creates a parser for the given SnowflakeId instance.static SnowflakeIdStateParserof(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.Parses a friendly ID string into SnowflakeIdState.intparseMachineId(long id) Extracts the machine ID portion of an ID.longparseSequence(long id) Extracts the sequence portion of an ID.parseTimestamp(long id) Extracts and parses the timestamp portion of an ID.
-
Field Details
-
DELIMITER
Delimiter used in friendly ID format.- See Also:
-
zoneId
Time zone used for timestamp conversion. -
epoch
protected final long epochEpoch timestamp in milliseconds. -
sequenceBit
protected final int sequenceBitNumber of bits for sequence portion. -
sequenceMask
protected final long sequenceMaskMask for extracting sequence from raw ID. -
machineBit
protected final int machineBitNumber of bits for machine ID portion. -
machineMask
protected final long machineMaskMask for extracting machine ID from raw ID. -
machineLeft
protected final int machineLeftNumber of bits to shift machine ID left. -
timestampBit
protected final int timestampBitNumber of bits for timestamp portion. -
timestampMask
protected final long timestampMaskMask for extracting timestamp from raw ID. -
timestampLeft
protected final int timestampLeftNumber of bits to shift timestamp left. -
padStart
protected final boolean padStartWhether 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 millisecondstimestampBit- number of bits for timestampmachineBit- number of bits for machine IDsequenceBit- 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 millisecondstimestampBit- number of bits for timestampmachineBit- number of bits for machine IDsequenceBit- number of bits for sequencezoneId- time zone for timestamp conversionpadStart- whether to pad numeric fields with leading zeros
-
-
Method Details
-
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
Gets the date time formatter for parsing timestamps.- Returns:
- the date time formatter
-
getTimestamp
Converts a time difference to a LocalDateTime.- Parameters:
diffTime- time difference from epoch in the appropriate unit- Returns:
- the corresponding LocalDateTime
-
getDiffTime
Converts a LocalDateTime to time difference from epoch.- Parameters:
timestamp- the LocalDateTime to convert- Returns:
- time difference from epoch
-
parse
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 invalidNullPointerException- if friendlyId is null
-
parse
Parses a raw snowflake ID into SnowflakeIdState.- Parameters:
id- the raw snowflake ID- Returns:
- the parsed state
-
parseTimestamp
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
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
Creates a parser for the given SnowflakeId instance with custom settings.- Parameters:
snowflakeId- the snowflake ID to create a parser forzoneId- time zone for timestamp conversionpadStart- whether to pad numeric fields- Returns:
- the appropriate parser for the snowflake ID type
-