Class TimeBasedGenerator

java.lang.Object
me.ahoo.cosid.time.TimeBasedGenerator
All Implemented Interfaces:
AutoCloseable, IdGenerator, PartsIdGenerator

public final class TimeBasedGenerator extends Object implements IdGenerator, PartsIdGenerator, AutoCloseable
The time-based generator: Snowflake layouts and the 80-bit CosId alike (03-core-model.md §4). Reads the slot binding once per id and checks the slot window before publishing the new state (§4.1); issuing is a short critical section and never waits while holding it.

Its public constructors are the assembly entry CosIdRuntime uses (R2-core-03): they keep their signatures, and a new parameter comes only through a builder or a static factory, never as one more positional overload (api-freeze.md §4.2).

  • Constructor Details

    • TimeBasedGenerator

      public TimeBasedGenerator(String name, Layout layout, SlotHandle slot, TimeSource time, ClockRegressionPolicy regression, GeneratorListener listener)
      A time-based generator.
      Parameters:
      name - the generator name, for events and errors
      layout - the id layout; its node bits must cover the slot bits
      slot - the machine slot, shared by every time-based generator of a runtime (D18)
      time - the time source
      regression - the clock regression policy
      listener - the event listener; a listener that throws is logged and never fails or loses an id
    • TimeBasedGenerator

      public TimeBasedGenerator(String name, Layout layout, SlotHandle slot, int nodeOffset, TimeSource time, ClockRegressionPolicy regression, GeneratorListener listener)
      A time-based generator whose node is the slot plus an offset: the slotOffset of a leased machine (api-freeze.md §2.1.1), so its nodes lie in [nodeOffset, nodeOffset + 2^bits).
      Parameters:
      name - the generator name, for events and errors
      layout - the id layout; its node bits must cover nodeOffset plus the slots
      slot - the machine slot
      nodeOffset - added to the slot to give the node of the ids, not negative
      time - the time source
      regression - the clock regression policy
      listener - the event listener; a listener that throws is logged and never fails or loses an id
  • Method Details

    • name

      public String name()
      The generator name.
      Returns:
      the name
    • generate

      public long generate()
      Issues the next id.
      Specified by:
      generate in interface IdGenerator
      Returns:
      the id
      Throws:
      IllegalStateException - if the layout is wider than 63 bits, or the generator is closed
      TimeOverflowException - if the time no longer fits the time bits of the layout
      ClockBackwardsException - if the clock went back further than the regression policy allows, or before the epoch
    • nextParts

      public IdParts nextParts()
      Description copied from interface: PartsIdGenerator
      The parts of the next id; thread-safe, never the same parts twice.
      Specified by:
      nextParts in interface PartsIdGenerator
      Returns:
      the parts
    • layout

      public Layout layout()
      The bit layout of the ids.
      Returns:
      the layout
    • slot

      public int slot()
      The slot of the current binding.
      Returns:
      the slot
    • node

      public int node()
      The node the ids of the current binding carry: the slot plus the node offset.
      Returns:
      the node
    • lastIssuedMillis

      public long lastIssuedMillis()
      The time of the last issued id.
      Returns:
      milliseconds since the Unix epoch, 0 when none was issued; exact once close() returned
    • close

      public void close()
      Moves the sequencer to its terminal state; no id is issued once this returns.
      Specified by:
      close in interface AutoCloseable