Package me.ahoo.cosid.snowflake
Class SafeJavaScriptSnowflakeId
java.lang.Object
me.ahoo.cosid.snowflake.SafeJavaScriptSnowflakeId
Safe JavaScript Snowflake ID generators.
JavaScript Numbers can only safely represent integers up to 2^53 - 1 (Number.MAX_SAFE_INTEGER = 9007199254740991). This class provides factory methods for creating SnowflakeId instances that stay within this limit by reducing total bits to 53 or fewer.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longMaximum safe JavaScript number value.static final intMaximum safe JavaScript number bit count. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisSafeJavaScript(long id) Checks if an ID is safe for JavaScript.static MillisecondSnowflakeIdofMillisecond(int machineId) Creates a safe millisecond SnowflakeId with default safe configuration.static MillisecondSnowflakeIdofMillisecond(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId, long sequenceResetThreshold) Creates a safe millisecond SnowflakeId.static SecondSnowflakeIdofSecond(int machineId) Creates a safe second SnowflakeId with default safe configuration.static SecondSnowflakeIdofSecond(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId, long sequenceResetThreshold) Creates a safe second SnowflakeId.
-
Field Details
-
JAVA_SCRIPT_MAX_SAFE_NUMBER_BIT
public static final int JAVA_SCRIPT_MAX_SAFE_NUMBER_BITMaximum safe JavaScript number bit count.- See Also:
-
JAVA_SCRIPT_MAX_SAFE_NUMBER
public static final long JAVA_SCRIPT_MAX_SAFE_NUMBERMaximum safe JavaScript number value.- See Also:
-
-
Constructor Details
-
SafeJavaScriptSnowflakeId
public SafeJavaScriptSnowflakeId()
-
-
Method Details
-
isSafeJavaScript
public static boolean isSafeJavaScript(long id) Checks if an ID is safe for JavaScript.- Parameters:
id- the ID to check- Returns:
- true if less than MAX_SAFE_NUMBER
-
ofMillisecond
public static MillisecondSnowflakeId ofMillisecond(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId, long sequenceResetThreshold) Creates a safe millisecond SnowflakeId.- Parameters:
epoch- epoch timestamptimestampBit- bits for timestampmachineBit- bits for machine IDsequenceBit- bits for sequencemachineId- the machine IDsequenceResetThreshold- threshold for sequence reset- Returns:
- a new MillisecondSnowflakeId
-
ofMillisecond
Creates a safe millisecond SnowflakeId with default safe configuration.Default safe configuration:
- Timestamp: 41 bits
- Machine: 3 bits
- Sequence: 9 bits
- Parameters:
machineId- the machine ID (max 7)- Returns:
- MillisecondSnowflakeId
-
ofSecond
public static SecondSnowflakeId ofSecond(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId, long sequenceResetThreshold) Creates a safe second SnowflakeId.- Parameters:
epoch- epoch timestamptimestampBit- bits for timestampmachineBit- bits for machine IDsequenceBit- bits for sequencemachineId- the machine IDsequenceResetThreshold- threshold for sequence reset- Returns:
- a new SecondSnowflakeId
-
ofSecond
Creates a safe second SnowflakeId with default safe configuration.Default safe configuration:
- Timestamp: 31 bits
- Machine: 3 bits
- Sequence: 19 bits
- Parameters:
machineId- the machine ID (max 7)- Returns:
- SecondSnowflakeId
-