Class ClockSyncSnowflakeId
- All Implemented Interfaces:
Decorator<IdGenerator>,IdGenerator,IdGeneratorDecorator,SnowflakeId,Statistical,StringIdGenerator
-
Field Summary
Fields inherited from interface me.ahoo.cosid.snowflake.SnowflakeId
TOTAL_BIT -
Constructor Summary
ConstructorsConstructorDescriptionClockSyncSnowflakeId(SnowflakeId actual) ClockSyncSnowflakeId(SnowflakeId actual, ClockBackwardsSynchronizer clockBackwardsSynchronizer) -
Method Summary
Modifier and TypeMethodDescriptionlonggenerate()Generate a distributed ID by delegating to the actual generator.Get the actual (wrapped) ID generator that this decorator is enhancing.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.booleanCheck if this Snowflake ID generator is safe for use in JavaScript.stat()Get statistical information about this decorated ID generator.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, idConverter
-
Constructor Details
-
ClockSyncSnowflakeId
-
ClockSyncSnowflakeId
public ClockSyncSnowflakeId(SnowflakeId actual, ClockBackwardsSynchronizer clockBackwardsSynchronizer)
-
-
Method Details
-
getActual
Description copied from interface:IdGeneratorDecoratorGet the actual (wrapped) ID generator that this decorator is enhancing.This method returns the underlying ID generator that this decorator is wrapping. All ID generation requests are typically delegated to this actual generator, with the decorator adding its additional functionality.
- Specified by:
getActualin interfaceDecorator<IdGenerator>- Specified by:
getActualin interfaceIdGeneratorDecorator- Returns:
- The actual ID generator being decorated
-
generate
public long generate()Description copied from interface:IdGeneratorDecoratorGenerate a distributed ID by delegating to the actual generator.This default implementation delegates the ID generation to the actual wrapped generator, ensuring that decorators can focus on adding functionality rather than reimplementing basic generation.
- Specified by:
generatein interfaceIdGenerator- Specified by:
generatein interfaceIdGeneratorDecorator- Returns:
- A unique distributed ID as a long value
-
stat
Description copied from interface:IdGeneratorDecoratorGet statistical information about this decorated ID generator.This method provides combined statistical information from both the decorator itself and the actual generator it wraps, as well as the ID converter being used. This gives a complete picture of the generator's state.
- Specified by:
statin interfaceIdGenerator- Specified by:
statin interfaceIdGeneratorDecorator- Specified by:
statin interfaceSnowflakeId- Specified by:
statin interfaceStatistical- Returns:
- Statistical information about this decorated ID generator
-
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
-
isSafeJavascript
public boolean isSafeJavascript()Description copied from interface:SnowflakeIdCheck if this Snowflake ID generator is safe for use in JavaScript.JavaScript can only safely represent integers up to 2^53 - 1. This method checks if the total bit size of the ID (timestamp + machine + sequence) is less than or equal to the maximum safe JavaScript number of bits, as defined in
SafeJavaScriptSnowflakeId.JAVA_SCRIPT_MAX_SAFE_NUMBER_BIT.- Specified by:
isSafeJavascriptin interfaceSnowflakeId- Returns:
trueif the ID is JavaScript-safe,falseotherwise
-
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
-