Package io.hypersistence.tsid
Class TSID.Factory.Builder
- java.lang.Object
-
- io.hypersistence.tsid.TSID.Factory.Builder
-
- Enclosing class:
- TSID.Factory
public static class TSID.Factory.Builder extends java.lang.ObjectA nested class that builds custom TSID factories.It is used to setup a custom
TSID.Factory.
-
-
Field Summary
Fields Modifier and Type Field Description private java.time.Clockclockprivate java.lang.LongcustomEpochprivate java.lang.Integernodeprivate java.lang.IntegernodeBitsprivate TSID.Factory.IRandomrandom
-
Constructor Summary
Constructors Constructor Description Builder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TSID.Factorybuild()Returns a build TSID factory.protected java.time.ClockgetClock()Gets the clock to be used in tests.protected java.lang.LonggetCustomEpoch()Gets the custom epoch.protected java.lang.IntegergetNode()Get the node identifier.protected java.lang.IntegergetNodeBits()Get the node identifier bits length within the range 0 to 20.protected TSID.Factory.IRandomgetRandom()Gets the random generator.TSID.Factory.BuilderwithClock(java.time.Clock clock)Set the clock to be used in tests.TSID.Factory.BuilderwithCustomEpoch(java.time.Instant customEpoch)Set the custom epoch.TSID.Factory.BuilderwithNode(java.lang.Integer node)Set the node identifier.TSID.Factory.BuilderwithNodeBits(java.lang.Integer nodeBits)Set the node identifier bits length.TSID.Factory.BuilderwithRandom(java.util.Random random)Set the random generator.TSID.Factory.BuilderwithRandomFunction(java.util.function.IntFunction<byte[]> randomFunction)Set the random function.TSID.Factory.BuilderwithRandomFunction(java.util.function.IntSupplier randomFunction)Set the random function.
-
-
-
Field Detail
-
node
private java.lang.Integer node
-
nodeBits
private java.lang.Integer nodeBits
-
customEpoch
private java.lang.Long customEpoch
-
random
private TSID.Factory.IRandom random
-
clock
private java.time.Clock clock
-
-
Method Detail
-
withNode
public TSID.Factory.Builder withNode(java.lang.Integer node)
Set the node identifier.- Parameters:
node- a number that must be between 0 and 2^nodeBits-1.- Returns:
TSID.Factory.Builder- Throws:
java.lang.IllegalArgumentException- if the node identifier is out of the range [0, 2^nodeBits-1] whenbuild()is invoked
-
withNodeBits
public TSID.Factory.Builder withNodeBits(java.lang.Integer nodeBits)
Set the node identifier bits length.- Parameters:
nodeBits- a number that must be between 0 and 20.- Returns:
TSID.Factory.Builder- Throws:
java.lang.IllegalArgumentException- if the node bits are out of the range [0, 20] whenbuild()is invoked
-
withCustomEpoch
public TSID.Factory.Builder withCustomEpoch(java.time.Instant customEpoch)
Set the custom epoch.- Parameters:
customEpoch- an instant that represents the custom epoch.- Returns:
TSID.Factory.Builder
-
withRandom
public TSID.Factory.Builder withRandom(java.util.Random random)
Set the random generator.The random generator is used to create a random function that is used to reset the counter when the millisecond changes.
- Parameters:
random- aRandomgenerator- Returns:
TSID.Factory.Builder
-
withRandomFunction
public TSID.Factory.Builder withRandomFunction(java.util.function.IntSupplier randomFunction)
Set the random function.The random function is used to reset the counter when the millisecond changes.
- Parameters:
randomFunction- a random function that returns a integer value- Returns:
TSID.Factory.Builder
-
withRandomFunction
public TSID.Factory.Builder withRandomFunction(java.util.function.IntFunction<byte[]> randomFunction)
Set the random function.The random function must return a byte array of a given length.
The random function is used to reset the counter when the millisecond changes.
Despite its name, the random function MAY return a fixed value, for example, if your app requires the counter to be reset to ZERO whenever the millisecond changes, like Twitter Snowflakes, this function should return an array filled with ZEROS.
- Parameters:
randomFunction- a random function that returns a byte array- Returns:
TSID.Factory.Builder
-
withClock
public TSID.Factory.Builder withClock(java.time.Clock clock)
Set the clock to be used in tests.- Parameters:
clock- a clock- Returns:
TSID.Factory.Builder
-
getNode
protected java.lang.Integer getNode()
Get the node identifier.- Returns:
- a number
- Throws:
java.lang.IllegalArgumentException- if the node is out of range
-
getNodeBits
protected java.lang.Integer getNodeBits()
Get the node identifier bits length within the range 0 to 20.- Returns:
- a number
- Throws:
java.lang.IllegalArgumentException- if the node bits are out of range
-
getCustomEpoch
protected java.lang.Long getCustomEpoch()
Gets the custom epoch.- Returns:
- a number
-
getRandom
protected TSID.Factory.IRandom getRandom()
Gets the random generator.- Returns:
- a random generator
-
getClock
protected java.time.Clock getClock()
Gets the clock to be used in tests.- Returns:
- a clock
-
build
public TSID.Factory build()
Returns a build TSID factory.- Returns:
TSID.Factory- Throws:
java.lang.IllegalArgumentException- if the node is out of rangejava.lang.IllegalArgumentException- if the node bits are out of range
-
-