Class AbstractSnowflakeId
- All Implemented Interfaces:
IdGenerator,SnowflakeId,Statistical,StringIdGenerator
- Direct Known Subclasses:
MillisecondSnowflakeId,SecondSnowflakeId
This abstract class provides the base implementation for Snowflake ID generation,
handling the common logic for timestamp management, sequence counting, and ID assembly.
Subclasses implement getCurrentTime() to provide time in different units
(milliseconds, seconds, etc.).
The ID is composed of: timestamp (configurable bits) + machine ID (configurable bits) + sequence (configurable bits)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final longEpoch timestamp used as the base for time calculations.protected longTimestamp of the last generated ID.protected final intNumber of bits allocated for the machine ID portion.protected final longThe machine ID value for this instance.protected final longNumber of bits to shift machine ID left (equal to sequenceBit).protected final intMaximum machine ID value representable by the machine ID bits.protected final longMaximum sequence value representable by the sequence bits.protected final longMaximum timestamp value representable by the timestamp bits.protected longCurrent sequence counter value.protected final intNumber of bits allocated for the sequence portion.protected final intNumber of bits allocated for the timestamp portion.protected final longNumber of bits to shift timestamp left (equal to sequenceBit + machineBit).Fields inherited from interface me.ahoo.cosid.snowflake.SnowflakeId
TOTAL_BIT -
Constructor Summary
ConstructorsConstructorDescriptionAbstractSnowflakeId(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId, long sequenceResetThreshold) Creates a new AbstractSnowflakeId. -
Method Summary
Modifier and TypeMethodDescriptionlonggenerate()Generates the next unique ID.protected abstract longGets the current time in the appropriate unit for this snowflake ID variant.longgetEpoch()Get the epoch timestamp used as the base for time calculations.longGet the timestamp of the last ID that was generated.intGet the number of bits used for the machine ID portion.intGet the machine ID assigned to this generator.intGet the maximum machine ID value that can be represented.longGet the maximum sequence value that can be represented.longGet the maximum timestamp value that can be represented.intGet the number of bits used for the sequence portion.intGet the number of bits used for the timestamp portion.protected longnextTime()Waits until the current time is greater than the last timestamp.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface me.ahoo.cosid.IdGenerator
generateAsString, idConverterMethods inherited from interface me.ahoo.cosid.snowflake.SnowflakeId
isSafeJavascript, stat
-
Field Details
-
epoch
protected final long epochEpoch timestamp used as the base for time calculations. -
timestampBit
protected final int timestampBitNumber of bits allocated for the timestamp portion. -
machineBit
protected final int machineBitNumber of bits allocated for the machine ID portion. -
sequenceBit
protected final int sequenceBitNumber of bits allocated for the sequence portion. -
maxTimestamp
protected final long maxTimestampMaximum timestamp value representable by the timestamp bits. -
maxSequence
protected final long maxSequenceMaximum sequence value representable by the sequence bits. -
maxMachineId
protected final int maxMachineIdMaximum machine ID value representable by the machine ID bits. -
machineLeft
protected final long machineLeftNumber of bits to shift machine ID left (equal to sequenceBit). -
timestampLeft
protected final long timestampLeftNumber of bits to shift timestamp left (equal to sequenceBit + machineBit). -
machineId
protected final long machineIdThe machine ID value for this instance.Note: When machineLeft is greater than 30, overflow can occur during calculation, so machineId should be kept as long during arithmetic operations.
-
sequence
protected long sequenceCurrent sequence counter value. -
lastTimestamp
protected long lastTimestampTimestamp of the last generated ID.
-
-
Constructor Details
-
AbstractSnowflakeId
public AbstractSnowflakeId(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId, long sequenceResetThreshold) Creates a new AbstractSnowflakeId.- Parameters:
epoch- epoch timestamp in millisecondstimestampBit- number of bits for timestampmachineBit- number of bits for machine IDsequenceBit- number of bits for sequencemachineId- the machine ID valuesequenceResetThreshold- threshold for resetting sequence on timestamp advance- Throws:
IllegalArgumentException- if total bits exceed 63 or machineId is invalid
-
-
Method Details
-
nextTime
protected long nextTime()Waits until the current time is greater than the last timestamp.- Returns:
- the next valid timestamp
-
getCurrentTime
protected abstract long getCurrentTime()Gets the current time in the appropriate unit for this snowflake ID variant.- Returns:
- current time value
-
generate
public long generate()Generates the next unique ID.This method is synchronized to ensure thread-safe ID generation.
- Specified by:
generatein interfaceIdGenerator- Returns:
- a unique snowflake ID
- Throws:
ClockBackwardsException- if system clock has moved backwardsTimestampOverflowException- if timestamp exceeds maximum value
-
getEpoch
public long getEpoch()Description copied from interface:SnowflakeIdGet the epoch timestamp used as the base for time calculations.This is the timestamp (in milliseconds) that serves as the starting point for the timestamp portion of generated IDs. IDs generated by this instance will have timestamps relative to this epoch.
- Specified by:
getEpochin interfaceSnowflakeId- Returns:
- The epoch timestamp in milliseconds
-
getTimestampBit
public int getTimestampBit()Description copied from interface:SnowflakeIdGet the number of bits used for the timestamp portion.This determines the range of time that can be represented by the timestamp portion of the ID. More bits allow for a longer time range but leave fewer bits for machine ID and sequence.
- Specified by:
getTimestampBitin interfaceSnowflakeId- Returns:
- The number of timestamp bits
-
getMachineBit
public int getMachineBit()Description copied from interface:SnowflakeIdGet the number of bits used for the machine ID portion.This determines how many unique machines can participate in ID generation. More bits allow for more machines but leave fewer bits for timestamp and sequence.
- Specified by:
getMachineBitin interfaceSnowflakeId- Returns:
- The number of machine ID bits
-
getSequenceBit
public int getSequenceBit()Description copied from interface:SnowflakeIdGet the number of bits used for the sequence portion.This determines how many IDs can be generated per time unit (e.g., per millisecond). More bits allow for higher throughput within a single time unit but leave fewer bits for timestamp and machine ID.
- Specified by:
getSequenceBitin interfaceSnowflakeId- Returns:
- The number of sequence bits
-
getMaxTimestamp
public long getMaxTimestamp()Description copied from interface:SnowflakeIdGet the maximum timestamp value that can be represented.This is the maximum value that can be stored in the timestamp portion of the ID, which determines how long the generator can be used before exhausting the timestamp space.
- Specified by:
getMaxTimestampin interfaceSnowflakeId- Returns:
- The maximum timestamp value
-
getMaxMachineId
public int getMaxMachineId()Description copied from interface:SnowflakeIdGet the maximum machine ID value that can be represented.This is the maximum value that can be stored in the machine ID portion of the ID, which determines how many unique machines can participate in ID generation.
- Specified by:
getMaxMachineIdin interfaceSnowflakeId- Returns:
- The maximum machine ID value
-
getMaxSequence
public long getMaxSequence()Description copied from interface:SnowflakeIdGet the maximum sequence value that can be represented.This is the maximum value that can be stored in the sequence portion of the ID, which determines how many IDs can be generated per time unit.
- Specified by:
getMaxSequencein interfaceSnowflakeId- Returns:
- The maximum sequence value
-
getLastTimestamp
public long getLastTimestamp()Description copied from interface:SnowflakeIdGet the timestamp of the last ID that was generated.This is used for clock synchronization and to ensure proper ordering of generated IDs. It can also help detect clock drift.
- Specified by:
getLastTimestampin interfaceSnowflakeId- Returns:
- The timestamp of the last generated ID
-
getMachineId
public int getMachineId()Description copied from interface:SnowflakeIdGet the machine ID assigned to this generator.This is the unique identifier for the machine or instance that is generating IDs, which ensures global uniqueness across the distributed system.
- Specified by:
getMachineIdin interfaceSnowflakeId- Returns:
- The machine ID
-