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 Summary
ConstructorsConstructorDescriptionTimeBasedGenerator(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: theslotOffsetof a leased machine (api-freeze.md §2.1.1), so its nodes lie in[nodeOffset, nodeOffset + 2^bits).TimeBasedGenerator(String name, Layout layout, SlotHandle slot, TimeSource time, ClockRegressionPolicy regression, GeneratorListener listener) A time-based generator. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Moves the sequencer to its terminal state; no id is issued once this returns.longgenerate()Issues the next id.longThe time of the last issued id.layout()The bit layout of the ids.name()The generator name.The parts of the next id; thread-safe, never the same parts twice.intnode()The node the ids of the current binding carry: the slot plus the node offset.intslot()The slot of the current binding.
-
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 errorslayout- the id layout; its node bits must cover the slot bitsslot- the machine slot, shared by every time-based generator of a runtime (D18)time- the time sourceregression- the clock regression policylistener- 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: theslotOffsetof 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 errorslayout- the id layout; its node bits must covernodeOffsetplus the slotsslot- the machine slotnodeOffset- added to the slot to give the node of the ids, not negativetime- the time sourceregression- the clock regression policylistener- the event listener; a listener that throws is logged and never fails or loses an id
-
-
Method Details
-
name
The generator name.- Returns:
- the name
-
generate
public long generate()Issues the next id.- Specified by:
generatein interfaceIdGenerator- Returns:
- the id
- Throws:
IllegalStateException- if the layout is wider than 63 bits, or the generator is closedTimeOverflowException- if the time no longer fits the time bits of the layoutClockBackwardsException- if the clock went back further than the regression policy allows, or before the epoch
-
nextParts
Description copied from interface:PartsIdGeneratorThe parts of the next id; thread-safe, never the same parts twice.- Specified by:
nextPartsin interfacePartsIdGenerator- Returns:
- the parts
-
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:
closein interfaceAutoCloseable
-