java.lang.Object
java.lang.Record
me.ahoo.cosid.time.Layout
- Record Components:
epoch- the time origin; whole seconds forChronoUnit.SECONDSunit- the time unit,ChronoUnit.MILLISorChronoUnit.SECONDStimeBits- the bits of the time part, 1 to 63nodeBits- the bits of the node part, 0 to 31seqBits- the bits of the sequence part, 1 to 62seqResetThreshold- the highest sequence a new time unit may start from (the low-bit scattering of 4.x)
public record Layout(Instant epoch, ChronoUnit unit, int timeBits, int nodeBits, int seqBits, long seqResetThreshold)
extends Record
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final LayoutThe 80-bit CosId of 4.x: milliseconds since the Unix epoch, 44/20/16 bits; parts only (D4).static final InstantThe 4.x epoch, 2019-12-24T16:00:00Z (D16).static final intThe most bits a JavaScript number holds exactly.static final LayoutMilliseconds, 41/3/9 bits: 53 bits, safe in JavaScript.static final LayoutSeconds, 31/3/19 bits: 53 bits, safe in JavaScript.static final intThe most bits alongid may take.static final LayoutMilliseconds, 41/10/12 bits fromCOSID_EPOCH: the 4.x default (D16).static final LayoutSeconds, 31/10/22 bits fromCOSID_EPOCH. -
Constructor Summary
ConstructorsConstructorDescriptionLayout(Instant epoch, ChronoUnit unit, int timeBits, int nodeBits, int seqBits, long seqResetThreshold) Creates an instance of aLayoutrecord class. -
Method Summary
Modifier and TypeMethodDescriptionstatic longdefaultSeqResetThreshold(int seqBits) The 4.x default reset threshold: half the sequence range.epoch()Returns the value of theepochrecord component.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.booleanisJsSafe()Whether every id of this layout is exactly representable in JavaScript.static booleanisJsSafe(long id) Whetheridis exactly representable in JavaScript.intnodeBits()Returns the value of thenodeBitsrecord component.longpack(long time, int node, long seq) Packs the parts into an id.intseqBits()Returns the value of theseqBitsrecord component.longReturns the value of theseqResetThresholdrecord component.inttimeBits()Returns the value of thetimeBitsrecord component.final StringtoString()Returns a string representation of this record class.intunit()Returns the value of theunitrecord component.unpack(long id) Unpacks an id into its parts.withNodeBits(int nodeBits) This layout with another node part; the time and sequence parts stay as they are.
-
Field Details
-
LONG_BITS
public static final int LONG_BITSThe most bits alongid may take.- See Also:
-
JS_SAFE_BITS
public static final int JS_SAFE_BITSThe most bits a JavaScript number holds exactly.- See Also:
-
COSID_EPOCH
The 4.x epoch, 2019-12-24T16:00:00Z (D16). -
SNOWFLAKE_MS
Milliseconds, 41/10/12 bits fromCOSID_EPOCH: the 4.x default (D16). -
SNOWFLAKE_S
Seconds, 31/10/22 bits fromCOSID_EPOCH. -
JS_SAFE_MS
Milliseconds, 41/3/9 bits: 53 bits, safe in JavaScript. -
JS_SAFE_S
Seconds, 31/3/19 bits: 53 bits, safe in JavaScript. -
COSID_80
The 80-bit CosId of 4.x: milliseconds since the Unix epoch, 44/20/16 bits; parts only (D4).
-
-
Constructor Details
-
Layout
public Layout(Instant epoch, ChronoUnit unit, int timeBits, int nodeBits, int seqBits, long seqResetThreshold) Creates an instance of aLayoutrecord class.- Parameters:
epoch- the value for theepochrecord componentunit- the value for theunitrecord componenttimeBits- the value for thetimeBitsrecord componentnodeBits- the value for thenodeBitsrecord componentseqBits- the value for theseqBitsrecord componentseqResetThreshold- the value for theseqResetThresholdrecord component
-
-
Method Details
-
defaultSeqResetThreshold
public static long defaultSeqResetThreshold(int seqBits) The 4.x default reset threshold: half the sequence range.- Parameters:
seqBits- the bits of the sequence part- Returns:
2^(seqBits-1) - 1
-
totalBits
public int totalBits() -
isJsSafe
public boolean isJsSafe()Whether every id of this layout is exactly representable in JavaScript.- Returns:
- whether
totalBits()is at most 53
-
isJsSafe
public static boolean isJsSafe(long id) Whetheridis exactly representable in JavaScript.- Parameters:
id- the id- Returns:
- whether
0 <= id < 2^53
-
withNodeBits
This layout with another node part; the time and sequence parts stay as they are.- Parameters:
nodeBits- the bits of the node part- Returns:
- the layout
- Throws:
IllegalArgumentException- if a layout that packed alongno longer would
-
pack
public long pack(long time, int node, long seq) Packs the parts into an id.- Parameters:
time- the time, in units since the epochnode- the nodeseq- the sequence- Returns:
- the id
- Throws:
IllegalStateException- if the layout is wider than 63 bits
-
unpack
Unpacks an id into its parts.- Parameters:
id- the id- Returns:
- the parts
- Throws:
IllegalStateException- if the layout is wider than 63 bitsIllegalArgumentException- if the id has bits beyondtotalBits()
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with '=='. -
epoch
Returns the value of theepochrecord component.- Returns:
- the value of the
epochrecord component
-
unit
Returns the value of theunitrecord component.- Returns:
- the value of the
unitrecord component
-
timeBits
public int timeBits()Returns the value of thetimeBitsrecord component.- Returns:
- the value of the
timeBitsrecord component
-
nodeBits
public int nodeBits()Returns the value of thenodeBitsrecord component.- Returns:
- the value of the
nodeBitsrecord component
-
seqBits
public int seqBits()Returns the value of theseqBitsrecord component.- Returns:
- the value of the
seqBitsrecord component
-
seqResetThreshold
public long seqResetThreshold()Returns the value of theseqResetThresholdrecord component.- Returns:
- the value of the
seqResetThresholdrecord component
-