Class SecondSnowflakeId

java.lang.Object
me.ahoo.cosid.snowflake.AbstractSnowflakeId
me.ahoo.cosid.snowflake.SecondSnowflakeId
All Implemented Interfaces:
IdGenerator, SnowflakeId, Statistical, StringIdGenerator

public class SecondSnowflakeId extends AbstractSnowflakeId
Second-based Snowflake ID generator.

Similar to MillisecondSnowflakeId but uses seconds instead of milliseconds as the time unit. This allows for a longer timestamp range but with lower time precision.

Default configuration:

  • Timestamp bits: 31 (about 68 years with second precision)
  • Machine ID bits: 10 (1024 unique machines)
  • Sequence bits: 22 (about 4 million IDs per second per machine)
  • Field Details

    • DEFAULT_TIMESTAMP_BIT

      public static final int DEFAULT_TIMESTAMP_BIT
      Default number of timestamp bits (31 bits).
      See Also:
    • DEFAULT_MACHINE_BIT

      public static final int DEFAULT_MACHINE_BIT
      Default number of machine ID bits (10 bits).
      See Also:
    • DEFAULT_SEQUENCE_BIT

      public static final int DEFAULT_SEQUENCE_BIT
      Default number of sequence bits (22 bits).
      See Also:
    • DEFAULT_SEQUENCE_RESET_THRESHOLD

      public static final long DEFAULT_SEQUENCE_RESET_THRESHOLD
      Default sequence reset threshold (half of max sequence).
      See Also:
  • Constructor Details

    • SecondSnowflakeId

      public SecondSnowflakeId(int machineId)
      Creates a SecondSnowflakeId with default configuration.
      Parameters:
      machineId - the machine ID
    • SecondSnowflakeId

      public SecondSnowflakeId(int machineBit, int machineId)
      Creates a SecondSnowflakeId with custom machine bits.
      Parameters:
      machineBit - the number of bits for machine ID
      machineId - the machine ID
    • SecondSnowflakeId

      public SecondSnowflakeId(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId)
      Creates a SecondSnowflakeId with custom bit configuration.
      Parameters:
      epoch - epoch timestamp in milliseconds
      timestampBit - number of bits for timestamp
      machineBit - number of bits for machine ID
      sequenceBit - number of bits for sequence
      machineId - the machine ID
    • SecondSnowflakeId

      public SecondSnowflakeId(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId, long sequenceResetThreshold)
      Creates a SecondSnowflakeId with full custom configuration.
      Parameters:
      epoch - epoch timestamp in milliseconds
      timestampBit - number of bits for timestamp
      machineBit - number of bits for machine ID
      sequenceBit - number of bits for sequence
      machineId - the machine ID
      sequenceResetThreshold - threshold for resetting sequence
  • Method Details

    • getCurrentTime

      protected long getCurrentTime()
      Description copied from class: AbstractSnowflakeId
      Gets the current time in the appropriate unit for this snowflake ID variant.
      Specified by:
      getCurrentTime in class AbstractSnowflakeId
      Returns:
      current time value