Class SnowflakeGenerator
java.lang.Object
tfagaming.projects.minecraft.homestead.snowflake.SnowflakeGenerator
Snowflake ID Generator for Homestead models.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a generator with a random worker ID.SnowflakeGenerator(long workerId) Creates a generator with a specific worker ID. -
Method Summary
Modifier and TypeMethodDescriptionstatic longextractSequence(long snowflakeId) Extracts the sequence number from a Snowflake ID.static longextractTimestamp(long snowflakeId) Extracts the timestamp (as epoch millis) from a Snowflake ID.static longextractWorkerId(long snowflakeId) Extracts the worker ID from a Snowflake ID.longReturns the worker ID of this generator instance.longnextId()Generates the next unique Snowflake ID.Generates the next unique Snowflake ID.static InstanttoInstant(long snowflakeId) Converts a Snowflake ID to an Instant.
-
Constructor Details
-
SnowflakeGenerator
public SnowflakeGenerator(long workerId) Creates a generator with a specific worker ID.- Parameters:
workerId- Unique ID for this machine/worker, ranging from 0 to 1023- Throws:
IllegalArgumentException- if workerId is out of range
-
SnowflakeGenerator
public SnowflakeGenerator()Creates a generator with a random worker ID.
-
-
Method Details
-
extractTimestamp
public static long extractTimestamp(long snowflakeId) Extracts the timestamp (as epoch millis) from a Snowflake ID. -
extractWorkerId
public static long extractWorkerId(long snowflakeId) Extracts the worker ID from a Snowflake ID. -
extractSequence
public static long extractSequence(long snowflakeId) Extracts the sequence number from a Snowflake ID. -
toInstant
Converts a Snowflake ID to an Instant. -
nextId
public long nextId()Generates the next unique Snowflake ID.- Returns:
- A unique 64-bit positive long ID
- Throws:
IllegalStateException- if the clock moves backwards
-
nextIdString
Generates the next unique Snowflake ID.- Returns:
- A unique and unsigned 64-bit positive long ID as string
- Throws:
IllegalStateException- if the clock moves backwards
-
getWorkerId
public long getWorkerId()Returns the worker ID of this generator instance.
-